Pydantic AI (tracing)
Pydantic AI (tracing)
Pydantic AI is a Python agent framework from the creators of Pydantic. It provides a type-safe way to build agents with tools, structured outputs, and multi-model support. Respan gives you full observability over every agent run, model call, and tool invocation — and gateway routing through the OpenAI-compatible Respan endpoint.
For TypeScript workloads that emit Pydantic AI-compatible OTEL spans, Respan also supports normalization through @respan/instrumentation-pydantic-ai.
Set up Respan
Create an account at platform.respan.ai and grab an API key.
Run npx @respan/cli setup to set up with your coding agent.
Use Respan Gateway
See Pydantic AI gateway setup to route this integration through the Respan gateway.
Example projects
Setup
Set environment variables
The examples route model calls through the Respan gateway by setting provider-compatible environment aliases in application code, so no separate provider API key is required.
For TypeScript OTEL-compatible workloads, use your OpenAI-compatible client environment and export traces through @respan/instrumentation-pydantic-ai.
View your trace
Open the Traces page to see your agent run with model spans, tool calls, tokens, and cost.
Native OpenTelemetry (without Logfire)
Pydantic AI can emit its native OpenTelemetry spans directly to Respan without configuring Logfire or installing the Respan Pydantic AI plugin. This follows Pydantic’s OTel without Logfire setup and exports the current Pydantic AI GenAI semantic conventions.
Choose one Pydantic AI instrumentation path. Do not combine this setup with PydanticAIInstrumentor() or logfire.instrument_pydantic_ai(); they configure the same Pydantic AI instrumentation and can overwrite each other’s tracer provider, content, and format settings.
Install Pydantic AI and the standard OTLP/HTTP exporter:
Set the model-provider and Respan API keys:
Configure an OpenTelemetry provider, enable Pydantic AI’s native instrumentation, and export directly to Respan:
This path exports Pydantic AI’s native version 5 spans as-is. Use the PydanticAIInstrumentor() setup above when you need Respan-specific field normalization, propagate_attributes, or the instrumentor’s content controls.
Configuration
PydanticAIInstrumentor options
The processor normalizes Pydantic AI v2 message parts, tool definitions, and response formats into JSON-safe string attributes before export, so newer structured chat payloads render correctly in Respan.
PydanticAIInstrumentor options (TypeScript)
Instrument a single agent
By default, PydanticAIInstrumentor() instruments all Pydantic AI agents globally. To instrument only one agent:
Attributes
In Respan()
Set defaults at initialization — these apply to all spans.
With propagate_attributes
Override per-request using a context scope.
Decorators (optional)
Decorators are not required. Pydantic AI model spans and tool calls are auto-traced by the instrumentor. Use @workflow and @task when you want to add structure around one or more agent runs.
Examples
Tool calls
Tool calls are automatically captured as spans with inputs, outputs, and timing.
Structured output
Structured outputs are traced the same way as normal agent runs.