Wraps Python functions as dspy.Tool objects and configures dspy.PythonInterpreter for sandboxed code execution in DSPy agents.
Ships in dspy-api-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Guide the user through wrapping functions as DSPy tools, using dspy.PythonInterpreter for sandboxed code execution, and wiring tools into agents with dspy.ReAct and dspy.CodeAct.
Ask the user before diving in:
PythonInterpreter. Otherwise, plain tool functions are simpler.Then jump to the relevant section below.
| Approach | Best for | Trade-off |
|---|---|---|
Function passed directly to dspy.ReAct | Most tool use — search, lookup, API calls | Simplest; name and description come from the function itself |
dspy.Tool explicit wrap | Override name/desc/schema; convert LangChain or MCP tools | Same runtime cost, more control over what the LM sees |
dspy.PythonInterpreter + dspy.CodeAct | Arbitrary computation, multi-step data manipulation | Requires Deno; more powerful but harder to debug |
dspy.Tool wraps a Python function so DSPy agents can call it. It automatically extracts the function's name, docstring, parameter types, and descriptions to build the tool schema that the LM sees.
You can pass plain functions directly to dspy.ReAct or dspy.CodeAct and DSPy wraps them for you. Use dspy.Tool explicitly when you need to override the inferred metadata, convert tools from LangChain or MCP, or inspect the generated schema.
import dspy
# Implicit -- pass a function directly (DSPy wraps it automatically)
agent = dspy.ReAct("question -> answer", tools=[my_search_function])
# Explicit -- wrap it yourself for control over name, description, etc.
tool = dspy.Tool(my_search_function, name="search", desc="Search the knowledge base")
agent = dspy.ReAct("question -> answer", tools=[tool])
dspy.Tool(
func, # Callable -- the function to wrap
name=None, # str | None -- tool name (inferred from func.__name__ if omitted)
desc=None, # str | None -- description (inferred from docstring if omitted)
args=None, # dict | None -- argument JSON schemas (inferred from type hints)
arg_types=None, # dict | None -- argument type mappings (inferred from type hints)
arg_desc=None, # dict | None -- per-argument descriptions
)
All parameters except func are optional. DSPy infers them from the function signature and docstring. Override them when the inferred values are wrong or when you want a different name or description.
def foo(x: int, y: str = "hello"):
"""Combine a number and a string."""
return str(x) + y
tool = dspy.Tool(foo)
print(tool.name) # "foo"
print(tool.desc) # "Combine a number and a string."
print(tool.args) # {'x': {'type': 'integer'}, 'y': {'type': 'string', 'default': 'hello'}}
The quality of your tools depends on three things: type hints, docstrings, and focused scope.
DSPy reads type hints to build the JSON schema the LM uses for tool calling. Always annotate every parameter and the return type.
# Good -- fully typed
def search(query: str, max_results: int = 5) -> str:
"""Search the knowledge base for documents matching the query."""
...
# Bad -- no type hints, agent won't know what to pass
def search(query, max_results=5):
"""Search the knowledge base."""
...
The docstring becomes the tool description. Write it from the perspective of someone deciding whether to call this tool.
# Good -- explains what the tool does and when to use it
def lookup_user(email: str) -> str:
"""Look up a user account by email address. Returns name, plan, and join date."""
...
# Bad -- vague, doesn't help the agent decide
def lookup_user(email: str) -> str:
"""Get user info."""
...
Keep tools focused. A tool that searches and summarizes is harder for the agent to use than two separate tools.
# Good -- single responsibility
def search(query: str) -> str:
"""Search for documents matching the query."""
...
def summarize(text: str) -> str:
"""Summarize a long piece of text into key points."""
...
# Bad -- does two things
def search_and_summarize(query: str) -> str:
"""Search for documents and summarize the results."""
...
Tool return values become the Observation the agent sees. Return a string (or something that converts to string cleanly).
import json
def check_order(order_id: str) -> str:
"""Check the status of an order by its ID."""
order = db.get_order(order_id)
if order:
return json.dumps(order)
return f"No order found with ID {order_id}."
For complex tools, add per-argument descriptions using arg_desc:
tool = dspy.Tool(
search,
arg_desc={
"query": "The search query -- use keywords, not full sentences",
"max_results": "Maximum number of results to return (1-20)",
},
)
dspy.PythonInterpreter runs Python code in a sandboxed Deno + Pyodide environment. By default, the sandbox has no filesystem, network, or environment access. You selectively enable what you need.
dspy.PythonInterpreter(
deno_command=None, # list[str] | None -- custom Deno launch command
enable_read_paths=None, # list[PathLike | str] | None -- paths the sandbox can read
enable_write_paths=None, # list[PathLike | str] | None -- paths the sandbox can write
enable_env_vars=None, # list[str] | None -- environment variables to expose
enable_network_access=None, # list[str] | None -- allowed network domains
sync_files=True, # bool -- sync file changes back to host
tools=None, # dict[str, Callable[..., str]] | None -- host-side tool functions
output_fields=None, # list[dict] | None -- output field definitions
)
Prerequisites: Deno must be installed. See https://docs.deno.com/runtime/getting_started/installation/
from dspy import PythonInterpreter
with PythonInterpreter() as interp:
result = interp("print(1 + 2)") # Returns "3"
Tools passed to PythonInterpreter run in your normal Python process (not the sandbox). The sandbox calls them via JSON-RPC. This lets tools access databases, APIs, and libraries that aren't available inside the sandbox.
def fetch_price(ticker: str) -> str:
"""Fetch the current stock price for a ticker symbol."""
import requests
resp = requests.get(f"https://api.example.com/price/{ticker}")
return resp.json()["price"]
with PythonInterpreter(tools={"fetch_price": fetch_price}) as interp:
result = interp("price = fetch_price(ticker='AAPL')\nprint(f'Price: {price}')")
# Allow reading from a data directory and accessing one API
interp = PythonInterpreter(
enable_read_paths=["./data"],
enable_network_access=["api.example.com"],
)
dspy.CodeAct creates a PythonInterpreter automatically if you don't pass one. Pass your own when you need custom permissions:
import dspy
interp = dspy.PythonInterpreter(
enable_read_paths=["./data"],
enable_network_access=["api.example.com"],
)
agent = dspy.CodeAct(
"question -> answer",
tools=[search, calculate],
interpreter=interp,
max_iters=5,
)
Upcoming in DSPy 3.3.0 (beta):
dspy.ToolCallsanddspy.ToolCallResultsare structured types for native tool-calling workflows added in 3.3.0 beta. Not yet available in the stable 3.2.1 release. See dspy.ToolCalls API docs when you upgrade.
dspy.ReAct is the standard choice for tool-using agents. Pass tools as a list of functions or dspy.Tool objects:
import dspy
def search(query: str) -> str:
"""Search for information about a topic."""
return "DSPy is a framework for programming language models."
def calculate(expression: str) -> float:
"""Evaluate a math expression and return the result."""
return eval(expression)
agent = dspy.ReAct(
"question -> answer",
tools=[search, calculate],
max_iters=5,
)
result = agent(question="What is 2^10 plus the year DSPy was released?")
print(result.answer)
The agent decides which tools to call, in what order, and when to stop. See /dspy-react for the full guide.
dspy.CodeAct agents write Python code that calls your tools. Tools must be pure functions (not callable objects). The agent can chain calls, use loops, and manipulate data in code:
import dspy
def factorial(n: int) -> int:
"""Calculate the factorial of n."""
if n <= 1:
return 1
return n * factorial(n - 1)
agent = dspy.CodeAct(
"question -> answer",
tools=[factorial],
max_iters=5,
)
result = agent(question="What is factorial(10) + factorial(5)?")
print(result.answer)
CodeAct tools have stricter requirements than ReAct tools: they must be plain functions, cannot import external libraries, and cannot reference global state. See /dspy-codeact for the full guide.
dspy.Tool.from_langchain() converts any LangChain tool to a DSPy tool:
import dspy
from langchain_community.tools import DuckDuckGoSearchRun, WikipediaQueryRun
from langchain_community.utilities import WikipediaAPIWrapper
search = dspy.Tool.from_langchain(DuckDuckGoSearchRun())
wikipedia = dspy.Tool.from_langchain(
WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper())
)
agent = dspy.ReAct("question -> answer", tools=[search, wikipedia])
Install LangChain tools with pip install langchain-community.
dspy.Tool.from_mcp_tool() converts Model Context Protocol tools into DSPy tools. It preserves the tool's name, description, and input schema, and creates an async callable that invokes the tool through the MCP session.
Install the MCP extra:
pip install -U "dspy[mcp]"
import dspy
from mcp import ClientSession
from mcp.client.streamable_http import streamablehttp_client
async with streamablehttp_client("https://mcp.example.com/sse") as (read, write, _):
async with ClientSession(read, write) as session:
await session.initialize()
mcp_tools = await session.list_tools()
tools = [dspy.Tool.from_mcp_tool(session, t) for t in mcp_tools.tools]
agent = dspy.ReAct("question -> answer", tools=tools)
result = await agent.aforward(question="What files are in the repo?")
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
server_params = StdioServerParameters(
command="npx",
args=["-y", "@modelcontextprotocol/server-filesystem", "./data"],
)
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
mcp_tools = await session.list_tools()
tools = [dspy.Tool.from_mcp_tool(session, t) for t in mcp_tools.tools]
agent = dspy.ReAct("question -> answer", tools=tools)
result = await agent.aforward(question="List the files")
ClientSession yourself using the mcp library.from_mcp_tool creates an async callable, so use await agent.aforward() or run inside an async context.Good tools make good agents. Before passing a tool to an agent, check:
| Check | Why it matters |
|---|---|
| All parameters have type hints | DSPy generates the JSON schema from them |
| Return type is annotated | Helps the agent know what to expect |
| Docstring explains what the tool does | The agent reads this to decide when to call it |
| Docstring mentions required input format | e.g., "Pass repo as 'owner/name'" |
| Parameters have sensible defaults | Reduces the number of decisions the agent makes |
| Errors return useful strings, not exceptions | The agent sees the error as an Observation and can retry |
# A well-documented tool
def get_github_repo(repo: str) -> str:
"""Get information about a GitHub repository.
Pass the full repository name like 'stanfordnlp/dspy'.
Returns name, description, stars, and language.
"""
try:
response = requests.get(f"https://api.github.com/repos/{repo}", timeout=10)
response.raise_for_status()
data = response.json()
return f"Name: {data['full_name']}, Stars: {data['stargazers_count']}"
except requests.RequestException as e:
return f"Error: {str(e)}"
Tools should catch exceptions and return error strings. When a tool returns an error string, the agent sees it as an Observation and can retry with different arguments or try a different tool.
def search(query: str) -> str:
"""Search for information."""
try:
response = requests.get("https://api.example.com/search", params={"q": query}, timeout=5)
response.raise_for_status()
return response.json()["results"]
except requests.Timeout:
return "Error: Search timed out. Try a shorter or simpler query."
except requests.HTTPError as e:
return f"Error: Search failed with status {e.response.status_code}."
except Exception as e:
return f"Error: {str(e)}"
dspy.Tool only when you need to override the inferred name, description, or arg schema.from_langchain() or from_mcp_tool().repr() which is often unhelpful. Always return a formatted string or json.dumps() output.lambda q: search(q, api_key=key)). Lambdas have no docstring and no type hints, so DSPy generates an empty tool schema and the agent cannot call the tool correctly. Use functools.partial or pass desc and arg_desc explicitly to dspy.Tool instead.await agent.aforward() with MCP tools. MCP tools are async, so the agent must be called with aforward(). Claude defaults to agent() which blocks or fails silently in async contexts.Install any skill:
npx skills add lebsral/DSPy-Programming-not-prompting-LMs-skills --skill <name>
/dspy-react/dspy-codeact/ai-taking-actions/dspy-signatures/dspy-modules/ai-do if you do not have it — it routes any AI problem to the right skill and is the fastest way to work: npx skills add lebsral/DSPy-Programming-not-prompting-LMs-skills --skill ai-doWhat SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
/dspy-api-skills:dspy-toolsfrom dspy-api-skills
npx claudepluginhub lebsral/dspy-programming-not-prompting-lms-skills --plugin dspy-api-skills39 skills in this plugin
/dspy-adaptersUse when you need to customize how DSPy formats prompts for a specific provider — switching from chat to completion format, forcing JSON output, or debugging prompt rendering issues. Common scenarios - debugging why your prompt looks wrong when sent to the model, switching from OpenAI to Anthropic and the formatting breaks, forcing the model to return valid JSON instead of markdown, working with completion-style models that do not support chat format, customizing system messages, or handling models that choke on structured output instructions. Related - ai-switching-models, ai-following-rules, ai-parsing-data. Also used for prompt template rendering, how DSPy builds the prompt, custom system message in DSPy, JSON mode not working, model ignores format instructions, switch from chat to completion API, dspy.ChatAdapter, dspy.JSONAdapter, prompt formatting issues, debug what DSPy sends to the model, dspy.XMLAdapter, XML output format./dspy-assertionsLegacy reference for dspy.Assert and dspy.Suggest, both removed in DSPy 3.x - use dspy.Refine or dspy.BestOfN instead. Use when maintaining a legacy DSPy 2.x codebase that uses dspy.Assert or dspy.Suggest, or migrating from Assert/Suggest to DSPy 3.x. Also used for dspy.Assert, dspy.Suggest, runtime validation for LLM output, retry on bad output, backtracking on constraint violation, guard rails in DSPy, migrate from Assert, migrate from Suggest, ImportError cannot import Assert, ImportError cannot import Suggest.Other plugins' skills for the same job