Concept Intermediate · 3 min read

What is DSPy ReAct module

Quick answer
The DSPy ReAct module is a component of the dspy library that enables building AI agents combining reasoning and action execution in a declarative manner. It implements the ReAct (Reasoning and Acting) paradigm, allowing models to interleave thought and action steps for complex task solving.
DSPy ReAct module is a declarative AI programming framework that integrates reasoning and action execution to build interactive AI agents.

How it works

The DSPy ReAct module implements the ReAct (Reasoning and Acting) paradigm, where an AI agent alternates between reasoning steps (thoughts) and action steps (interactions with tools or APIs). This approach mimics human problem-solving by allowing the model to think, decide, and act iteratively. In dspy, you define signatures for reasoning and action, then chain them declaratively. The module manages the flow, invoking the language model for reasoning and triggering external actions as needed.

Think of it as a conversation where the AI thinks aloud, decides to perform an action (like a web search or calculator), observes the result, and continues reasoning until the task is complete.

Concrete example

Here is a minimal example using dspy ReAct to create an agent that reasons and acts by calling a calculator tool:

python
import os
import dspy
from dspy import InputField, OutputField

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

# Define reasoning signature
class Reasoning(dspy.Signature):
    question: str = InputField()
    thought: str = OutputField()

# Define action signature
class Calculator(dspy.Signature):
    expression: str = InputField()
    result: str = OutputField()

# Create ReAct chain
react = dspy.ReAct(Reasoning, Calculator)

# Run the agent
response = react(question="What is 12 * 15?")
print("Thought:", response.thought)
print("Result:", response.result)
output
Thought: To find the product of 12 and 15, I will calculate 12 * 15.
Result: 180

When to use it

Use the DSPy ReAct module when you need AI agents that require iterative reasoning combined with external actions, such as tool use, API calls, or multi-step problem solving. It is ideal for tasks like question answering with calculators, web search, or database queries where the model must think, act, and observe results repeatedly.

Do not use it for simple one-step completions or when no external actions are needed, as the overhead of ReAct is unnecessary.

Key terms

TermDefinition
ReActReasoning and Acting paradigm combining thought and action steps.
SignatureA declarative definition of inputs and outputs for reasoning or action in DSPy.
ReasoningThe AI model's internal thought process step.
ActionAn external operation or tool invocation triggered by the AI agent.
ChainA sequence of reasoning and action steps managed by DSPy ReAct.

Key Takeaways

  • DSPy ReAct enables building AI agents that alternate between reasoning and actions declaratively.
  • It is best suited for complex tasks requiring iterative tool use or multi-step problem solving.
  • Define reasoning and action signatures to structure your AI agent's workflow clearly.
Verified 2026-04 · gpt-4o-mini
Verify ↗