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

# Pydantic AI

> Integrate Pydantic AI with automatic Gentrace tracing

<Info>
  For a complete example, see the [Pydantic AI
  example](https://github.com/Gentrace/gentrace-python/blob/main/examples/pydantic_ai_simple.py)
  on GitHub.
</Info>

Pydantic AI uses OpenTelemetry internally, which Gentrace automatically captures when initialized, providing seamless tracing without additional configuration.

## Prerequisites

* Python 3.8+
* OpenAI API key (or other supported LLM)
* [Gentrace API key](https://gentrace.ai/s/api-keys)

## Installation

<CodeGroup>
  ```bash pip theme={null}
  pip install gentrace pydantic-ai pydantic-ai-openai
  ```

  ```bash uv theme={null}
  uv pip install gentrace pydantic-ai pydantic-ai-openai
  ```
</CodeGroup>

## Configuration

Simply initialize Gentrace before using Pydantic AI:

```python pydantic_ai_simple.py theme={null}
import os
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIModel
from gentrace import init, interaction

# Initialize Gentrace (will capture Pydantic AI's OTEL traces)
init(
    api_key=os.getenv("GENTRACE_API_KEY"),
    base_url=os.getenv("GENTRACE_BASE_URL", "https://gentrace.ai/api"),
)

# Create a simple Pydantic AI agent
agent = Agent(
    OpenAIModel("gpt-4o-mini"),
    system_prompt="You are a helpful assistant that gives concise answers.",
)

@interaction(name="pydantic_ai_chat", pipeline_id=os.getenv("GENTRACE_PIPELINE_ID", ""))
async def chat_with_agent(prompt: str) -> str:
    result = await agent.run(prompt)
    return result.output

# Usage
import asyncio

async def main():
    response = await chat_with_agent("What is 2+2?")
    print(f"Agent says: {response}")

asyncio.run(main())
```

## Environment Variables

```bash .env theme={null}
GENTRACE_API_KEY=your-gentrace-api-key
GENTRACE_PIPELINE_ID=your-pipeline-id
OPENAI_API_KEY=your-openai-api-key
```

## How It Works

Pydantic AI automatically generates OpenTelemetry traces for:

* Agent invocations
* Model calls
* Tool usage
* Retries and validation

Since Gentrace is OpenTelemetry-compatible, it automatically captures all these traces without requiring any additional instrumentation.

## Supported Models

Pydantic AI supports multiple models through different providers:

* OpenAI (GPT-4, GPT-3.5)
* Anthropic (Claude)
* Google (Gemini)
* Groq
* Mistral

## Related Resources

* [SDK Reference: init](/getting-started/initialization)
* [SDK Reference: interaction](/tracing/interactions)
* [OpenTelemetry Advanced](/reference/opentelemetry-guide)
