Skip to main content
  1. Sign up — Create an account at platform.respan.ai
  2. Create an API key — Generate one on the API keys page
  3. Add credits or a provider key — Add credits on the Credits page or connect your own provider key on the Integrations page
Add the Docs MCP to your AI coding tool to get help building with Respan. No API key needed.
{
  "mcpServers": {
    "respan-docs": {
      "url": "https://docs.respan.ai/mcp"
    }
  }
}

What is DSPy?

DSPy is a framework from Stanford for programming with foundation models using declarative signatures. Instead of manual prompt engineering, DSPy compiles high-level modules into optimized prompts.

Setup

1

Install packages

pip install dspy respan-ai openinference-instrumentation-dspy python-dotenv
2

Set environment variables

export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"
export OPENAI_API_KEY="YOUR_OPENAI_API_KEY"
3

Initialize and run

import os
from dotenv import load_dotenv

load_dotenv()

import dspy
from respan import Respan
from openinference.instrumentation.dspy import DSPyInstrumentor

# Initialize Respan with DSPy instrumentation
respan = Respan(instrumentations=[DSPyInstrumentor()])

# Configure the language model
lm = dspy.LM("openai/gpt-4.1-nano")
dspy.configure(lm=lm)

# Run a simple prediction
predict = dspy.Predict("question -> answer")
result = predict(question="What is the capital of France?")
print(result.answer)
respan.flush()
4

View your trace

Open the Traces page to see your DSPy module execution with LLM call spans.

Configuration

ParameterTypeDefaultDescription
api_keystr | NoneNoneFalls back to RESPAN_API_KEY env var.
base_urlstr | NoneNoneFalls back to RESPAN_BASE_URL env var.
instrumentationslist[]Plugin instrumentations to activate (e.g. DSPyInstrumentor()).
is_auto_instrumentbool | NoneFalseAuto-discover and activate all installed instrumentors via OpenTelemetry entry points.
customer_identifierstr | NoneNoneDefault customer identifier for all spans.
metadatadict | NoneNoneDefault metadata attached to all spans.
environmentstr | NoneNoneEnvironment tag (e.g. "production").

Attributes

In Respan()

Set defaults at initialization — these apply to all spans.
from respan import Respan
from openinference.instrumentation.dspy import DSPyInstrumentor

respan = Respan(
    instrumentations=[DSPyInstrumentor()],
    customer_identifier="user_123",
    metadata={"service": "dspy-app", "version": "1.0.0"},
)

With propagate_attributes

Override per-request using a context manager.
from respan import Respan, workflow, propagate_attributes
from openinference.instrumentation.dspy import DSPyInstrumentor

respan = Respan(instrumentations=[DSPyInstrumentor()])

@workflow(name="handle_request")
def handle_request(user_id: str, question: str):
    with propagate_attributes(
        customer_identifier=user_id,
        thread_identifier="conv_001",
        metadata={"plan": "pro"},
    ):
        predict = dspy.Predict("question -> answer")
        result = predict(question=question)
        print(result.answer)
AttributeTypeDescription
customer_identifierstrIdentifies the end user in Respan analytics.
thread_identifierstrGroups related messages into a conversation.
metadatadictCustom key-value pairs. Merged with default metadata.

Decorators

Use @workflow and @task to create structured trace hierarchies.
from respan import Respan, workflow, task
from openinference.instrumentation.dspy import DSPyInstrumentor
import dspy

respan = Respan(instrumentations=[DSPyInstrumentor()])

@task(name="generate_answer")
def generate_answer(question: str) -> str:
    predict = dspy.Predict("question -> answer")
    result = predict(question=question)
    return result.answer

@workflow(name="qa_pipeline")
def pipeline(question: str):
    answer = generate_answer(question)
    print(answer)

pipeline("What are the benefits of LLM observability?")
respan.flush()

Examples

Basic signature

import dspy

lm = dspy.LM("openai/gpt-4.1-nano")
dspy.configure(lm=lm)

predict = dspy.Predict("question -> answer")
result = predict(question="What is machine learning?")
print(result.answer)

Chain of thought

import dspy

lm = dspy.LM("openai/gpt-4.1-nano")
dspy.configure(lm=lm)

cot = dspy.ChainOfThought("question -> answer")
result = cot(question="If a train travels at 60 mph for 2.5 hours, how far does it go?")
print(f"Reasoning: {result.rationale}")
print(f"Answer: {result.answer}")