---
title: 'Pydantic AI: Type-Safe Python Framework for AI Agents & LLM Applications'
description: 'Build production-grade AI applications with Pydantic AI - a model-agnostic Python framework featuring type safety, structured outputs, validation, and seamless observability. Supports OpenAI, Anthropic, Google, and more.'
canonical: https://pydantic.dev/pydantic-ai
last-reviewed: '2026-07-22'
---

> Markdown version of [Pydantic AI](https://pydantic.dev/pydantic-ai) — the canonical HTML page.
>
> Site index: [/llms.txt](https://pydantic.dev/llms.txt)

---

# Production-grade applications with Generative AI

Type-safe Python framework for building agents and LLM applications. Model-agnostic with built-in validation, structured outputs, and seamless observability. Open source under the MIT license.

```bash
uv add pydantic-ai
```

Get started: https://pydantic.dev/docs/ai/overview/ · GitHub: https://github.com/pydantic/pydantic-ai

## Build intelligent AI agents

Create agents that can reason, use tools, and interact with external systems. Pydantic AI provides a modular, type-safe platform for building production-ready AI agents with any model provider.

Built-in integration with [Pydantic Logfire](https://pydantic.dev/logfire) for complete visibility into agent runs: trace LLM calls, track token costs, debug failures, and understand latency across your entire AI stack.

```python
import logfire
from pydantic import BaseModel
from pydantic_ai import Agent

logfire.configure()
logfire.instrument_pydantic_ai()

class MyModel(BaseModel):
    city: str
    country: str

agent = Agent("openai:gpt-5.2", output_type=MyModel)

if __name__ == "__main__":
    result = agent.run_sync("The windy city in the US of A.")
    logfire.info(str(result.output))
```

## Connect to MCP servers

The [Model Context Protocol](https://modelcontextprotocol.io/) (MCP) is an open standard for connecting AI models to external tools and data sources. Pydantic AI has built-in support for MCP servers, enabling your agents to access file systems, databases, APIs, and more. See [how Pydantic AI supports MCP](https://pydantic.dev/docs/ai/mcp/overview/).

## Stream AI agent events to your frontend in real time

Stream text, tool calls, and reasoning to your frontend as they happen. Pydantic AI offers out-of-the-box support for the [AG-UI protocol](https://pydantic.dev/docs/ai/integrations/ui/ag-ui/) for standardized agent-to-UI communication and the [Vercel AI Data Stream Protocol](https://pydantic.dev/docs/ai/integrations/ui/vercel-ai/). See the [UI event streams docs](https://pydantic.dev/docs/ai/integrations/ui/overview/).

## Durable execution: build fault-tolerant agents

Build durable agents that preserve their progress across transient API failures and application errors or restarts. Handle long-running, asynchronous, and human-in-the-loop workflows with production-grade reliability. Durable agents have full support for streaming and MCP, with the added benefit of fault tolerance.

Pydantic AI natively supports these durable execution solutions:

- [Temporal](https://pydantic.dev/docs/ai/integrations/durable_execution/temporal/)
- [DBOS](https://pydantic.dev/docs/ai/integrations/durable_execution/dbos/)
- [Prefect](https://pydantic.dev/docs/ai/integrations/durable_execution/prefect/)
- [Restate](https://pydantic.dev/docs/ai/integrations/durable_execution/restate/)

These integrations only use Pydantic AI's public interface, so they also serve as a reference for integrating with other durable systems.

## Why Pydantic AI?

- **Validated structured outputs** — Pydantic validation guarantees type-safety on structured outputs. Trusted by OpenAI, Anthropic, Google, and millions of developers.
- **Integrated AI model routing** — built-in cost control and model routing without performance overhead via [Pydantic AI Gateway](https://pydantic.dev/ai-gateway). BYOK or built-in providers for single-key access to models.
- **Production observability** — built-in integration with [Pydantic Logfire](https://pydantic.dev/logfire) for real-time debugging, tracing, and cost tracking with massive AI workloads.
- **Testing & evaluation** — test your agents with [Pydantic Evals](https://pydantic.dev/docs/ai/evals/evals/): create datasets, run evaluations, track model performance and visualise it on your CLI or in Logfire.
- **Streaming support** — stream responses token-by-token for real-time user feedback; access structured data as it arrives.
- **Multi-agent workflows** — build complex systems with multiple specialized agents, coordinated with graph-based workflows.

## Function tools: give agents access to your code

Use `@agent.tool` or `@agent.tool_plain` decorators to register tool access to an agent context. Pydantic AI automatically generates JSON schemas from your type hints and docstrings, enabling models to call your functions correctly.

```python
import random

from pydantic_ai import Agent, RunContext

agent = Agent(
    "gateway/gemini-3-pro-preview",
    deps_type=str,
    system_prompt=(
        "You're a dice game, you should roll the die and see if the number "
        "you get back matches the user's guess. If so, tell them they're a winner. "
        "Use the player's name in the response."
    ),
)

@agent.tool_plain
def roll_dice() -> int:
    """Roll a six-sided die and return the result."""
    return random.randint(1, 6)

@agent.tool
def get_player_name(ctx: RunContext[str]) -> str:
    """Get the player's name."""
    return ctx.deps

dice_result = agent.run_sync("My guess is 4", deps="Anne")
print(dice_result.output)
# > Congratulations Anne, you guessed correctly! You're a winner!
```

## Part of the Pydantic Stack

Pydantic AI integrates seamlessly with [Pydantic Logfire](https://pydantic.dev/logfire) for complete observability, [Pydantic AI Gateway](https://pydantic.dev/ai-gateway) for intelligent model routing, and [Pydantic Evals](https://pydantic.dev/docs/ai/evals/evals/) for systematic evaluation.

## Ready to build?

Open source (MIT license). Install with uv (or pip) and start building production-grade AI applications today.

```bash
uv add pydantic-ai
# or
pip install pydantic-ai
```

- Documentation: https://pydantic.dev/docs/ai/overview/
- GitHub: https://github.com/pydantic/pydantic-ai
- PyPI: https://pypi.org/project/pydantic-ai
- Pydantic AI is free; Logfire and AI Gateway pricing: https://pydantic.dev/pricing (markdown: https://pydantic.dev/pricing.md)

## Related

- [Pydantic Logfire](https://pydantic.dev/logfire) — observability for your agents in production ([markdown](https://pydantic.dev/logfire.md))
- [Pydantic AI Gateway](https://pydantic.dev/ai-gateway) — one key for all your models ([markdown](https://pydantic.dev/ai-gateway.md))
- [Pydantic AI section index](https://pydantic.dev/pydantic-ai/llms.txt)
