OpenInference DSPy Instrumentation

raw JSON →
0.1.34 verified Fri May 01 auth: no python

OpenTelemetry instrumentation for DSPy that automatically traces LLM calls, retrievers, and chain-of-thought reasoning. Current version 0.1.34, requires Python >=3.10, <3.15. Part of the Arize OpenInference ecosystem, actively maintained.

pip install openinference-instrumentation-dspy
error ModuleNotFoundError: No module named 'openinference.instrumentation.dspy'
cause The package is not installed or the import path is wrong.
fix
Install with pip install openinference-instrumentation-dspy and use the correct import: from openinference.instrumentation.dspy import DSPyInstrumentor.
error RuntimeError: DSPyInstrumentor is not instrumented.
cause The instrument() method must be called exactly once before using DSPy.
fix
Call DSPyInstrumentor().instrument() at the start of your application. Do not call it multiple times.
error AttributeError: module 'dspy' has no attribute 'LM'
cause DSPy version mismatch or import order issue. The instrumentor may have modified the module incorrectly if instrumented after import.
fix
Ensure DSPy is installed (pip install dspy) and instrument before any dspy usage: DSPyInstrumentor().instrument(); import dspy.
gotcha DSPyInstrumentor must be instrumented before any DSPy modules are imported or configured. If you instrument after importing dspy, some spans may be lost.
fix Call DSPyInstrumentor().instrument() at the very start of your application, before importing dspy or any DSPy-related modules.
gotcha Requires Python >=3.10 and <3.15. Using Python 3.9 or Python 3.15+ will result in installation failures or runtime errors.
fix Use Python 3.10, 3.11, 3.12, or 3.13. Check your Python version before installing.
deprecated The older openinference-instrumentation-dspy package had different import paths (e.g., openinference_instrumentation_dspy). Migrate to the current OpenInference naming convention.
fix Use `from openinference.instrumentation.dspy import DSPyInstrumentor`.

Installs and configures the DSPy instrumentor, then runs a simple ChainOfThought example.

import os
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from openinference.instrumentation.dspy import DSPyInstrumentor
from openinference.semconv.trace import SpanAttributes

# Set up a tracer provider with a simple span processor (replace endpoint with your collector)
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
trace_provider = TracerProvider()
trace_provider.add_span_processor(
    SimpleSpanProcessor(OTLPSpanExporter(endpoint=os.environ.get('OTEL_EXPORTER_OTLP_ENDPOINT', 'http://localhost:4318/v1/traces')))
)
trace.set_tracer_provider(trace_provider)

# Instrument DSPy
DSPyInstrumentor().instrument()

# Now use DSPy normally - all traces will be captured.
import dspy
lm = dspy.LM('openai/gpt-4o-mini')
dspy.configure(lm=lm)
coT = dspy.ChainOfThought('question -> answer')
print(coT(question="What is the capital of France?"))