IBM watsonx.ai embedding model integration.
To use, you should have langchain_ibm python package installed,
and the environment variable WATSONX_API_KEY set with your API key, or pass
it as a named parameter api_key to the constructor.
pip install -U langchain-ibm
# or using uv
uv add langchain-ibm
export WATSONX_API_KEY="your-api-key"
apikey and WATSONX_APIKEY are deprecated and will be removed in
version 2.0.0. Use api_key and WATSONX_API_KEY instead.
from langchain_ibm import WatsonxEmbeddings
embeddings = WatsonxEmbeddings(
model_id="ibm/granite-embedding-278m-multilingual",
url="https://us-south.ml.cloud.ibm.com",
project_id="*****",
# api_key="*****"
)input_text = "The meaning of life is 42"
vector = embeddings.embed_query("hello")
print(vector[:3])
[-0.0020519258, 0.0147288125, -0.0090887165]vectors = embeddings.embed_documents(["hello", "goodbye"])
# Showing only the first 3 coordinates
print(len(vectors))
print(vectors[0][:3])
2
[-0.0020519265, 0.01472881, -0.009088721]await embeddings.aembed_query(input_text)
print(vector[:3])
# multiple:
# await embeddings.aembed_documents(input_texts)
[-0.0020519258, 0.0147288125, -0.0090887165]WatsonxEmbeddings()BaseModelLangChainEmbeddingsType of model to use.
Name or alias of the foundation model to use. When using IBM's watsonx.ai Model Gateway (public preview), you can specify any supported third-party model—OpenAI, Anthropic, NVIDIA, Cerebras, or IBM's own Granite series—via a single, OpenAI-compatible interface. Models must be explicitly provisioned (opt-in) through the Gateway to ensure secure, vendor-agnostic access and easy switch-over without reconfiguration.
For more details on configuration and usage, see IBM watsonx Model Gateway docs
ID of the Watson Studio project.
ID of the Watson Studio space.
URL to the Watson Machine Learning or CPD instance.
API key to the Watson Machine Learning or CPD instance.
Token to the CPD instance.
Password to the CPD instance.
Username to the CPD instance.
Instance_id of the CPD instance.
Version of the CPD instance.
Model parameters to use during request generation.
You can pass one of following as verify: