# OpenInference DSPy Instrumentation
[](https://pypi.org/project/openinference-instrumentation-dspy/)
Python auto-instrumentation library for DSPy.
These traces are fully OpenTelemetry-compatible and can be sent to an OpenTelemetry collector for viewing, such as [`arize-phoenix`](https://github.com/Arize-ai/phoenix).
## Installation
```shell
pip install openinference-instrumentation-dspy
```
## Quickstart
This quickstart shows you how to instrument your DSPy application. It is adapted from the [DSPy quickstart](https://dspy-docs.vercel.app/docs/quick-start/minimal-example).
Install required packages.
```shell
pip install openinference-instrumentation-dspy dspy-ai arize-phoenix opentelemetry-sdk opentelemetry-exporter-otlp
```
Start Phoenix in the background as a collector. By default, it listens on `http://localhost:6006`. You can visit the app via a browser at the same address. (Phoenix does not send data over the internet. It only operates locally on your machine.)
```shell
python -m phoenix.server.main serve
```
Set up `DSPyInstrumentor` to trace your DSPy application and sends the traces to Phoenix at the endpoint defined below.
```python
from openinference.instrumentation.dspy import DSPyInstrumentor
from opentelemetry import trace as trace_api
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
endpoint = "http://127.0.0.1:6006/v1/traces"
tracer_provider = trace_sdk.TracerProvider()
trace_api.set_tracer_provider(tracer_provider)
tracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint)))
DSPyInstrumentor().instrument()
```
Import `dspy` and configure your language model.
```python
import dspy
from dspy.datasets.gsm8k import GSM8K, gsm8k_metric
turbo = dspy.OpenAI(model='gpt-3.5-turbo-instruct', max_tokens=250)
dspy.settings.configure(lm=turbo)
gms8k = GSM8K()
gsm8k_trainset, gsm8k_devset = gms8k.train[:10], gms8k.dev[:10]
```
Define a custom program that utilizes the `ChainOfThought` module to perform step-by-step reasoning to generate answers.
```python
class CoT(dspy.Module):
def __init__(self):
super().__init__()
self.prog = dspy.ChainOfThought("question -> answer")
def forward(self, question):
return self.prog(question=question)
```
Optimize your program using the `BootstrapFewShotWithRandomSearch` teleprompter.
```python
from dspy.teleprompt import BootstrapFewShot
config = dict(max_bootstrapped_demos=4, max_labeled_demos=4)
teleprompter = BootstrapFewShot(metric=gsm8k_metric, **config)
optimized_cot = teleprompter.compile(CoT(), trainset=gsm8k_trainset, valset=gsm8k_devset)
```
Evaluate performance on the dev dataset.
```python
from dspy.evaluate import Evaluate
evaluate = Evaluate(devset=gsm8k_devset, metric=gsm8k_metric, num_threads=4, display_progress=True, display_table=0)
evaluate(optimized_cot)
```
Visit the Phoenix app at `http://localhost:6006` to see your traces.
## More Info
* [More info on OpenInference and Phoenix](https://docs.arize.com/phoenix)
* [How to customize spans to track sessions, metadata, etc.](https://github.com/Arize-ai/openinference/tree/main/python/openinference-instrumentation#customizing-spans)
* [How to account for private information and span payload customization](https://github.com/Arize-ai/openinference/tree/main/python/openinference-instrumentation#tracing-configuration)
Raw data
{
"_id": null,
"home_page": null,
"name": "openinference-instrumentation-dspy",
"maintainer": null,
"docs_url": null,
"requires_python": "<3.14,>=3.9",
"maintainer_email": null,
"keywords": null,
"author": null,
"author_email": "OpenInference Authors <oss@arize.com>",
"download_url": "https://files.pythonhosted.org/packages/60/ba/98a47ebdf61ec0cb6262487d3c551f90cb85ec323914baa67dbd97b3665d/openinference_instrumentation_dspy-0.1.27.tar.gz",
"platform": null,
"description": "# OpenInference DSPy Instrumentation\n\n[](https://pypi.org/project/openinference-instrumentation-dspy/)\n\nPython auto-instrumentation library for DSPy.\n\nThese traces are fully OpenTelemetry-compatible and can be sent to an OpenTelemetry collector for viewing, such as [`arize-phoenix`](https://github.com/Arize-ai/phoenix).\n\n\n## Installation\n\n```shell\npip install openinference-instrumentation-dspy\n```\n\n## Quickstart\n\nThis quickstart shows you how to instrument your DSPy application. It is adapted from the [DSPy quickstart](https://dspy-docs.vercel.app/docs/quick-start/minimal-example).\n\nInstall required packages.\n\n```shell\npip install openinference-instrumentation-dspy dspy-ai arize-phoenix opentelemetry-sdk opentelemetry-exporter-otlp\n```\n\nStart Phoenix in the background as a collector. By default, it listens on `http://localhost:6006`. You can visit the app via a browser at the same address. (Phoenix does not send data over the internet. It only operates locally on your machine.)\n\n```shell\npython -m phoenix.server.main serve\n```\n\nSet up `DSPyInstrumentor` to trace your DSPy application and sends the traces to Phoenix at the endpoint defined below.\n\n```python\nfrom openinference.instrumentation.dspy import DSPyInstrumentor\nfrom opentelemetry import trace as trace_api\nfrom opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter\nfrom opentelemetry.sdk import trace as trace_sdk\nfrom opentelemetry.sdk.trace.export import SimpleSpanProcessor\n\nendpoint = \"http://127.0.0.1:6006/v1/traces\"\ntracer_provider = trace_sdk.TracerProvider()\ntrace_api.set_tracer_provider(tracer_provider)\ntracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint)))\n\nDSPyInstrumentor().instrument()\n```\n\nImport `dspy` and configure your language model.\n\n```python\nimport dspy\nfrom dspy.datasets.gsm8k import GSM8K, gsm8k_metric\n\nturbo = dspy.OpenAI(model='gpt-3.5-turbo-instruct', max_tokens=250)\ndspy.settings.configure(lm=turbo)\ngms8k = GSM8K()\ngsm8k_trainset, gsm8k_devset = gms8k.train[:10], gms8k.dev[:10]\n```\n\nDefine a custom program that utilizes the `ChainOfThought` module to perform step-by-step reasoning to generate answers.\n\n```python\nclass CoT(dspy.Module):\n def __init__(self):\n super().__init__()\n self.prog = dspy.ChainOfThought(\"question -> answer\")\n \n def forward(self, question):\n return self.prog(question=question)\n```\n\nOptimize your program using the `BootstrapFewShotWithRandomSearch` teleprompter.\n\n```python\nfrom dspy.teleprompt import BootstrapFewShot\n\nconfig = dict(max_bootstrapped_demos=4, max_labeled_demos=4)\nteleprompter = BootstrapFewShot(metric=gsm8k_metric, **config)\noptimized_cot = teleprompter.compile(CoT(), trainset=gsm8k_trainset, valset=gsm8k_devset)\n```\n\nEvaluate performance on the dev dataset.\n\n```python\nfrom dspy.evaluate import Evaluate\n\nevaluate = Evaluate(devset=gsm8k_devset, metric=gsm8k_metric, num_threads=4, display_progress=True, display_table=0)\nevaluate(optimized_cot)\n```\n\nVisit the Phoenix app at `http://localhost:6006` to see your traces.\n\n## More Info\n\n* [More info on OpenInference and Phoenix](https://docs.arize.com/phoenix)\n* [How to customize spans to track sessions, metadata, etc.](https://github.com/Arize-ai/openinference/tree/main/python/openinference-instrumentation#customizing-spans)\n* [How to account for private information and span payload customization](https://github.com/Arize-ai/openinference/tree/main/python/openinference-instrumentation#tracing-configuration)",
"bugtrack_url": null,
"license": null,
"summary": "OpenInference DSPy Instrumentation",
"version": "0.1.27",
"project_urls": {
"Homepage": "https://github.com/Arize-ai/openinference/tree/main/python/instrumentation/openinference-instrumentation-dspy"
},
"split_keywords": [],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "81b9e60e0f38d083f0b8cb8e789897631cc90fb7c337f5abd192de5bd360f527",
"md5": "e88508c42da29f500ccce12694acac52",
"sha256": "fd119f993d6aae0d900103a014d820f75906c6fc009ed540754759fb7b425a42"
},
"downloads": -1,
"filename": "openinference_instrumentation_dspy-0.1.27-py3-none-any.whl",
"has_sig": false,
"md5_digest": "e88508c42da29f500ccce12694acac52",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<3.14,>=3.9",
"size": 14691,
"upload_time": "2025-07-30T16:50:35",
"upload_time_iso_8601": "2025-07-30T16:50:35.058858Z",
"url": "https://files.pythonhosted.org/packages/81/b9/e60e0f38d083f0b8cb8e789897631cc90fb7c337f5abd192de5bd360f527/openinference_instrumentation_dspy-0.1.27-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "60ba98a47ebdf61ec0cb6262487d3c551f90cb85ec323914baa67dbd97b3665d",
"md5": "786a38f2b800a6471445a5659c65e23a",
"sha256": "05f0fe9b961e195d8bacd575d9b289daab6151fa0a39ce939a2a01632d55965a"
},
"downloads": -1,
"filename": "openinference_instrumentation_dspy-0.1.27.tar.gz",
"has_sig": false,
"md5_digest": "786a38f2b800a6471445a5659c65e23a",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<3.14,>=3.9",
"size": 25492,
"upload_time": "2025-07-30T16:50:36",
"upload_time_iso_8601": "2025-07-30T16:50:36.176722Z",
"url": "https://files.pythonhosted.org/packages/60/ba/98a47ebdf61ec0cb6262487d3c551f90cb85ec323914baa67dbd97b3665d/openinference_instrumentation_dspy-0.1.27.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-07-30 16:50:36",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Arize-ai",
"github_project": "openinference",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "openai",
"specs": [
[
">=",
"1.0.0"
]
]
},
{
"name": "python-dotenv",
"specs": [
[
">=",
"1.0.0"
]
]
},
{
"name": "grpcio",
"specs": [
[
">=",
"1.60.0"
]
]
},
{
"name": "opentelemetry-api",
"specs": [
[
">=",
"1.22.0"
]
]
},
{
"name": "opentelemetry-sdk",
"specs": [
[
">=",
"1.22.0"
]
]
},
{
"name": "opentelemetry-instrumentation-openai",
"specs": [
[
">=",
"0.40b0"
]
]
},
{
"name": "opentelemetry-instrumentation-langchain",
"specs": [
[
">=",
"0.40b0"
]
]
},
{
"name": "opentelemetry-exporter-otlp-proto-grpc",
"specs": [
[
">=",
"1.22.0"
]
]
},
{
"name": "langchain",
"specs": [
[
">=",
"0.1.0"
]
]
},
{
"name": "langgraph",
"specs": [
[
">=",
"0.0.15"
]
]
},
{
"name": "typing-extensions",
"specs": [
[
">=",
"4.8.0"
]
]
},
{
"name": "phoenix-client",
"specs": [
[
">=",
"0.1.0"
]
]
},
{
"name": "openinference-instrumentation-openllmetry",
"specs": [
[
">=",
"0.1.0"
]
]
}
],
"lcname": "openinference-instrumentation-dspy"
}