Skip to main content

Respan exporter for Pydantic AI SDK

Project description

Respan Exporter for Pydantic AI

respan.ai · Documentation · PyPI

Instrument Pydantic AI agents with Respan: traces, spans, and metrics are sent to Respan via OpenTelemetry and standard semantic conventions. Requires respan-tracing (installed automatically).


Install

pip install respan-exporter-pydantic-ai

Environment Variables

Variable Required Description
RESPAN_API_KEY Yes Respan API key (used when api_key is not passed to RespanTelemetry)
RESPAN_BASE_URL No Respan API base URL (default: https://api.respan.ai/api)

Quickstart

import os
from pydantic_ai import Agent
from respan_tracing import RespanTelemetry
from respan_exporter_pydantic_ai.instrument import instrument_pydantic_ai

respan_api_key = os.environ["RESPAN_API_KEY"]
respan_base_url = os.getenv("RESPAN_BASE_URL", "https://api.respan.ai/api")

# Use Respan as the LLM gateway (no separate OpenAI key needed)
os.environ["OPENAI_BASE_URL"] = respan_base_url
os.environ["OPENAI_API_KEY"] = respan_api_key

# 1. Initialize Respan telemetry
telemetry = RespanTelemetry(app_name="my-app", api_key=respan_api_key)

# 2. Instrument Pydantic AI (global: all agents)
instrument_pydantic_ai()

# 3. Use your agent
agent = Agent("openai:gpt-4o")
result = agent.run_sync("What is the capital of France?")
print(result.output)

To instrument a single agent instead of globally:

agent = Agent("openai:gpt-4o")
instrument_pydantic_ai(agent=agent)

API Reference

RespanTelemetry

Initialize once before calling instrument_pydantic_ai():

Argument Description
app_name Application name shown in Respan.
api_key Optional if RESPAN_API_KEY is set.
base_url Optional; overrides RESPAN_BASE_URL.
is_enabled Set to False to disable tracing.
is_batching_enabled Batch export (default: True); set False for immediate flush in tests.

instrument_pydantic_ai()

Argument Description
agent Optional. If provided, only that agent is instrumented; if None, all agents are instrumented globally.
include_content Include message content in telemetry (default: True).
include_binary_content Include binary content in telemetry (default: True).

Traces appear in the Respan dashboard. Open a trace to see the workflow → task → LLM span tree.


Dev Guide

OpenAI instrumentation dependency

This package lists opentelemetry-instrumentation-openai as a mandatory dependency. respan-tracing >=2.9.0 no longer bundles it, but this exporter still relies on OpenAI instrumentation to extract token usage (prompt_tokens, completion_tokens) as first-class metrics. Pydantic AI's own token attributes are currently mapped to metadata fields only, so removing the OpenAI instrumentor would cause token counts to disappear from span aggregation.

If you want to drop the OpenAI instrumentation dependency, you must first refactor the token extraction so that Pydantic AI's gen_ai.usage.* attributes are promoted to top-level usage metrics (not just metadata). This is a larger change.

Setup

cd python-sdks/respan-exporter-pydantic-ai
poetry install

Tests

# Unit tests (no network)
poetry run pytest tests/test_instrument.py tests/test_extraction_functions.py -v

# All tests
poetry run pytest tests/ -v

# Integration test (real gateway, requires RESPAN_API_KEY)
IS_REAL_GATEWAY_TESTING_ENABLED=1 RESPAN_API_KEY="your-key" \
  poetry run pytest tests/test_real_gateway_integration.py -v -s

Run script (trace tree)

RESPAN_API_KEY="your-key" poetry run python scripts/run_real_gateway_test.py

Further Reading

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

respan_exporter_pydantic_ai-0.1.2.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

respan_exporter_pydantic_ai-0.1.2-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file respan_exporter_pydantic_ai-0.1.2.tar.gz.

File metadata

  • Download URL: respan_exporter_pydantic_ai-0.1.2.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.12.8 Darwin/25.2.0

File hashes

Hashes for respan_exporter_pydantic_ai-0.1.2.tar.gz
Algorithm Hash digest
SHA256 e78a7b2db349431bf5b60b8ae8f2a1e6311796c3873d810ba8c1ea700033ad46
MD5 c5016335ba30ee427627e3b52c1772b5
BLAKE2b-256 442374b525eb7d36a2ac0db772c02d2b87322424ab4038a907b99b948a01fb6f

See more details on using hashes here.

File details

Details for the file respan_exporter_pydantic_ai-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for respan_exporter_pydantic_ai-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 45e694df1bd9b707fd2edf64dc86533d4741137749715e6c7e64954b49ea9c19
MD5 4f0811d8a6f453ff653a8188985c3ffd
BLAKE2b-256 567631b83ddfed9ce263b6fbe6ce61c7c158aafdd97a831fdbf9f2e20c77a592

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page