Pydantic AI
Type-safe agents with automatic validation
What it is
Pydantic framework for building AI agents with structured outputs automatically validated via Python types. Eliminates malformed JSON issues: define a Pydantic schema and the agent always returns conforming data. Supports all major models (OpenAI, Anthropic, Gemini) with the same interface.
How to use it, step by step
- 1
Install: pip install pydantic-ai (Python 3.9+).
- 2
Define a Pydantic model (BaseModel) describing the structure of the data you want as output.
- 3
Create an Agent specifying the LLM model and result_type equal to your Pydantic schema.
- 4
Run agent.run_sync('your request'): the output comes already validated and typed, not raw text.
- 5
Access fields with dot notation (result.output.field), like a normal Python object.
💡Practical tips
- →Perfect when you need reliable output to feed other code (no more fragile JSON parsing).
- →Use tools decorated with @agent.tool to give the agent functions it can call.
- →Switching provider (OpenAI to Anthropic) leaves your code almost identical.
💰Pricing
Open-source and free. You only pay for model usage.
The official site opens in a new tab.