DSPy
Auto-instrument DSPy for declarative prompting.
Risicare automatically instruments DSPy for declarative prompt programming.
Python only
Installation
pip install 'risicare[dspy]'
# or
pip install risicare dspyVersion Compatibility
Requires dspy >= 2.5.0.
One call of a program is one trace
Since Python 0.16.0, one call of a DSPy program is one trace. Each module, LM and tool span is the parent of the spans that its call makes. So the LM calls and the tool calls of a module are its children. A span that your code starts inside a tool or a module is a child of that span. A call that you make inside a span of yours is a child of that span. This holds in the threads of dspy.Parallel too (DSPy 3.4.1 and 3.5 were checked).
Before 0.16.0, no DSPy span had a DSPy parent. With dspy 3.x, one dspy.ReAct run made up to 10 traces. The trace counts of DSPy projects go down. A span that your code or a provider starts inside a DSPy call is now a child, not a root. With sample_rate below 1, it follows its parent, so more of those spans are sent.
Auto-Instrumentation
import risicare
import dspy
risicare.init()
lm = dspy.LM("openai/gpt-4o")
dspy.configure(lm=lm)
class BasicQA(dspy.Signature):
question = dspy.InputField()
answer = dspy.OutputField()
qa = dspy.Predict(BasicQA)
# Automatically traced
result = qa(question="What is DSPy?")What's Captured
| Feature | Description |
|---|---|
| Module Calls | Predict, ChainOfThought, ReAct, etc. |
| Signatures | Input/output field definitions |
| Demonstrations | Few-shot examples |
| LLM Calls | Underlying provider calls (deduplicated) |
| Optimizers | Compilation and optimization runs |
Spans
| Span | Kind | What it is |
|---|---|---|
dspy.module/{type} | INTERNAL. AGENT for a ReAct module (since 0.16.0) | One call of a module, for example Predict or ReAct |
dspy.lm/{model} | LLM_CALL | One model call, with the token counts |
dspy.tool/{name} | TOOL_CALL | One tool call of ReAct, with tool.input and tool.output when content capture is on (since 0.16.0). In dspy 3.5, ReAct also calls a tool named submit |
The spans of a call are nested: the module span is the parent of the LM spans and the tool spans of its call. The dspy.module/ReAct span has the status ok when a tool of the run failed (measured with 0.15.0): only the dspy.tool/{name} span is error.
The agent name. A ReAct module (or a subclass of it) is the agent ReAct, named by its class, when no agent is current (since 0.16.0). The DSPy spans under it carry the agent. Another DSPy module has no agent name: put the call in @risicare.agent(name=...) or agent_context(...). A span of your own code inside a DSPy tool does not carry the agent.
Provider Deduplication
When using DSPy, underlying LLM provider spans are automatically suppressed to avoid duplicate traces. You don't need to disable provider instrumentation manually.
Modules
All DSPy modules are traced:
Predict
predict = dspy.Predict(BasicQA)
result = predict(question="What is AI?")ChainOfThought
cot = dspy.ChainOfThought(BasicQA)
result = cot(question="Explain quantum computing")
# reasoning field is capturedReAct
def search(query: str) -> str:
return f"Results for {query}"
react = dspy.ReAct(BasicQA, tools=[search])
result = react(question="Search for AI news")
# Each tool call is a dspy.tool/{name} spanProgramOfThought
pot = dspy.ProgramOfThought(MathProblem)
result = pot(problem="What is 2+2?")
# Generated code is capturedCompilers/Optimizers
Optimization runs are traced:
from dspy.teleprompt import BootstrapFewShot
optimizer = BootstrapFewShot(metric=my_metric)
compiled = optimizer.compile(qa, trainset=trainset)Note
Optimizers and Assertions do not create distinct spans. Their activity is captured within the parent module span.