Pydantic AI is the agent framework built by the Pydantic team, the very validation library used inside the OpenAI SDK, the Anthropic SDK and LangChain. Its idea is simple and powerful: you build agents in Python where every model output is validated against a type you declare, so errors stop showing up in production and surface while you write the code. In this guide you will see what Pydantic AI is, how its structured outputs work, how to give it tools and dependencies, a complete example agent and how it differs from LangChain. The same explanation is available in Spanish.

Key takeaways

  • Pydantic AI is an open-source agent framework (MIT licence) built on Pydantic’s type system; it has over 18,600 stars on GitHub and is on version v2.11.0, released in July 2026.
  • It is maintained by the Pydantic team, the validation library already used inside the OpenAI SDK, the Anthropic SDK, Google ADK and LangChain, so it inherits a well-tested validation engine.
  • Its defining trait is typed structured output: you define the answer as a Pydantic model (output_type) and the agent guarantees that structure, retrying if the model breaks the format.
  • It is provider-agnostic: it works with more than twenty model providers (OpenAI, Anthropic, Gemini, DeepSeek, Mistral, Groq, Bedrock, Ollama and others) through the same API.
  • It requires Python 3.10 or higher, installs with pip and integrates with Pydantic Logfire for OpenTelemetry-based observability.

What is Pydantic AI?

Pydantic AI is, in the words of its own documentation, the "GenAI agent framework, the Pydantic way". It came from a team with a lot of credibility: the same people who created Pydantic[1], the most widely used data-validation library in the Python ecosystem, present inside the OpenAI SDK, the Anthropic SDK, Google ADK and LangChain. With Pydantic AI they bring that reliability to the world of AI agents.

The project is free software under the MIT licence and has had a notable reception: it holds more than 18,600 stars on GitHub and is on version v2.11.0, released in July 2026. It needs Python 3.10 or higher and installs with a single command:

pip install pydantic-ai

# Slim install: only with the provider you will use
pip install "pydantic-ai-slim[openai]"

The core promise is type safety. The official documentation puts it this way: "moving entire classes of errors from runtime to write-time for a bit of that Rust ‘if it compiles, it works’ feel". In practice that means your editor and type checker understand what each agent returns, and a formatting failure appears before you deploy, not once it is already in your users’ hands.

Structured output and type validation

This is the heart of Pydantic AI. Instead of receiving loose text from the model and parsing it by hand, you declare the answer you expect as a Pydantic model and pass it to the agent in the output_type parameter. The agent instructs the model to produce that shape, validates the response against the schema and, if something does not fit, retries automatically until it gets a valid object.

from pydantic import BaseModel
from pydantic_ai import Agent

class Invoice(BaseModel):
    number: str
    total: float
    currency: str

agent = Agent('anthropic:claude-sonnet-4-5', output_type=Invoice)
result = agent.run_sync('Extract the data from: invoice F-2026-014 for 149.90 EUR')

print(result.output.total)     # 149.9, already validated as a float
print(result.output.currency)  # EUR

The result.output object is not a string you have to interpret: it is an instance of Invoice with its fields already validated and correctly typed. Your IDE autocompletes result.output.total and knows it is a float. If the model returned "one hundred and forty-nine ninety" instead of a number, validation would fail and Pydantic AI would ask the model to fix it, rather than propagating broken data through your application.

Tools and dependencies

A useful agent needs to act on the world: query a database, call an API or read a file. In Pydantic AI you do that with tools, Python functions you decorate with @agent.tool that the model can invoke when it needs them. And to pass them external resources (a database connection, the user id, a key) you use dependency injection with deps_type and the RunContext object.

from dataclasses import dataclass
from pydantic_ai import Agent, RunContext

@dataclass
class Context:
    customer_id: int

agent = Agent('openai:gpt-4o', deps_type=Context)

@agent.tool
async def outstanding_balance(ctx: RunContext[Context]) -> float:
    """Return the outstanding balance for the current customer."""
    return get_balance(ctx.deps.customer_id)

The model decides when to call outstanding_balance, but the sensitive data (here customer_id) is injected by you from outside and travels through ctx.deps, not through the prompt text. This pattern keeps the code decoupled and easy to test: in your tests you can inject a fake context and check the agent without touching the real database. It is the same dependency-injection approach you would see in a mature web framework, applied to agents.

An example agent

With the three pieces together (model, typed output and instructions) a complete agent fits in a few lines. The following example asks for a weather report and gets a validated object, not free text:

from pydantic import BaseModel
from pydantic_ai import Agent

class Report(BaseModel):
    city: str
    temperature_c: float
    summary: str

agent = Agent(
    'openai:gpt-4o',
    output_type=Report,
    instructions='Answer with concrete data.',
)

result = agent.run_sync('Give me today report for Seville')
print(result.output.city)           # Seville
print(result.output.temperature_c)  # e.g. 34.0

Notice the detail: switching provider is as simple as replacing the string 'openai:gpt-4o' with 'anthropic:claude-sonnet-4-5' or 'google:gemini-2.5-pro'. The rest of the code stays untouched, because Pydantic AI abstracts away the difference between the twenty-plus providers it supports. If you want to build more complex agents with state and branches, the library itself includes a graph system, territory similar to what LangGraph covers.

Pydantic AI versus LangChain

The comparison is almost mandatory because Pydantic already lives inside LangChain. The difference in philosophy is clear. LangChain is a huge, veteran ecosystem, with integrations for almost everything and a very capable state graph (LangGraph); in return, it carries many layers of abstraction and a steep learning curve. Pydantic AI bets on the opposite: a small, very Pythonic surface where types are first-class citizens and the code reads almost like plain Python.

If your priority is output reliability, strict typing and clean, testable code, Pydantic AI fits beautifully. If you need an immense catalogue of ready-made integrations or you already have a big investment in LangChain, that ecosystem remains a solid option. They are not mutually exclusive: you can use Pydantic AI for the agent part and lean on other libraries for orchestration, just as you would combine the Anthropic SDK with your own tools. The choice depends on whether you value reach or type discipline more.

Frequently asked questions

Do I need to know Pydantic to use Pydantic AI?

It helps a lot, but you do not need to be an expert. If you can define a class that inherits from BaseModel with a few typed fields, you already have the essentials to declare structured outputs. The rest of the concepts (tools, dependencies, instructions) you learn as you go with the documentation examples. Since Pydantic is the most widespread validation library in Python, you have probably already used it without realising.

Which models does Pydantic AI work with?

With more than twenty providers through the same API: OpenAI, Anthropic, Gemini, DeepSeek, Grok, Mistral, Cohere, Groq, Amazon Bedrock, OpenRouter and others, plus models you run on your own machine with Ollama. Switching from one to another is a matter of editing the model identifier string, without rewriting the agent logic.

Is Pydantic AI suitable for production?

Yes, that is its stated goal. It provides strict input and output validation, durable execution to survive failures in long flows, human approval of sensitive tools and integration with Pydantic Logfire for OpenTelemetry observability. Being on version v2.11.0 and backed by the Pydantic team, it offers a mature base, though it is worth pinning the exact version you use because the project moves fast.

Conclusion

Pydantic AI brings Python’s type discipline to agent development: you define the output you expect as a model, the agent validates it for you and formatting errors surface while you write, not in production. With a small, Pythonic API, support for more than twenty providers and Logfire integration, it is an excellent choice when reliability matters more than the size of the ecosystem. The next step is to install it with pip install pydantic-ai, write your first agent with output_type and compare it with LangGraph to decide which fits your project best.

Sources: [1] Official Pydantic AI documentation[2], [2] Pydantic AI on GitHub[3], [3] pydantic-ai package on PyPI[4], [4] What is Pydantic AI in 2026, Future AGI analysis[5].

Sources

  1. Pydantic
  2. Official Pydantic AI documentation
  3. Pydantic AI on GitHub
  4. pydantic-ai package on PyPI
  5. What is Pydantic AI in 2026, Future AGI analysis

Route: Frameworks to Build AI Agents