Pydantic AI is a Python framework for building production-ready applications powered by generative AI. Created by the team behind Pydantic, it offers type-safe agents with structured outputs, built-in dependency injection, and native support for leading LLM providers like OpenAI, Anthropic, and Gemini.

This guide shows how to trace Pydantic AI agents with Openlayer.

Configuration

The integration works by sending trace data to Openlayer’s OpenTelemetry endpoint.

The full code used in this guide is available here.

To set it up, you need to:

1

Set the environment variables

Set the following environment variables:

OTEL_EXPORTER_OTLP_ENDPOINT="https://api.openlayer.com/v1/otel"
OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer YOUR_OPENLAYER_API_KEY_HERE, x-bt-parent=pipeline_id:YOUR_PIPELINE_ID_HERE"
2

Configure Logfire

Configure Logfire in your application:

import logfire

logfire.configure(send_to_logfire=False)
logfire.instrument_pydantic_ai()
3

Use Agents as usual

Once instrumentation is set up, you can run your Agents as usual. Trace data will be automatically captured and exported to Openlayer, where you can begin testing and analyzing it.

For example:

from pydantic_ai import Agent

agent = Agent('openai:gpt-4o')
result = await agent.run('What is the capital of France?')