Pydantic AI Explained: Type-Safe Agents, Validation and Dependencies in Python

Pydantic AI explained: a Python agent framework that validates every model output against a type you declare. Learn agents, tools, dependencies and output.

By Galactic News Editorial · Published 2026-11-26T23:01:15+00:00 · Updated 2026-11-26T23:01:15+00:00

Pydantic AI explained for Python developers building agents

Pydantic AI explained in one sentence: it is a Python agent framework from the Pydantic team that applies Pydantic validation to every step of an LLM agent loop. You declare the answer shape as a Pydantic model. The agent asks the model for that shape, validates the reply, and retries automatically when the reply does not fit. The result is an agent whose outputs are typed, tested and predictable, instead of loose text you parse by hand.

Pydantic AI Explained: The Core Idea

Pydantic AI comes from the team behind Pydantic, the validation library used inside the OpenAI SDK, the Anthropic SDK, the Google ADK and LangChain. That lineage is the point. The same validation engine that checks your API payloads now checks your model's answers.

The framework's own documentation describes the goal as moving whole classes of errors from runtime to write-time. Your editor and type checker understand what each agent returns. A formatting failure appears while you write the code, not once the agent is already in your users' hands.

Pydantic AI is open source under the MIT licence. It needs Python 3.10 or higher. Install it with pip install pydantic-ai. The default install pulls the core package, the libraries needed for the OpenAI, Anthropic and Google models, plus the CLI, MCP, Evals, Web UI and Logfire integrations. A slimmer install, pydantic-ai-slim, takes only the extras you need.

Pydantic AI explained at the level of daily use starts with agents. Agents are the primary interface. An agent holds a system prompt, one or more function tools, an optional structured result type, and a dependency type constraint. You create an agent once, often as a module global, and reuse it through your application, similar to a small FastAPI app. One agent definition runs as a CLI, a built-in web chat, or realtime speech. It also runs behind a web frontend, in a terminal, on a voice call, or as a plain object you call run() on.

Agents, Tools and Dependencies

An agent is generic in its dependency and result types. An agent that needs an integer dependency and returns a boolean has type Agent[int, bool]. In practice you should not need to think about that. It means your IDE can tell you when you have the right type, and static type checking works if you choose to use it.

There are three ways to run an agent. agent.run() is a coroutine that returns a RunResult with the completed response. agent.run_sync() is the plain synchronous version. agent.run_stream() returns a StreamedRunResult that streams the response as an async iterable.

Tools are functions the model may call while generating a response. You register one with the @agent.tool decorator. The function signature becomes the tool schema sent to the model. The docstring becomes the tool description. Parameter descriptions come from the docstring too. Pydantic validates the tool arguments, and errors are passed back to the model so it can retry.

Dependencies are the typed way to give an agent access to your world. A database connection, a customer ID, or an HTTP client goes into a dependencies dataclass. Tools and instructions receive it through RunContext, which is parameterized with the dependency type. If the type annotation is wrong, static type checkers catch it. Swap in a test double and the same agent runs in unit tests.

Capabilities bundle related tools, instructions, hooks and model settings into reusable units. Core ships fundamentals like MCP and web search. The Pydantic AI Harness adds memory, guardrails, sub-agents, planning, context management and persistence. Complete agents like Coder and Researcher are just capabilities composed. Capabilities can also load on demand, staying hidden until the model decides they are relevant.

Structured Output and Validation

Structured output is the heart of Pydantic AI. Instead of receiving loose text and parsing it by hand, you declare the answer as a Pydantic model and pass it to the agent as output_type. The agent instructs the model to produce that shape, validates the response against the schema, and retries automatically if something does not fit.

Validation can include rules, not just shapes. A field can require a value greater than zero. A field validator can reject a country that is not on your allowed list. When validation fails, Pydantic AI feeds the errors back to the model and asks it to try again. That retry loop is built in. You do not write it yourself.

Observability comes from Pydantic Logfire, which records agent runs and the validations inside them. Each run shows the output the model produced, the errors your validators raised, and the retry that followed. Pydantic Evals grades final outputs and tool-call trajectories, against datasets in code or against a sample of live production traffic.

A Minimal Working Example

Build a small agent in five steps.

  1. Install the package. Run pip install pydantic-ai in your project environment.
  2. Define the output model. Create a Pydantic BaseModel with the fields you need, such as a city name, a country and a population with a greater-than-zero rule.
  3. Create the agent. Pass a model string such as 'openai:gpt-4o' and your model as output_type. The model is a string swap away: change the string and the same agent runs on another provider.
  4. Add a tool if you need one. Write a plain Python function, decorate it with @agent.tool, and give it a clear docstring. The framework turns the signature into a tool schema.
  5. Run it and read typed output. Call agent.run_sync('List the 3 largest cities in Japan') and read result.output. The result is a list of your model instances, not a string to parse.

Provider support is broad. Pydantic AI works with OpenAI, Anthropic, Google, Bedrock, Azure AI Foundry, Groq, Mistral, xAI, Ollama and dozens more through the same API. The Pydantic AI Gateway offers one key for every model, with failover and cost monitoring built in. No flagship feature is locked to one vendor.

When Pydantic AI Fits, and When It Does Not

Pydantic AI fits best when output shape matters. Data extraction, form filling, classification, and structured reporting all benefit from validated output. It fits when you write Python and want type checking across the agent loop. It fits when you need the same agent to run on several providers.

That is the short version of Pydantic AI explained for teams choosing a framework. It fits less well when you want a no-code agent builder, or when your stack is not Python. It also adds a learning curve if your team has never used Pydantic models. For a one-off script that calls a model once and prints the text, the framework is more than you need. Use a plain SDK call instead.

FrameworkCore ideaTyped outputProvider modelBest for
Pydantic AITyped agent loop with validationBuilt in, with automatic retryString swap, dozens of providersPython teams, structured output
LangChainBroad abstractions and integrationsAvailable through output parsersMany integrationsLarge integration surface
Google ADKGoogle agent development kitAvailableGoogle-firstGoogle Cloud workloads
Claude Agent SDKAnthropic agent toolingAvailableAnthropic-firstClaude-centred agents

The comparison page in the official documentation sets out where Pydantic AI differs from LangChain, Google ADK, the Claude Agent SDK and others. Read it before you commit. The differences are real, and they matter more than the feature lists suggest.

Bottom Line

Pydantic AI explained simply: declare the answer as a type, give the agent typed tools and dependencies, and let validation and retry handle the rest. The framework suits Python developers who want structured output they can trust, provider freedom, and errors that surface at write time instead of in production. Start with one small agent, one output model, and one tool. Add capabilities only when the agent outgrows the basics.