How to chain DSPy modules
Quick answer
Use
dspy.ChainOfThought or compose multiple dspy.Predict modules by passing outputs from one as inputs to another. This chaining enables modular, stepwise AI workflows with declarative signatures in dspy.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install dspy openai>=1.0
Setup
Install dspy and openai packages and set your OpenAI API key as an environment variable.
- Install packages:
pip install dspy openai - Set environment variable:
export OPENAI_API_KEY='your_api_key'(Linux/macOS) orsetx OPENAI_API_KEY "your_api_key"(Windows)
pip install dspy openai Step by step
Define multiple dspy.Signature classes representing individual tasks, then create dspy.Predict instances for each. Chain them by passing the output of one module as input to the next. Use dspy.ChainOfThought to enable stepwise reasoning chaining.
import os
import dspy
from dspy import InputField, OutputField
# Configure DSPy with OpenAI LLM
lm = dspy.LM("openai/gpt-4o-mini", api_key=os.environ["OPENAI_API_KEY"])
dspy.configure(lm=lm)
# Define first module signature
class ExtractName(dspy.Signature):
text: str = InputField()
name: str = OutputField()
# Define second module signature
class GreetUser(dspy.Signature):
name: str = InputField()
greeting: str = OutputField()
# Create Predict instances
extract_name = dspy.Predict(ExtractName)
greet_user = dspy.Predict(GreetUser)
# Input text
input_text = "My name is Alice."
# Run first module
extracted = extract_name(text=input_text)
print(f"Extracted name: {extracted.name}")
# Chain output to second module
greeting = greet_user(name=extracted.name)
print(f"Greeting: {greeting.greeting}") output
Extracted name: Alice Greeting: Hello, Alice! Nice to meet you.
Common variations
You can use dspy.ChainOfThought to chain modules with intermediate reasoning steps automatically. Also, you can switch models by changing the dspy.LM initialization, or use async calls with dspy if supported.
import os
import dspy
lm = dspy.LM("openai/gpt-4o-mini", api_key=os.environ["OPENAI_API_KEY"])
dspy.configure(lm=lm)
class QA(dspy.Signature):
question: str = dspy.InputField()
answer: str = dspy.OutputField()
qa_chain = dspy.ChainOfThought(QA)
result = qa_chain(question="What is DSPy?")
print(result.answer) output
DSPy is a declarative Python framework for building modular AI workflows by defining signatures and chaining predictions.
Troubleshooting
- If you see
API key missingerrors, ensureOPENAI_API_KEYis set in your environment. - If chaining outputs fail, verify that output field names match input field names between modules.
- For model errors, confirm you use a current model like
gpt-4o-miniand have network access.
Key Takeaways
- Use
dspy.Predictto create modular AI components with declarative input/output signatures. - Chain modules by passing outputs from one as inputs to another for clean, composable workflows.
- Leverage
dspy.ChainOfThoughtfor automatic stepwise reasoning chains. - Always configure
dspywith a validdspy.LMinstance using your OpenAI API key. - Match input and output field names carefully to avoid chaining errors.