Get the FREE Ultimate OpenClaw Setup Guide →

dspy-react-agent-builder

Use Caution
npx machina-cli add skill OmidZamani/dspy-skills/dspy-react-agent-builder --openclaw
Files (1)
SKILL.md
6.6 KB

DSPy ReAct Agent Builder

Goal

Build production-quality ReAct agents that use tools to solve complex multi-step tasks with reasoning, acting, and error handling.

When to Use

  • Multi-step tasks requiring tool use
  • Search + reasoning workflows
  • Complex question answering with external data
  • Tasks needing calculation, retrieval, or API calls

Related Skills

Inputs

InputTypeDescription
signaturestrTask signature (e.g., "question -> answer")
toolslist[callable]Available tools/functions
max_itersintMax reasoning steps (default: 20)

Outputs

OutputTypeDescription
agentdspy.ReActConfigured ReAct agent

Workflow

Phase 1: Define Tools

Tools are Python functions with clear docstrings. The agent uses docstrings to understand tool capabilities:

import dspy

def search(query: str) -> list[str]:
    """Search knowledge base for relevant information.

    Args:
        query: Search query string

    Returns:
        List of relevant text passages
    """
    retriever = dspy.ColBERTv2(url='http://20.102.90.50:2017/wiki17_abstracts')
    results = retriever(query, k=3)
    return [r['text'] for r in results]

def calculate(expression: str) -> float:
    """Safely evaluate mathematical expressions.

    Args:
        expression: Math expression (e.g., "2 + 2", "sqrt(16)")

    Returns:
        Numerical result
    """
    try:
        interpreter = dspy.PythonInterpreter()
        return interpreter.execute(expression)
    except Exception as e:
        return f"Error: {e}"

Phase 2: Create ReAct Agent

# Configure LM
dspy.configure(lm=dspy.LM("openai/gpt-4o-mini"))

# Create agent
agent = dspy.ReAct(
    signature="question -> answer",
    tools=[search, calculate],
    max_iters=5
)

# Use agent
result = agent(question="What is the population of Paris plus 1000?")
print(result.answer)

Phase 3: Production Agent with Error Handling

import dspy
import logging

logger = logging.getLogger(__name__)

class ResearchAgent(dspy.Module):
    """Production agent with error handling and logging."""

    def __init__(self, max_iters: int = 5):
        self.max_iters = max_iters
        self.agent = dspy.ReAct(
            signature="question -> answer",
            tools=[self.search, self.calculate, self.summarize],
            max_iters=max_iters
        )

    def search(self, query: str) -> list[str]:
        """Search for relevant documents."""
        try:
            retriever = dspy.ColBERTv2(
                url='http://20.102.90.50:2017/wiki17_abstracts'
            )
            results = retriever(query, k=5)
            return [r['text'] for r in results]
        except Exception as e:
            logger.error(f"Search failed: {e}")
            return [f"Search unavailable: {e}"]

    def calculate(self, expression: str) -> str:
        """Evaluate mathematical expressions safely."""
        try:
            interpreter = dspy.PythonInterpreter()
            result = interpreter.execute(expression)
            return str(result)
        except Exception as e:
            logger.error(f"Calculation failed: {e}")
            return f"Error: {e}"

    def summarize(self, text: str) -> str:
        """Summarize long text into key points."""
        try:
            summarizer = dspy.Predict("text -> summary: str")
            return summarizer(text=text[:1000]).summary
        except Exception as e:
            logger.error(f"Summarization failed: {e}")
            return "Summarization unavailable"

    def forward(self, question: str) -> dspy.Prediction:
        """Execute agent with error handling."""
        try:
            return self.agent(question=question)
        except Exception as e:
            logger.error(f"Agent failed: {e}")
            return dspy.Prediction(answer=f"Error: {e}")

# Usage
agent = ResearchAgent(max_iters=6)
response = agent(question="What is the capital of France and its population?")
print(response.answer)

Phase 4: Optimize with GEPA

ReAct agents benefit from reflective optimization:

from dspy.evaluate import Evaluate

def feedback_metric(example, pred, trace=None, pred_name=None, pred_trace=None):
    """Provide textual feedback for GEPA."""
    is_correct = example.answer.lower() in pred.answer.lower()
    score = 1.0 if is_correct else 0.0
    feedback = "Correct." if is_correct else f"Expected '{example.answer}'. Check tool selection."
    return dspy.Prediction(score=score, feedback=feedback)

# Optimize agent
optimizer = dspy.GEPA(
    metric=feedback_metric,
    reflection_lm=dspy.LM("openai/gpt-4o"),
    auto="medium",
    enable_tool_optimization=True  # Also optimize tool docstrings
)

compiled = optimizer.compile(agent, trainset=trainset)
compiled.save("research_agent_optimized.json", save_program=False)

Best Practices

  1. Clear tool docstrings - Agent relies on docstrings to understand tool capabilities
  2. Error handling - All tools should handle failures gracefully and return error messages
  3. Tool independence - Test each tool separately before adding to agent
  4. Logging - Track tool calls and agent reasoning for debugging
  5. Limit iterations - Set reasonable max_iters to prevent infinite loops (default is 20, but 5-10 often sufficient for simpler tasks)

Limitations

  • ReAct works best with 3-7 tools; too many tools confuse the agent
  • Not all LMs support tool calling equally well (GPT-4 > GPT-3.5)
  • Agent may call tools unnecessarily or miss necessary calls
  • Requires GEPA optimization for production quality
  • Tool execution is sequential, not parallelized

Official Documentation

Source

git clone https://github.com/OmidZamani/dspy-skills/blob/master/skills/dspy-react-agent-builder/SKILL.mdView on GitHub

Overview

This skill helps you build production-quality ReAct agents that use tools to solve complex tasks with reasoning and action. It emphasizes multi-step workflows, error handling, and tool orchestration to produce reliable results.

How This Skill Works

First, define tools as Python functions with clear docstrings that describe capabilities. Next, configure the LM and instantiate a dspy.ReAct with a task signature, the available tools, and a max_iters limit. Finally, wrap the agent in production patterns with error handling and logging to improve reliability.

When to Use It

  • Multi-step tasks requiring tool use
  • Search + reasoning workflows
  • Complex question answering with external data
  • Calculations, retrieval, or API calls
  • Productions agents that use tools to solve complex tasks

Quick Start

  1. Step 1: Configure LM: dspy.configure(lm=dspy.LM("openai/gpt-4o-mini"))
  2. Step 2: Create ReAct agent with tools and signature: agent = dspy.ReAct(signature="question -> answer", tools=[search, calculate], max_iters=5)
  3. Step 3: Run the agent: result = agent(question="What is the population of Paris plus 1000?"); print(result.answer)

Best Practices

  • Provide tools with clear docstrings so the agent can reason about capabilities
  • Set a sensible max_iters to balance depth and latency
  • Gracefully handle tool failures and surface meaningful errors
  • Add robust logging and monitoring around tool calls
  • Test end-to-end scenarios including edge cases and failure modes

Example Use Cases

  • Answer questions by searching a knowledge base with a tool like ColBERTv2 and computing results
  • A production research agent using search, calculate, and summarize tools with error handling
  • Customer support bot that invokes APIs to retrieve data and respond with reasoning
  • Financial calculator agent that fetches data and computes metrics
  • Educational assistant that chains search and calculation tools to solve problems

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers