> ## Documentation Index
> Fetch the complete documentation index at: https://docs.agnost.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# DSPy

> Capture traces from DSPy modules and optimizers

<Note>
  DSPy traces can include module inputs, outputs, prompts, completions, and optimizer data. Review [Data Governance](/data-governance) before enabling production traffic.
</Note>

## Install

```bash theme={null}
pip install dspy-ai openinference-instrumentation-dspy \
            opentelemetry-sdk opentelemetry-exporter-otlp-proto-http
```

## Setup

```python theme={null}
import os, dspy
from openinference.instrumentation.dspy import DSPyInstrumentor
from openinference.instrumentation import using_attributes
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter

os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = "https://otel.agnost.ai"
os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = "X-Agnost-Org-ID=<your-org-id>"

provider = TracerProvider()
provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))
trace.set_tracer_provider(provider)
DSPyInstrumentor().instrument(tracer_provider=provider)

dspy.settings.configure(lm=dspy.LM("openai/gpt-4o-mini"))
qa = dspy.Predict("question -> answer")

with using_attributes(session_id="sess-123", user_id="u-42"):
    qa(question="What is OTel?")
```

Module/program/optimizer spans nest properly and carry `dspy.module`, `dspy.signature`, plus standard OpenInference `input.value` / `output.value`. Pair with a provider instrumentation (e.g. `OpenAIInstrumentor`) for the deepest trace tree.

## Alternative: MLflow

If you already use MLflow, `mlflow.dspy.autolog()` works too: Agnost reads `mlflow.spanInputs` / `mlflow.spanOutputs`.

## Verify

Run one DSPy module call, then open **Raw logs** in Agnost. Confirm `dspy.module` and input/output attributes are present.

## Troubleshooting

* Instrument DSPy before calling modules.
* Pair DSPy instrumentation with provider instrumentation if you need LLM-level spans.
* Confirm `OTEL_EXPORTER_OTLP_HEADERS` contains `X-Agnost-Org-ID=<your-org-id>`.

## References

* [Enable OpenTelemetry export](https://dspy.ai/tutorials/observability/)
* [Add custom metadata](https://mlflow.org/docs/latest/genai/tracing/app-instrumentation/manual-tracing/)
