How to intermediate · 3 min read

How to use DSPy for complex reasoning

Quick answer
Use dspy to define structured signatures representing reasoning tasks and chain them with dspy.ChainOfThought for complex multi-step logic. Integrate with an LLM like openai/gpt-4o-mini via dspy.LM to execute and combine reasoning steps programmatically.

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.

  • Run pip install dspy openai
  • Export your API key: export OPENAI_API_KEY='your_api_key' on Linux/macOS or set in Windows environment variables.
bash
pip install dspy openai

Step by step

Define a reasoning signature with input and output fields, create a dspy.LM client for the OpenAI model, configure dspy with the LM, and use dspy.ChainOfThought to perform multi-step reasoning.

python
import os
import dspy

# Initialize the LM with OpenAI GPT-4o-mini
lm = dspy.LM("openai/gpt-4o-mini", api_key=os.environ["OPENAI_API_KEY"])
dspy.configure(lm=lm)

# Define a signature for reasoning tasks
class ReasoningTask(dspy.Signature):
    question: str = dspy.InputField()
    answer: str = dspy.OutputField()

# Create a ChainOfThought instance for multi-step reasoning
chain = dspy.ChainOfThought(ReasoningTask)

# Run a complex reasoning example
result = chain(question="If all cats are mammals and some mammals are black, are some cats black?")
print("Answer:", result.answer)
output
Answer: Some cats may be black because some mammals are black and all cats are mammals, but it is not guaranteed.

Common variations

You can use different models by changing the model name in dspy.LM, run asynchronously with asyncio, or customize reasoning by chaining multiple signatures for stepwise logic.

python
import asyncio
import os
import dspy

async def async_reasoning():
    lm = dspy.LM("openai/gpt-4o-mini", api_key=os.environ["OPENAI_API_KEY"])
    dspy.configure(lm=lm)

    class ReasoningTask(dspy.Signature):
        question: str = dspy.InputField()
        answer: str = dspy.OutputField()

    chain = dspy.ChainOfThought(ReasoningTask)

    result = await chain.acall(question="Is the statement 'All birds can fly' true?")
    print("Async answer:", result.answer)

asyncio.run(async_reasoning())
output
Async answer: The statement 'All birds can fly' is false because some birds, like ostriches and penguins, cannot fly.

Troubleshooting

  • If you get authentication errors, verify your OPENAI_API_KEY environment variable is set correctly.
  • If responses are incomplete, increase the model's max_tokens parameter by configuring dspy.LM or the underlying client.
  • For unexpected outputs, ensure your signature fields match the expected input/output format.

Key Takeaways

  • Use dspy.Signature to define structured reasoning tasks with clear inputs and outputs.
  • Chain reasoning steps with dspy.ChainOfThought for complex multi-step logic workflows.
  • Configure dspy.LM with your OpenAI API key and preferred model for execution.
  • Support async calls with acall for non-blocking reasoning workflows.
  • Adjust model parameters and signature definitions to tailor reasoning complexity and output.
Verified 2026-04 · openai/gpt-4o-mini
Verify ↗