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

DSPySignatureChatGenerator

Generate structured chat responses using DSPy signatures and modules inside a Haystack pipeline.

Key Features​

  • Uses DSPy signatures to define structured input/output contracts for generation.
  • Supports three DSPy module types: Predict, ChainOfThought, and ReAct.
  • Accepts and returns messages in ChatMessage format, making it compatible with standard Haystack chat pipelines.
  • Supports any LiteLLM-compatible model via the model parameter (for example, openai/gpt-5-mini, anthropic/claude-sonnet-4-5).
  • Supports custom input_mapping to connect signature fields to pipeline inputs from upstream components.
  • API keys are read automatically from environment variables by DSPy and LiteLLM (for example, OPENAI_API_KEY).

Configuration​

  1. Drag the DSPySignatureChatGenerator 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 LiteLLM model identifier you want to use (for example, openai/gpt-5-mini).
    2. Set the signature to define your input/output structure (for example, "question -> answer" or a fully qualified class name).
    3. Set the module_type to Predict, ChainOfThought, or ReAct.
    4. Make sure the API key for the model provider is set as an environment variable (for example, OPENAI_API_KEY). For instructions, see Create Secrets.
  4. Go to the Advanced tab to configure generation_kwargs, input_mapping, and pipeline_inputs.

Connections​

DSPySignatureChatGenerator receives a list of ChatMessage objects. It extracts the last user message and passes its text to the DSPy module. 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​

DSPySignatureChatGenerator:
type: haystack_integrations.components.generators.dspy.chat.chat_generator.DSPySignatureChatGenerator
init_parameters:
signature: "question -> answer"
model: openai/gpt-5-mini
module_type: ChainOfThought
output_field: answer
generation_kwargs:
max_tokens: 1024

Using the Component in a 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.dspy.chat.chat_generator.DSPySignatureChatGenerator
init_parameters:
signature: "question -> answer"
model: openai/gpt-5-mini
module_type: ChainOfThought
output_field: answer

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. The last user message is used as the primary input to the DSPy module.

Outputs​

ParameterTypeDescription
repliesList[ChatMessage]A list containing one reply ChatMessage with the value of the signature's output field.

Init Parameters​

These are the parameters you can configure in Pipeline Builder:

ParameterTypeDefaultDescription
signaturestr | type[dspy.Signature]The DSPy signature defining the input/output structure. Use a string like "question -> answer" or pass a fully qualified dspy.Signature subclass name.
modelstropenai/gpt-5-miniLiteLLM model identifier (for example, openai/gpt-5-mini, anthropic/claude-sonnet-4-5).
api_baseOptional[str]NoneOptional base URL for the API. Useful for local or self-hosted models.
module_typestrChainOfThoughtThe DSPy module type: Predict, ChainOfThought, or ReAct.
output_fieldstranswerThe signature output field whose value is used as the reply text.
generation_kwargsOptional[Dict[str, Any]]NoneAdditional generation parameters (for example, temperature, max_tokens).
module_kwargsOptional[Dict[str, Any]]NoneAdditional keyword arguments passed to the DSPy module constructor.
input_mappingOptional[Dict[str, str]]NoneMaps signature input field names to run() kwarg names from upstream components.
pipeline_inputsOptional[List[str]]NoneSignature input fields exposed as Haystack pipeline input sockets so upstream components can connect to them.

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. The last user message is used as input.
generation_kwargsOptional[Dict[str, Any]]NoneRuntime generation parameters that override init-time values.