Pydantic AI Instrumentor

Normalize Pydantic AI-compatible TypeScript spans for Respan.

1import { Respan } from "@respan/respan";
2import { PydanticAIInstrumentor } from "@respan/instrumentation-pydantic-ai";
3
4const respan = new Respan({
5 instrumentations: [new PydanticAIInstrumentor()],
6});
7await respan.initialize();

Install

$npm install @respan/respan @respan/instrumentation-pydantic-ai

This instrumentor wraps the active OpenTelemetry SpanProcessor and normalizes Pydantic AI-compatible spans before export. It supports:

  • Pydantic AI-native gen_ai.* span attributes (for example gen_ai.input.messages and gen_ai.output.messages).
  • Pydantic AI-scoped OpenInference spans (for example openinference.span.kind).

Configure

You can control which span sources are normalized in the constructor.

1import { PydanticAIInstrumentor } from "@respan/instrumentation-pydantic-ai";
2
3const pydanticAIInstrumentor = new PydanticAIInstrumentor({
4 includeNativeSpans: true,
5 includeOpenInferenceSpans: true,
6});

Example

1import { Respan } from "@respan/respan";
2import { PydanticAIInstrumentor } from "@respan/instrumentation-pydantic-ai";
3import { trace } from "@opentelemetry/api";
4
5const respan = new Respan({
6 apiKey: process.env.RESPAN_API_KEY,
7 instrumentations: [new PydanticAIInstrumentor()],
8});
9await respan.initialize();
10
11const tracer = trace.getTracer("pydantic-ai-compatible");
12const span = tracer.startSpan("chat completion", {
13 attributes: {
14 "gen_ai.operation.name": "chat",
15 "gen_ai.system": "openai",
16 "gen_ai.request.model": "gpt-4o-mini",
17 },
18});
19span.end();
20
21await respan.flush();