> ## Documentation Index
> Fetch the complete documentation index at: https://docs.openlayer.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Pydantic AI

> Learn how to trace Pydantic AI agents with Openlayer

<img width="700" style={{ borderRadius: "0.5rem" }} src="https://mintcdn.com/openlayer-44/zlWrng2mbxttdDP9/images/integrations/pydantic_ai_hero.png?fit=max&auto=format&n=zlWrng2mbxttdDP9&q=85&s=506d5f2e2d583a85bf5c73c8e9730e1b" alt="Pydantic AI hero" data-path="images/integrations/pydantic_ai_hero.png" />

[Pydantic AI](https://ai.pydantic.dev/) 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](/integrations/opentelemetry).

<Tip>
  The full code used in this guide is available
  [here](https://colab.research.google.com/github/openlayer-ai/openlayer-python/blob/main/examples/tracing/pydantic-ai/pydantic_ai_tracing.ipynb).
</Tip>

To set it up, you need to:

<Steps>
  <Step title="Set the environment variables">
    Set the following environment variables:

    ```bash theme={null}
    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"
    ```
  </Step>

  <Step title="Configure Logfire">
    Configure Logfire in your application:

    ```python theme={null}
    import logfire

    logfire.configure(send_to_logfire=False)
    logfire.instrument_pydantic_ai()
    ```
  </Step>

  <Step title="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:

    ```python theme={null}
    from pydantic_ai import Agent

    agent = Agent('openai:gpt-4o')
    result = await agent.run('What is the capital of France?')
    ```
  </Step>
</Steps>
