DSPy
Serve a DSPy program as an AgentOS agent.
DSPyAgent wraps any DSPy Module so it can be served through AgentOS or used standalone.
Save the following as dspy_agent.py. After the Install step, start it with python dspy_agent.py. Later snippets are alternatives or fragments using the same imports.
import dspy
from agno.agents.dspy import DSPyAgent
from agno.db.sqlite import SqliteDb
from agno.os import AgentOS
dspy.configure(lm=dspy.LM("openai/gpt-5.4"))
agent = DSPyAgent(
name="DSPy Assistant",
program=dspy.ChainOfThought("question -> answer"),
)
agent_os = AgentOS(
agents=[agent],
tracing=True,
db=SqliteDb(db_file="tmp/agentos.db"),
)
app = agent_os.get_app()
if __name__ == "__main__":
agent_os.serve(app="dspy_agent:app", reload=True)Install
uv pip install "agno[os]" dspy
export OPENAI_API_KEY=sk-...Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | str | None | Display name for the agent. |
id | str | None | Unique identifier. Auto-generated from name if unset. |
program | dspy.Module | None | A DSPy module (Predict, ChainOfThought, ReAct, or custom). |
input_field | str | "question" | Name of the input field in the DSPy signature. |
output_field | str | "answer" | Name of the output field on the Prediction. |
lm | dspy.LM | None | Optional LM to scope this agent. Falls back to the global dspy.configure(lm=...). |
program_kwargs | Dict[str, Any] | {} | Extra kwargs passed to the program on every call. |
db | BaseDb | None | Database for session persistence. |
Examples
AgentOS deployment
Serve a DSPy program through AgentOS.
Standalone usage
Call the agent directly with .run() and .print_response().
ReAct with tools
Use dspy.ReAct to call tools from the program.
Sessions
Resume conversations across runs with session_id.