Learn how to instrument DSPy programs with the LangWatch Python SDK
autotrack_dspy()
on the current LangWatch trace.
autotrack_dspy()
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.
autotrack_dspy()
:langwatch.get_current_trace()
).dspy.Predict
, dspy.ChainOfThought
, dspy.Retrieve
) and the underlying LM calls.langwatch.setup()
OpenInferenceDSPyInstrumentor
from OpenInference) to the instrumentors
list in the langwatch.setup()
call. LangWatch will then manage the lifecycle of this instrumentor.
pip install openinference-instrumentation-dspy
).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
.
langwatch.setup(instrumentors=[...])
, LangWatch handles the setup.DSPyInstrumentor().instrument()
), ensure that the TracerProvider
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 to langwatch.setup()
.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.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.autotrack_dspy()
in a Chainlit application, similar to the dspy_bot.py
found in the SDK examples.
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.