Skip to main content

langchain-ibm

This package provides the integration between LangChain and IBM watsonx.ai through the ibm-watsonx-ai SDK.

Installation

To use the langchain-ibm package, follow these installation steps:

pip install -U langchain-ibm

Setting up

To use IBM's models, you must have an IBM Cloud user API key. Here's how to obtain and set up your API key:

  1. Obtain an API Key: For more details on how to create and manage an API key, refer to IBM's documentation.
  2. Set the API Key as an Environment Variable: For security reasons, it's recommended to not hard-code your API key directly in your scripts. Instead, set it up as an environment variable. You can use the following code to prompt for the API key and set it as an environment variable:
import os
from getpass import getpass

watsonx_api_key = getpass()
os.environ["WATSONX_API_KEY"] = watsonx_api_key

In alternative, you can set the environment variable in your terminal.

  • Linux/macOS: Open your terminal and execute the following command:

    export WATSONX_API_KEY='your_ibm_api_key'
    

    To make this environment variable persistent across terminal sessions, add the above line to your ~/.bashrc, ~/.bash_profile, or ~/.zshrc file.

  • Windows: For Command Prompt, use:

    set WATSONX_API_KEY=your_ibm_api_key
    

Setting parameters

You might need to adjust model parameters for different models or tasks. For more details on the parameters, refer to Parameter Scheme IBM's documentation.

Note: You must use the correct parameter schema for the class you are initializing:

This example uses ChatWatsonx, so we import TextChatParameters.

from ibm_watsonx_ai.foundation_models.schema import TextChatParameters

parameters = TextChatParameters(
    temperature=0.5,
    max_completion_tokens=1024,
    top_p=1,
)

You can also pass it as a dictionary object.

parameters = {
    "temperature": 0.5,
    "max_completion_tokens": 1024,
    "top_p": 1,
}

Chat Models

ChatWatsonx class exposes chat models from IBM.

Initialization the ChatWatsonx class with the previously set parameters.

from langchain_ibm import ChatWatsonx

model = ChatWatsonx(
    model_id="PASTE THE CHOSEN MODEL_ID HERE",
    url="PASTE YOUR URL HERE",
    project_id="PASTE YOUR PROJECT_ID HERE",
    params=parameters,
)

model.invoke("Sing a ballad of LangChain.")

Note:

  • You must provide a project_id or space_id. For more information refer to IBM's documentation.
  • Depending on the region of your provisioned service instance, use one of the urls described here.
  • You need to specify the model you want to use for inferencing through model_id. You can find the list of available models here.

Alternatively for all classes you can use Cloud Pak for Data credentials. For more details, refer to IBM's documentation.

from langchain_ibm import ChatWatsonx

model = ChatWatsonx(
    model_id="ibm/granite-4-h-small",
    url="PASTE YOUR URL HERE",
    username="PASTE YOUR USERNAME HERE",
    password="PASTE YOUR PASSWORD HERE",
    project_id="PASTE YOUR PROJECT_ID HERE",
    params=parameters,
)

Embedding Models

WatsonxEmbeddings class exposes embeddings from IBM.

from langchain_ibm import WatsonxEmbeddings
from ibm_watsonx_ai.metanames import EmbedTextParamsMetaNames

embed_params = {
    EmbedTextParamsMetaNames.TRUNCATE_INPUT_TOKENS: 3,
    EmbedTextParamsMetaNames.RETURN_OPTIONS: {"input_text": True},
}

embeddings = WatsonxEmbeddings(
    model_id="ibm/granite-embedding-107m-multilingual",
    url="https://us-south.ml.cloud.ibm.com",
    project_id="PASTE YOUR PROJECT_ID HERE",
    params=embed_params,
)

embeddings.embed_query("What is the meaning of life?")

LLMs

WatsonxLLM class exposes LLMs from IBM.

from langchain_ibm import WatsonxLLM
from ibm_watsonx_ai.foundation_models.schema import TextGenParameters, TextGenDecodingMethod

parameters = TextGenParameters(
    decoding_method=TextGenDecodingMethod.SAMPLE,
    temperature=0.5,
    top_k=50,
    top_p=1
)

llm = WatsonxLLM(
    model_id="ibm/granite-4-h-small",
    url="https://us-south.ml.cloud.ibm.com",
    project_id="PASTE YOUR PROJECT_ID HERE",
    params=parameters,
)

llm.invoke("The meaning of life is")

Reranker

WatsonxRerank class exposes reranker from IBM.

from langchain_ibm import WatsonxRerank

rerank = WatsonxRerank(
    model_id="cross-encoder/ms-marco-minilm-l-12-v2",
    url="https://us-south.ml.cloud.ibm.com",
    project_id="PASTE YOUR PROJECT_ID HERE",
)

Toolkit

WatsonxToolkit class exposes Toolkit from IBM.

from langchain_ibm.agent_toolkits.utility import WatsonxToolkit

watsonx_toolkit = WatsonxToolkit(
    url="https://us-south.ml.cloud.ibm.com",
)

Metadata

Release files for langchain-ibm 1.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langchain-ibm 1.1.2
File Size Uploaded
langchain_ibm-1.1.2.tar.gz 75.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for langchain-ibm 1.1.2
File Interpreter ABI Platform
langchain_ibm-1.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 129.9 kB

Release files / langchain_ibm-1.1.2.tar.gz

Download URL langchain_ibm-1.1.2.tar.gz
Size 75.1 kB
Tags Source
SHA-256 checksum
How to use checksums
ae162e7a8eaaf72a945abc92609638049619691e3f4a7c338b80a360574f0bbc
BLAKE2b-256 checksum
How to use checksums
4277f1729121f208ddfeae5995631ebd4011a8a7f9dee81456396548a303d23d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / langchain_ibm-1.1.2-py3-none-any.whl

Download URL langchain_ibm-1.1.2-py3-none-any.whl
Size 54.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bf62265f8734072746e6e804e9af73d934fb578fedc0388b81778d45352642b0
BLAKE2b-256 checksum
How to use checksums
cf0a1afc846f961f1509f30b685babf19e62e6ebfb9822fdf200696aeb33fd0a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Release history Release notifications | RSS feed

This release

1.1.2 This release

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.11

2 release files

1.0.9

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.3.22

2 release files

0.3.21

2 release files

0.3.20

2 release files

0.3.19

2 release files

0.3.18

2 release files

0.3.17

2 release files

0.3.16

2 release files

0.3.13

2 release files

0.3.11

2 release files

0.3.9

2 release files

0.3.8

2 release files

0.3.7

2 release files

0.3.6

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.11

2 release files

0.1.10

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page