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

OllamaChatGenerator

Generate chat responses using models served with Ollama.

Key Features

  • Works with any model available through Ollama, including Llama, Mistral, Qwen, and Gemma.
  • Accepts and returns messages in ChatMessage format.
  • Supports streaming responses through a configurable callback.
  • Supports tool calling for agentic workflows.
  • Supports structured output with JSON schema validation.
  • Supports reasoning with extended thinking (think parameter).

Configuration

  1. Drag the OllamaChatGenerator 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 Ollama model you want to use (for example, llama3.2).
    2. Set the url to the address of your running Ollama server. The default is http://localhost:11434.
  4. Go to the Advanced tab to configure generation_kwargs, timeout, keep_alive, and response_format.

Connections

OllamaChatGenerator 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

  OllamaChatGenerator:
type: haystack_integrations.components.generators.ollama.chat.chat_generator.OllamaChatGenerator
init_parameters:
model: llama3.2
url: http://localhost:11434
generation_kwargs:
temperature: 0.7
timeout: 120

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.ollama.chat.chat_generator.OllamaChatGenerator
init_parameters:
model: llama3.2
url: http://localhost:11434
timeout: 120

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
modelstrqwen3:0.6bThe name of the Ollama model to use. Run ollama list to see available models.
urlstrhttp://localhost:11434The URL of the Ollama API server.
generation_kwargsOptional[Dict[str, Any]]NoneAdditional parameters for text generation, such as temperature, top_k, top_p, and num_predict.
timeoutint120Request timeout in seconds.
max_retriesint0Maximum number of retries on failed requests.
keep_aliveOptional[Union[float, str]]NoneControls how long the model stays loaded in memory after the last request.
streaming_callbackOptional[Callable]NoneA callback function for streaming responses.
toolsOptional[List[Tool]]NoneA list of tools the model can use for tool calling.
response_formatOptional[Union[Literal["json"], Dict]]NoneForces a specific response format. Set to "json" for JSON output or provide a JSON schema.
thinkUnion[bool, Literal["low", "medium", "high"]]FalseEnables extended reasoning. Set to True or a verbosity level string for reasoning models.

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.
toolsOptional[List[Tool]]NoneTools to make available to the model, overriding init-time tools.