Pydantic
Pydantic AI
Type-safe agents with Pydantic at the core.
From the team behind Pydantic, this framework brings the same model-validation rigor to agents. Structured outputs, dependency injection, and streaming — all fully typed and async-first.
Install
bash
pip install pydantic-aiQuickstart
A minimal example to verify your setup.
python
from pydantic_ai import Agent
from pydantic import BaseModel
class City(BaseModel):
name: str
country: str
agent = Agent("openai:gpt-4o-mini", output_type=City)
result = agent.run_sync("Where is the Eiffel Tower?")
print(result.output)Core concepts
Typed outputs
Declare a Pydantic model as the output type and get validated, structured results from any supported model.
Dependency injection
Pass typed dependencies (DB sessions, API clients) into tools and system prompts.
Tools
Register Python functions as tools; signatures and docstrings become the schema sent to the model.
Streaming & evals
First-class streaming of partial outputs, plus an evals package for test-driven agent development.
Common use cases
- ›Structured data extraction with validation
- ›API-backed assistants with strict schemas
- ›Type-safe agent backends in FastAPI apps
- ›Production agents needing strong testing