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, andReAct. - Accepts and returns messages in
ChatMessageformat, making it compatible with standard Haystack chat pipelines. - Supports any LiteLLM-compatible model via the
modelparameter (for example,openai/gpt-5-mini,anthropic/claude-sonnet-4-5). - Supports custom
input_mappingto 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
- Drag the
DSPySignatureChatGeneratorcomponent onto the canvas from the Component Library. - Click on the component to open the configuration panel.
- On the General tab:
- Set the
modelto the LiteLLM model identifier you want to use (for example,openai/gpt-5-mini). - Set the
signatureto define your input/output structure (for example,"question -> answer"or a fully qualified class name). - Set the
module_typetoPredict,ChainOfThought, orReAct. - 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.
- Set the
- Go to the Advanced tab to configure
generation_kwargs,input_mapping, andpipeline_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
| Parameter | Type | Description |
|---|---|---|
messages | List[ChatMessage] | A list of chat messages. The last user message is used as the primary input to the DSPy module. |
Outputs
| Parameter | Type | Description |
|---|---|---|
replies | List[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:
| Parameter | Type | Default | Description |
|---|---|---|---|
signature | str | 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. | |
model | str | openai/gpt-5-mini | LiteLLM model identifier (for example, openai/gpt-5-mini, anthropic/claude-sonnet-4-5). |
api_base | Optional[str] | None | Optional base URL for the API. Useful for local or self-hosted models. |
module_type | str | ChainOfThought | The DSPy module type: Predict, ChainOfThought, or ReAct. |
output_field | str | answer | The signature output field whose value is used as the reply text. |
generation_kwargs | Optional[Dict[str, Any]] | None | Additional generation parameters (for example, temperature, max_tokens). |
module_kwargs | Optional[Dict[str, Any]] | None | Additional keyword arguments passed to the DSPy module constructor. |
input_mapping | Optional[Dict[str, str]] | None | Maps signature input field names to run() kwarg names from upstream components. |
pipeline_inputs | Optional[List[str]] | None | Signature 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.
| Parameter | Type | Default | Description |
|---|---|---|---|
messages | List[ChatMessage] | A list of chat messages. The last user message is used as input. | |
generation_kwargs | Optional[Dict[str, Any]] | None | Runtime generation parameters that override init-time values. |
Related Information
Was this page helpful?