IBM Watson Machine Learning Client
The `ibm-watson-machine-learning` library provides a Python API client for interacting with the IBM Watson Machine Learning service on IBM Cloud and IBM Cloud Pak for Data. It enables users to programmatically train, test, store, and deploy machine learning models as APIs. The library is currently in maintenance mode (as of IBM Cloud Pak for Data 5.0.x, June 2024), with a recommendation to migrate to the `ibm-watsonx-ai` package for enhanced functionalities.
Common errors
-
Library not compatible or missing
cause This error occurs when importing an AutoAI notebook from a catalog due to a mismatch between the notebook's saved runtime environment and the project's current runtime environment.fixUpdate the runtime environment of your project to the latest supported version (e.g., Runtime 24.1 if the notebook used 23.1) and ensure adequate computing resources. -
Error 502 - Bad Gateway
cause Deployment failures after upgrading IBM Cloud Pak for Data (e.g., from 4.7.0 to 4.8.4) can be caused by outdated constricted software specifications for deployed assets.fixUpdate the constricted software specification for your deployed asset to use the latest supported version. Alternatively, if no longer needed, delete the application deployment. -
IncompatibleVersionError('Failed to deserialize the batch output from bytes. The bytes input might be corrupted or produced by an incompatible version of WDU.')cause This error can occur when running a text extraction request using the watsonx.ai REST API.fixRe-run the text extraction request. This often resolves transient deserialization issues. -
Connecting to Kernel... Connection failed. Reconnecting... connection failed error message
cause When working with notebooks in Watson Studio behind a firewall, the firewall might be blocking the WebSocket connection required for the kernel.fixAdd the WebSocket connection `wss://dataplatform.cloud.ibm.com` to your firewall settings to allow notebook connectivity.
Warnings
- breaking The `ibm-watson-machine-learning` package is in maintenance mode and is deprecated as of IBM Cloud Pak for Data 5.0.x (June 2024). IBM recommends migrating to the `ibm-watsonx-ai` package for new development and enhanced functionalities.
- breaking Runtime 23.1 for deploying AI assets has been discontinued. Model types and software specifications deployed with Runtime 23.1 will no longer function, potentially leading to deployment failures.
- gotcha Upgrading Watson Machine Learning, especially within IBM Cloud Pak for Data environments, may fail due to runtime errors, orphaned objects, or issues with include_vars tasks.
- gotcha Batch deployments that process large volumes of data as input might fail due to internal timeout settings, resulting in errors like 'Incorrect input data: Flight returned internal error'.
Install
-
pip install ibm-watson-machine-learning
Imports
- APIClient
from ibm_watson_machine_learning import APIClient
from ibm_watson_machine_learning.APIClient import APIClient
Quickstart
import os
from ibm_watson_machine_learning.APIClient import APIClient
# --- IMPORTANT: This library is in maintenance mode. Consider ibm-watsonx-ai ---
# Replace with your IBM Cloud API Key and Watson Machine Learning service endpoint.
# For IBM Cloud: API_KEY and the service_endpoint (e.g., 'https://us-south.ml.cloud.ibm.com')
# For Cloud Pak for Data: USERNAME, PASSWORD, and service_endpoint (e.g., 'https://<your_cpd_cluster_host>')
# Example for IBM Cloud (API Key authentication):
api_key = os.environ.get('IBM_CLOUD_API_KEY', 'YOUR_IBM_CLOUD_API_KEY')
service_endpoint = os.environ.get('WML_SERVICE_ENDPOINT', 'https://us-south.ml.cloud.ibm.com') # Example endpoint
if 'YOUR_IBM_CLOUD_API_KEY' in api_key or 'YOUR_CPD_CLUSTER_HOST' in service_endpoint:
print("Please set IBM_CLOUD_API_KEY and WML_SERVICE_ENDPOINT environment variables or replace placeholders.")
else:
try:
wml_credentials = {
"apikey": api_key,
"url": service_endpoint
}
client = APIClient(wml_credentials)
print("IBM Watson Machine Learning client initialized successfully.")
print(f"Client version: {client.version}")
# Example: Get spaces (requires a WML service instance and configured spaces)
# spaces = client.spaces.get_details()
# print("Available WML Spaces:")
# for space in spaces['resources']:
# print(f"- {space['entity']['name']} (ID: {space['metadata']['id']})")
except Exception as e:
print(f"Error initializing WML client: {e}")
print("Ensure your API key and service endpoint are correct and you have access to the service.")