Pydantic AI overview
Pydantic AI is the Pydantic team's agent framework that brings type-checked, validated output to language model apps.
Last updated: 28 Sep, 2026 · Pydantic AI 2.51
It is built by Pydantic, the company behind the Pydantic validation library that many model SDKs already depend on, and it is released under the MIT licence. This course uses pydantic-ai-slim 2.51.0 and builds one program throughout: a support desk agent that looks up orders and asks a person before large refunds.
What Pydantic AI is for
An Agent holds a model, its instructions, its tools and the type of answer you want back. You describe the answer with ordinary Python type hints, and Pydantic checks the model delivered it. When the model gets it wrong, the validation error is sent back to the model to fix, so your code receives a checked object rather than free text.
How an agent runs
You give the agent a prompt; it sends the prompt to the model; the model may ask to call a tool you wrote; the agent runs the tool and sends the result back; the model then writes the final answer. The whole exchange is kept as a list of messages you can print.
Pick one to watch it run, step by step.
What you will build
A support desk agent that sorts a ticket into a typed record, looks an order up with a tool, reads per-run dependencies such as the current customer, retries when validation fails, and stops for human approval before a large refund. Each lesson adds a few lines, runs them and reads the result.
A Python function as the model
Pydantic AI lets a plain Python function take the place of the model. In FunctionModel: write a stand-in model you write one that sorts tickets by keyword rules, so every answer traces back to a rule you can read and no API key is needed. Real models: providers, keys and model names shows the single line that swaps that function for a hosted model.
Where you use Pydantic AI
- Turning a model's reply into a validated object your code can trust.
- Agents that call your Python functions as tools and read per-run context.
- Flows that retry on bad output and pause for a human before a costly action.
- Handing work between several agents, or to the tools of an MCP server.
Advantages and limits
- Advantage: the answer is a checked Pydantic object, so a wrong shape is caught before it reaches your code.
- Advantage: tools, dependencies, retries, streaming and human approval are part of the framework.
- Advantage: one model-agnostic API reaches OpenAI, Anthropic, Google, Groq and more.
- Limit: for a single free-text call with no validation, calling a provider SDK directly is lighter.
- Limit: you work in terms of typed output and tools, a small shift over ad-hoc string handling.
Prerequisites
- Python classes, type hints, async, and Pydantic models.
- The idea of a tool call from LLM fundamentals.
- No API key needed until Real models: providers, keys and model names; Python 3.10 or newer.
Related
- Next: Installation and setup
- Reference: Pydantic AI documentation
Every expert started right here.