DSPy Instrumentation
Learn how to instrument DSPy programs with the LangWatch Python SDK
LangWatch provides seamless integration with DSPy, allowing you to automatically capture detailed information about your DSPy program executions, including module calls and language model interactions.
The primary way to instrument your DSPy programs is by using autotrack_dspy()
on the current LangWatch trace.
Using autotrack_dspy()
The autotrack_dspy()
function, when called on an active trace object, dynamically patches the DSPy framework to capture calls made during the execution of your DSPy programs within that trace.
You typically call this method on the trace object obtained via langwatch.get_current_trace()
inside a function decorated with @langwatch.trace()
. This ensures that all DSPy operations within that traced function are monitored.
Key points for autotrack_dspy()
:
- It must be called on an active trace object (e.g., obtained via
langwatch.get_current_trace()
). - It instruments DSPy operations specifically for the duration and scope of the current trace.
- LangWatch will capture interactions with DSPy modules (like
dspy.Predict
,dspy.ChainOfThought
,dspy.Retrieve
) and the underlying LM calls.
Using Community OpenTelemetry Instrumentors
If you prefer to use broader OpenTelemetry-based instrumentation, or are already using libraries like OpenInference, LangWatch can seamlessly integrate with them. These libraries provide instrumentors that automatically capture data from various LLM frameworks, including DSPy.
The OpenInference community provides an instrumentor for DSPy which can be used with LangWatch.
There are two main ways to integrate these:
1. Via langwatch.setup()
You can pass an instance of the instrumentor (e.g., OpenInferenceDSPyInstrumentor
from OpenInference) to the instrumentors
list in the langwatch.setup()
call. LangWatch will then manage the lifecycle of this instrumentor.
Ensure you have the respective community instrumentation library installed (e.g., pip install openinference-instrumentation-dspy
).
2. Direct Instrumentation
If you have an existing OpenTelemetry TracerProvider
configured in your application (or if LangWatch is configured to use the global provider), you can use the community instrumentor’s instrument()
method directly. LangWatch will automatically pick up the spans generated by these instrumentors as long as its exporter is part of the active TracerProvider
.
Key points for community instrumentors:
- These instrumentors often patch DSPy at a global level, meaning all DSPy calls will be captured once instrumented.
- If using
langwatch.setup(instrumentors=[...])
, LangWatch handles the setup. - If instrumenting directly (e.g.,
DSPyInstrumentor().instrument()
), ensure that theTracerProvider
used by the instrumentor is the same one LangWatch is exporting from. This usually means LangWatch is configured to use an existing global provider or one you explicitly pass tolangwatch.setup()
.
Which Approach to Choose?
autotrack_dspy()
is ideal for targeted instrumentation within specific traces or when you want fine-grained control over which DSPy program executions are tracked. It’s simpler if you’re not deeply invested in a separate OpenTelemetry setup.- Community Instrumentors (like OpenInference’s
DSPyInstrumentor
) are powerful if you’re already using OpenTelemetry, want to capture DSPy calls globally across your application, or need to instrument other libraries alongside DSPy with a consistent OpenTelemetry approach. They provide a more holistic observability solution if you have multiple OpenTelemetry-instrumented components.
Choose the method that best fits your existing setup and instrumentation needs. Both approaches effectively send DSPy call data to LangWatch for monitoring and analysis.
Example: Chainlit Bot with DSPy and LangWatch
The following is a more complete example demonstrating autotrack_dspy()
in a Chainlit application, similar to the dspy_bot.py
found in the SDK examples.
By calling autotrack_dspy()
within your LangWatch-traced functions, you gain valuable insights into your DSPy program’s behavior, including latencies, token counts, and the flow of data through your defined modules and signatures. This is essential for debugging, optimizing, and monitoring your DSPy-powered AI applications.