DSPy
See every step your DSPy program takes: each module call, the prompts it builds, the model’s responses, how many tokens it used, and any errors, as a trace (the full journey of one request, made of nested spans, where each span is one unit of work with a name, a start, and a duration) in Logfire. DSPy composes prompts and model calls into programs, and this integration shows each nested step in one trace.
- Each DSPy module call as a span, with its duration and any exceptions
- The prompts DSPy builds and the model’s responses
- Nested steps of your program, shown as child spans in the trace
- Response details, including the number of tokens used
You’ll need a Logfire project. Open Add data in your project (top navigation) and follow the
setup for your language: it signs your machine in with logfire auth (a browser sign-in, no token
to copy) and, for production or other languages, creates a write token (the credential your app
uses to send data). New to Logfire? Start with Getting Started.
You’ll also need an API key for whichever model provider DSPy uses (for example, OPENAI_API_KEY for the openai/... model below). DSPy reads it from the provider’s environment variable.
Install logfire with the dspy extra and the DSPy package:
pip install 'logfire[dspy]'
uv add 'logfire[dspy]'
pip install dspy
Add two lines to your app: logfire.configure() to connect to your project, and
logfire.instrument_dspy() to record every DSPy step.
import dspy
import logfire
logfire.configure()
logfire.instrument_dspy()
lm = dspy.LM('openai/gpt-5-mini')
dspy.configure(lm=lm)
class ExtractInfo(dspy.Signature):
"""Extract structured information from text."""
text: str = dspy.InputField()
title: str = dspy.OutputField()
headings: list[str] = dspy.OutputField()
entities: list[dict[str, str]] = dspy.OutputField(desc='a list of entities and their metadata')
module = dspy.Predict(ExtractInfo)
text = (
'Apple Inc. announced its latest iPhone 14 today. '
'The CEO, Tim Cook, highlighted its new features in a press release.'
)
response = module(text=text)
print(response.title)
print(response.headings)
print(response.entities)
Run your program, then open your project in the Logfire web app and go to the Live view. Within a few seconds you should see a trace for the DSPy run, with a span for each module call. Click into it to see the prompts, responses, and token counts.
Not seeing your DSPy steps in Logfire? Check these first:
logfire.configure()runs beforelogfire.instrument_dspy(). Configure the connection first, then instrument.- You called
instrument_dspy()exactly once. - Your Logfire write token is set. In local development, run
logfire projects use <your-project>; in production, set theLOGFIRE_TOKENenvironment variable. See Getting Started.
- API reference:
logfire.instrument_dspy() - Underlying OpenTelemetry package:
openinference-instrumentation-dspy