Skip to main content
For the complete documentation index for agents and LLMs, see llms.txt.

WatsonxChatGenerator

Generate chat responses using IBM watsonx.ai foundation models.

Key Features

  • Supports IBM's foundation models available on watsonx.ai, including Granite and Llama models.
  • Accepts and returns messages in ChatMessage format.
  • Supports multimodal inputs with text and images.
  • Supports streaming responses through a configurable callback.
  • Supports tool calling for agentic workflows.
  • Configurable IBM Cloud region through api_base_url.

Configuration

  1. Drag the WatsonxChatGenerator component onto the canvas from the Component Library.
  2. Click on the component to open the configuration panel.
  3. On the General tab:
    1. Set the model to the watsonx.ai model you want to use (for example, ibm/granite-4-h-small). For a full list, see the IBM watsonx.ai documentation.
    2. Create secrets for your watsonx.ai credentials. Use WATSONX_API_KEY for the API key and WATSONX_PROJECT_ID for the project ID. For instructions, see Create Secrets.
  4. Go to the Advanced tab to configure generation_kwargs, api_base_url, timeout, and tools.

Connections

WatsonxChatGenerator receives a list of ChatMessage objects, typically from PromptBuilder or ChatPromptBuilder. It outputs a list of reply ChatMessage objects you can connect to AnswerBuilder or other downstream components.

Source Code

To check this component's source code, open chat_generator.py in the Haystack Core Integrations repository.

Usage Examples

Basic Configuration

  WatsonxChatGenerator:
type: haystack_integrations.components.generators.watsonx.chat.WatsonxChatGenerator
init_parameters:
api_key:
type: env_var
env_vars:
- WATSONX_API_KEY
strict: false
project_id:
type: env_var
env_vars:
- WATSONX_PROJECT_ID
strict: false
model: ibm/granite-4-h-small
api_base_url: https://us-south.ml.cloud.ibm.com
generation_kwargs:
max_tokens: 1024

Using the Component in a Pipeline

# haystack-pipeline
components:
prompt_builder:
type: haystack.components.builders.chat_prompt_builder.ChatPromptBuilder
init_parameters:
required_variables: "*"
template:
- role: user
content: "Answer the following question: {{ question }}"

llm:
type: haystack_integrations.components.generators.watsonx.chat.WatsonxChatGenerator
init_parameters:
api_key:
type: env_var
env_vars:
- WATSONX_API_KEY
strict: false
project_id:
type: env_var
env_vars:
- WATSONX_PROJECT_ID
strict: false
model: ibm/granite-4-h-small
generation_kwargs:
max_tokens: 1024

answer_builder:
type: deepset_cloud_custom_nodes.augmenters.deepset_answer_builder.DeepsetAnswerBuilder
init_parameters:
reference_pattern: acm

connections:
- sender: prompt_builder.prompt
receiver: llm.messages
- sender: llm.replies
receiver: answer_builder.replies

max_runs_per_component: 100

metadata: {}

inputs:
query:
- answer_builder.query
- prompt_builder.question

outputs:
answers: answer_builder.answers

Parameters

Inputs

ParameterTypeDescription
messagesList[ChatMessage]A list of chat messages representing the conversation so far.

Outputs

ParameterTypeDescription
repliesList[ChatMessage]A list of generated reply messages from the model.

Init Parameters

These are the parameters you can configure in Pipeline Builder:

ParameterTypeDefaultDescription
api_keySecretSecret.from_env_var("WATSONX_API_KEY")The IBM Cloud API key for watsonx.ai.
modelstribm/granite-4-h-smallThe name of the watsonx.ai foundation model. For a full list, see the IBM documentation.
project_idSecretSecret.from_env_var("WATSONX_PROJECT_ID")The watsonx.ai project ID.
api_base_urlstrhttps://us-south.ml.cloud.ibm.comThe IBM Cloud watsonx.ai API base URL. Change the region prefix (for example, eu-de) to use a different IBM Cloud region.
generation_kwargsOptional[Dict[str, Any]]NoneAdditional generation parameters for the watsonx.ai API, such as max_tokens, temperature, top_p, and stop_sequences.
timeoutOptional[float]NoneRequest timeout in seconds.
max_retriesOptional[int]NoneMaximum number of retries on API errors.
verifyOptional[Union[bool, str]]NoneSSL certificate verification setting.
streaming_callbackOptional[Callable]NoneA callback function for streaming responses.
toolsOptional[List[Tool]]NoneA list of tools the model can use for tool calling.

Run Method Parameters

These are the parameters you can configure for the component's run() method. This means you can pass these parameters at query time through the API, in Playground, or when running a job. For details, see Modify Pipeline Parameters at Query Time.

ParameterTypeDefaultDescription
messagesList[ChatMessage]A list of chat messages representing the conversation.
generation_kwargsOptional[Dict[str, Any]]NoneGeneration parameters to override init-time values.
streaming_callbackOptional[Callable]NoneA callback function to override the init-time streaming callback.
toolsOptional[List[Tool]]NoneTools to make available to the model.