Skip to main content

DSPy

Auto-instrument DSPy for declarative prompting.

Risicare automatically instruments DSPy for declarative prompt programming.

Python only

This framework integration is available in the Python SDK only. No JavaScript package exists for DSPy.

Installation

pip install 'risicare[dspy]'
# or
pip install risicare dspy

Version 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

FeatureDescription
Module CallsPredict, ChainOfThought, ReAct, etc.
SignaturesInput/output field definitions
DemonstrationsFew-shot examples
LLM CallsUnderlying provider calls (deduplicated)
OptimizersCompilation and optimization runs

Spans

SpanKindWhat 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_CALLOne model call, with the token counts
dspy.tool/{name}TOOL_CALLOne 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 captured

ReAct

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} span

ProgramOfThought

pot = dspy.ProgramOfThought(MathProblem)
result = pot(problem="What is 2+2?")
# Generated code is captured

Compilers/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.

Next Steps