DSPy

Trace your DSPy applications by patching LiteLLM and configuring the Braintrust callback handler.

DSPy is a declarative framework for programming language models developed at Stanford NLP that enables you to iterate on structured code rather than brittle strings.

Installation

uv add braintrust dspy

Usage

DSPy uses LiteLLM internally, so you need to patch LiteLLM before importing DSPy:

# Patch LiteLLM before importing DSPy
from braintrust.wrappers.litellm import patch_litellm
 
patch_litellm()
 
# Now import DSPy
import dspy
from braintrust import init_logger
from braintrust.wrappers.dspy import BraintrustDSpyCallback
 
# Initialize Braintrust
logger = init_logger(project="dspy-example")
 
# Configure DSPy with Braintrust callback
lm = dspy.LM("openai/gpt-4o-mini")
dspy.configure(lm=lm, callbacks=[BraintrustDSpyCallback()])
 
# Use DSPy as normal - all execution will be automatically traced
cot = dspy.ChainOfThought("question -> answer")
result = cot(question="What is the capital of France?")

This will automatically log:

  • DSPy module executions (Predict, ChainOfThought, ReAct, etc.)
  • LLM calls with detailed token counts and costs (from LiteLLM)
  • Tool invocations
  • Hierarchical span relationships
  • Complete observability of your DSPy pipelines

Learn More

On this page

DSPy - Docs - Braintrust