What Is Pydantic AI? Type-Safe LLM Apps

Photo: Sun God Apolo / Pexels
Pydantic AI is a Python framework for building production large language model (LLM) applications with strict type safety, built by the team behind Pydantic, the data-validation library that already runs inside FastAPI, LangChain, and thousands of other Python projects. Launched in December 2024 by Samuel Colvin, it aims to do for LLM development what FastAPI did for web APIs: take a trusted validation engine and wrap it in an ergonomic, opinionated framework. This article covers how it actually enforces type safety, what its newer durable-execution and evaluation tooling does, and where it beats — or loses to — LangChain, Instructor, and OpenAI's own Agents SDK.
The problem Pydantic AI was built to solve
Every developer who has called an LLM API directly has hit the same wall. The model returns a string of text, not a Python object. If your application needs a specific shape of data — a customer's order details, a weather report, a decision with a confidence score — you have to parse that string yourself, handle the times the model hallucinates a field, retry when the JSON is malformed, and write defensive try/except blocks around all of it.
Before frameworks like this existed, most teams solved the problem the hard way: regex extraction, manual JSON schema validation, and hand-rolled retry loops scattered across the codebase. It worked, but it was brittle, and every new endpoint meant rebuilding the same plumbing.
Pydantic AI's answer is to make the model's output a first-class Python type. You define a Pydantic model — the same BaseModel class millions of Python developers already use for data validation — and the framework guarantees the LLM's response matches it, or it never reaches your code.
The single most surprising fact here: Pydantic, the underlying validation library, is downloaded more than 500 million times a month and already runs inside the OpenAI Python SDK itself, according to Pydantic's own documentation. Pydantic AI isn't a new dependency competing for adoption — it's built on infrastructure most Python developers are already running without knowing it.
The core idea: type-safe agents
Everything in Pydantic AI is built around Agents. An agent is a Python object that wraps an LLM, a system prompt, a set of tools (Python functions the model can call), and a strict output type — usually a Pydantic model.
from pydantic import BaseModel
from pydantic_ai import Agent
class WeatherReport(BaseModel):
city: str
temperature_c: float
conditions: str
agent = Agent(
"anthropic:claude-opus-4-7",
output_type=WeatherReport,
system_prompt="You are a weather assistant.",
)
result = agent.run_sync("What's the weather in Istanbul?")
print(result.output.temperature_c) # typed float, validated
The model's response must match the WeatherReport schema. If it doesn't — say the model returns temperature_c as the string "warm" instead of a number — Pydantic AI automatically reprompts the model with the specific validation error until the output passes, or it raises an exception after a configurable number of retries.
Think of it like a copy editor who refuses to publish a manuscript until every fact-check box is ticked. The editor doesn't just reject bad copy silently — they send it back with exactly what's wrong circled in red, and the writer fixes only that. Pydantic AI does the same thing to the model: it doesn't just discard a bad response, it hands the model the precise validation error and asks it to fix that specific field.
How the validation-retry loop actually works
When an agent runs, Pydantic AI sends your prompt plus the target schema (converted to JSON Schema under the hood) to the model. The model returns a response, which Pydantic AI attempts to parse into your output_type. If parsing fails, the framework does not simply throw an error at your application — it captures Pydantic's structured ValidationError, which lists exactly which fields failed and why, and appends that error back into the conversation as a system message.
The model then gets a second attempt with full knowledge of what went wrong, not just "try again." This retry count is configurable per agent (the default is one retry before raising), which matters for cost control: unbounded retries on a broken schema can silently burn through an API budget.
Key features that go beyond structured outputs
Structured output validation is the headline feature, but Pydantic AI has grown a fuller toolkit since its December 2024 launch, according to the official Pydantic AI documentation and the project's GitHub repository.
- Model-agnostic. Works with OpenAI, Anthropic, Google Gemini, DeepSeek, Grok, Cohere, Mistral, Amazon Bedrock, Azure AI Foundry, and local models through Ollama, LiteLLM, Groq, and OpenRouter — all through one unified interface.
- Structured outputs with automatic retry. Covered above; the framework's namesake feature.
- Tools as decorated functions. Use
@agent.toolto expose a Python function to the model. The function's type-annotated signature is converted directly into the JSON schema the model sees — no separate schema file to maintain. - Dependency injection. Pass typed context (database connections, API clients, authenticated user IDs) into tools without global variables or thread-local hacks.
- Streaming with progressive validation. Stream tokens from the model and validate partial Pydantic models as fields fill in, rather than waiting for the full response before any validation happens.
- Durable execution. Agents can preserve progress across process crashes or API failures on long-running workflows, so a multi-step agent doesn't have to restart from zero after a transient network error.
- MCP (Model Context Protocol) support. Agents can connect to external tool servers built on Anthropic's open MCP standard, giving them access to tools maintained outside your own codebase.
- Human-in-the-loop tool approval. Specific tool calls can be flagged to require human sign-off before executing, based on the arguments the model is about to pass — useful for anything that sends money, deletes data, or emails a customer.
- Pydantic Graph. An optional module for building workflows as typed graphs with decision nodes, joins, and parallel branches, aimed at agents whose control flow is too complex for a single linear loop.
- Pydantic Evals. A built-in evaluation framework for testing agent behavior against datasets and tracking accuracy over time, viewable in the CLI or in Logfire.
- Logfire integration. Built-in observability through Pydantic's own Logfire product — traces, token counts, tool call logs, and latency, without bolting on a third-party tracing SDK.
- Multi-agent orchestration. Agents can delegate subtasks to other agents by treating them as tools, letting you compose small, single-purpose agents into a larger system.
Fact check: Pydantic itself — the validation library underneath Pydantic AI — is used inside the OpenAI Python SDK to validate its own API responses, and the broader Pydantic ecosystem has passed 10 billion cumulative downloads across all Python projects, per Pydantic's public GitHub statistics.
A tool example, walked through
Tools are how an agent does anything beyond generating text: querying a database, calling an internal API, running a calculation. Here's a fuller example with dependency injection, which is one of the framework's more distinctive design choices.
from dataclasses import dataclass
from pydantic_ai import Agent, RunContext
@dataclass
class Deps:
db: "Database"
user_id: int
agent = Agent("openai:gpt-4o", deps_type=Deps)
@agent.tool
async def lookup_order_status(ctx: RunContext[Deps], order_id: int) -> str:
"""Look up the shipping status of a customer's order."""
order = await ctx.deps.db.get_order(order_id, user_id=ctx.deps.user_id)
return order.status
result = await agent.run(
"Where's my order #4471?",
deps=Deps(db=my_database, user_id=current_user.id),
)
The model only ever sees a tool definition with a typed order_id: int parameter and the docstring as its description. It has no visibility into ctx.deps.db or ctx.deps.user_id — those are injected by your application at call time, never serialized into a prompt and never sent to the LLM provider. That separation matters in regulated environments where you cannot let a third-party model see a raw database connection string or an internal user ID scheme.
Compare this to the more common pattern of stuffing credentials into global variables or environment lookups inside tool functions — a pattern that works in a single-user script but breaks down the moment two requests from different users are being served by the same process.
What happens if the tool call itself fails?
If a tool raises an exception, Pydantic AI catches it and, depending on configuration, either surfaces the error back to the model as a message (so the model can try a different approach) or propagates it up to your application code. This is configurable per tool, which lets you decide whether a failure should be recoverable by the model or should halt the run entirely — the difference between "the search API timed out, try a narrower query" and "the payment gateway is down, stop everything."
Testing agents without burning API credits
One detail that rarely makes it into introductory coverage: Pydantic AI ships a TestModel and a FunctionModel that let you exercise your agent's tool-calling logic and control flow entirely offline, without hitting a real LLM API or spending a cent on tokens. TestModel returns synthetic responses that satisfy your output schema so you can unit-test the surrounding application code — the parts that consume result.output — independent of model behavior.
For testing actual model quality (does the agent give correct answers, not just correctly shaped answers), Pydantic Evals is the newer tool. You define a dataset of example inputs and expected properties of the output, run the agent against the dataset, and get a pass/fail and scoring report you can track over releases, either from the command line or inside Logfire. This distinction — structural correctness tested for free with TestModel, semantic correctness tested with Pydantic Evals against a real model — is one of the more practical design decisions in the framework, because it separates two very different kinds of bugs that usually get conflated in ad hoc LLM testing.
Pydantic AI vs. other Python LLM frameworks
Comparison of Python LLM and agent frameworks by focus, style, and best-fit use case.
| Framework | Focus | Style | Best For |
|---|---|---|---|
| Pydantic AI | Type-safe agents | Pythonic, FastAPI-feel | Production apps, structured outputs |
| LangChain | Everything | Heavy abstractions, chains | Prototyping, broad ecosystem |
| LlamaIndex | RAG and retrieval | Index/query-centric | Document Q&A, knowledge bases |
| Instructor | Structured outputs only | Patches the model client | Adding validation to existing code |
| OpenAI Agents SDK | OpenAI-first agents | Similar to Pydantic AI | OpenAI-only stacks |
| LangGraph | Stateful agent graphs | Explicit state machines | Long-running, branching workflows |
| CrewAI | Role-based multi-agent teams | High-level "crew" abstraction | Simulated team workflows |
| DSPy | Programmatic prompts | Compile-style optimization | Research, prompt optimization |
vs. LangChain
LangChain's strength is breadth: hundreds of integrations, every retrieval pattern, every vector store and storage backend anyone has ever needed. Its long-standing weakness has been the same thing — deep abstraction layers, breaking changes between minor versions, and stack traces that run five frameworks deep before you find your own code. Pydantic AI is deliberately smaller. There is less machinery between your function and the model, which makes debugging a production incident faster because there are fewer layers to step through.
That trade-off cuts both ways. If you need an integration LangChain already has — a specific vector database connector, a document loader for an obscure file format — writing it yourself in Pydantic AI costs real engineering time that LangChain would have saved you.
vs. Instructor
Instructor, created by Jason Liu, is the elder sibling in the structured-outputs space. It patches the OpenAI (and now other providers') client library to accept a Pydantic model as a response_model parameter, giving you validated structured output with minimal code change to an existing project. Pydantic AI is broader: Instructor does one thing extremely well, while Pydantic AI adds tools, agents, dependency injection, multi-agent orchestration, durable execution, and streaming on top of the same validation core. If structured output is genuinely all you need and you already have a working LLM integration, Instructor is a lighter lift. If you're building the agent from scratch, Pydantic AI's broader scope avoids stitching multiple libraries together later.
vs. OpenAI Agents SDK
OpenAI released its own Agents SDK in March 2025 with a similar philosophy: a thin, type-aware agent framework rather than a heavyweight one. The practical difference is model coverage. Pydantic AI treats every provider as a first-class citizen through one abstraction; OpenAI's SDK is naturally strongest with OpenAI's own models, with other providers supported but less battle-tested inside that ecosystem. If your product needs to swap model providers cheaply — for cost, latency, or capability reasons — Pydantic AI's provider-neutral design is the safer long-term bet.
vs. LangGraph
LangGraph, also from the LangChain team, focuses on explicit state machines for agents whose logic branches, loops, and needs to resume after a pause — think an approval workflow that waits days for a human response. Pydantic AI's own Pydantic Graph module covers similar ground for teams that want that capability without leaving the Pydantic AI ecosystem, but LangGraph has a longer track record for extremely long-running, human-in-the-loop-heavy workflows and more documented production deployments at scale, including at companies like Klarna and Elastic. For a deeper look at how graph-based agent orchestration works, see our explainer on LangGraph.
vs. CrewAI
CrewAI frames multi-agent systems as a "crew" of role-playing agents (a researcher, a writer, an editor) coordinating on a task, which is an intuitive mental model for teams new to multi-agent design. Pydantic AI's multi-agent support is lower-level: agents call other agents as tools, giving you more control over exactly how and when delegation happens, at the cost of more code to write yourself. CrewAI is faster to prototype with; Pydantic AI is easier to reason about and debug once the system grows past a handful of agents.
vs. DSPy
DSPy takes a fundamentally different approach: instead of hand-writing prompts, you declare the input/output signature you want and let DSPy's optimizer search for effective prompts and few-shot examples automatically, similar to how a compiler optimizes code. It's a research-oriented tool best suited to teams actively tuning prompt performance against a metric. Pydantic AI doesn't optimize prompts for you — it assumes you're writing them — and instead focuses on guaranteeing the shape of what comes back. The two aren't strictly competitors; some teams use DSPy to find a good prompt, then wire that prompt into a Pydantic AI agent for production.
When to reach for Pydantic AI
It's a strong default choice when:
- You're building a production Python application, not a one-off prototype.
- You want strict typed outputs with automatic retry on validation failure.
- Your team already uses Pydantic and FastAPI, so the conventions and mental model already match.
- You need to support multiple model providers behind one interface, either for redundancy or cost arbitrage.
- You want observability without bolting on a separate tracing system.
- Your agent has tool calls that touch money, user data, or irreversible actions, and you need human-in-the-loop approval gates.
It's a weaker fit when:
- You're prototyping in a notebook and want the broadest possible plugin ecosystem — LangChain still wins there.
- Your primary need is retrieval-augmented generation over a large document corpus — LlamaIndex has more purpose-built infrastructure for chunking, indexing, and hybrid search.
- You're locked into OpenAI specifically and want their first-party SDK with the tightest possible integration to OpenAI-only features.
- Your team wants a fully managed, no-code agent builder rather than a code-first framework — Pydantic AI assumes a comfortable Python developer on the other end.
Adoption and trajectory
Pydantic AI moved from a December 2024 initial release to a 1.x stable API by late 2025, and by April 2026 the project had crossed roughly 16,000 to 19,000 GitHub stars depending on the measurement date, according to the project's public GitHub repository. Its inclusion as a supported framework option in Amazon's Bedrock AgentCore tooling is one signal of enterprise-side adoption beyond the open-source community itself.
The credibility runway here is unusual for a year-old framework: Pydantic the library was already load-bearing infrastructure inside FastAPI and the OpenAI Python SDK before Pydantic AI existed, so teams adopting the agent framework weren't taking a bet on an unproven validation engine — only on a new layer built on top of one they already trusted. Samuel Colvin's company, Pydantic, is Sequoia-backed as of 2023, according to Pydantic's company announcement, which gave the project resources to build out Logfire, Evals, and Graph as first-party tooling rather than leaving observability and testing to third parties.
The framework remains small enough to read end-to-end in an afternoon — a genuine advantage when something breaks in production and an engineer needs to understand the whole call path, not just the function they wrote.
What people get wrong about Pydantic AI
"It's just Instructor with extra steps." It shares the structured-output core, but Instructor patches an existing client library for one job. Pydantic AI is a full agent framework: tools, dependency injection, durable execution, and multi-agent composition are not things Instructor tries to do at all.
"Type safety means the model can't hallucinate." It doesn't. Type safety guarantees the shape of the output — the right fields, the right types — not that the content is factually correct. A WeatherReport with temperature_c: float will happily validate a confidently wrong temperature. Structural validation and factual accuracy are separate problems; Pydantic Evals addresses the second one, not the type system.
"You need Logfire to use it." Logfire is the default and best-integrated observability backend, but it's optional. You can run Pydantic AI agents without ever sending a trace to Logfire, using your own logging or a different tracing tool entirely.
"It only works with OpenAI." A common assumption carried over from frameworks that started OpenAI-only and expanded later. Pydantic AI was designed provider-neutral from its first release, and Anthropic, Google, and open-weight models via Ollama have been supported since early versions, not bolted on afterward.
"Structured outputs and function calling are the same feature." They overlap but aren't identical. Function calling (tool use) is about letting the model invoke code mid-conversation to fetch information or take an action. Structured outputs are about constraining the model's final response to a fixed schema. Pydantic AI supports both, and a single agent commonly uses tool calls to gather information, then returns a structured, validated final answer.
Frequently Asked Questions
Q: Who built Pydantic AI? A: Samuel Colvin and the Pydantic team built it, launching in December 2024 on top of the Pydantic validation library, which was already widely used across the Python ecosystem including inside FastAPI and the OpenAI Python SDK.
Q: Is Pydantic AI better than LangChain? A: They solve overlapping problems differently. Pydantic AI is smaller, more type-safe, and closer to plain Python; LangChain has far more built-in integrations. Most teams building new production agents in 2026 lean toward Pydantic AI for its smaller surface area, while teams needing a specific niche integration LangChain already supports may find it faster to stay there.
Q: Does Pydantic AI work with Claude and Gemini?
A: Yes. It supports OpenAI, Anthropic Claude, Google Gemini, DeepSeek, Grok, Cohere, Mistral, Amazon Bedrock, Azure AI Foundry, and local models via Ollama, LiteLLM, Groq, and OpenRouter, all through one unified Agent interface.
Q: How is Pydantic AI different from Instructor? A: Instructor patches an existing model client to accept a Pydantic model as a response schema, focusing solely on structured outputs. Pydantic AI is a broader agent framework that includes structured outputs plus tools, dependency injection, multi-agent orchestration, durable execution, and streaming.
Q: Do I need Logfire to use Pydantic AI? A: No. Logfire is the default observability integration and the most tightly built-in option, but it's optional. You can run agents without sending any trace data to Logfire at all.
Q: Can I use Pydantic AI for retrieval-augmented generation (RAG)? A: Yes, by writing retrieval logic as a tool the agent can call. If RAG over a large document set is your primary use case, though, LlamaIndex ships more out-of-the-box infrastructure for chunking, indexing, and hybrid search.
Q: Does type safety in Pydantic AI stop the model from making factual mistakes? A: No. Type safety only guarantees the output matches the expected structure — correct fields, correct types. It cannot verify that the content itself is factually accurate. Catching factual errors requires a separate evaluation step, which is what Pydantic Evals is built for.
Q: What is Pydantic Graph and when do I need it? A: Pydantic Graph is an optional module for defining agent workflows as typed graphs with decision nodes, joins, and parallel branches. It's useful once an agent's control flow gets too complex for a single linear loop, such as workflows with multiple conditional paths or steps that need to run in parallel and then merge.
Q: Is Pydantic AI free and open source? A: Yes, the core framework is MIT-licensed and free, according to its GitHub repository. Logfire, the observability product, has its own separate pricing for hosted usage beyond a free tier, though it isn't required to use Pydantic AI itself.
Q: Can Pydantic AI agents call other agents? A: Yes. Multi-agent orchestration works by treating one agent as a tool that another agent can call, letting you compose several small, single-purpose agents into a larger system rather than writing one large agent that tries to do everything. For a broader look at how autonomous multi-step agents work in general, see our explainer on AI agents.
The takeaway
Pydantic AI's real contribution isn't structured outputs alone — validation libraries have done that for years. It's treating an LLM's output as a Python type the rest of your application can trust, then building tools, dependency injection, durable execution, and evaluation around that same guarantee, all inside a codebase small enough to read in an afternoon. That combination, backed by a validation library already running inside most of the Python AI ecosystem, is why it moved from a December 2024 launch to a widely recommended production default within about eighteen months. It won't replace LangChain's integration breadth or LlamaIndex's retrieval depth, and it can't stop a model from being confidently wrong — only from being wrongly shaped. For teams building a new production Python agent from scratch, though, it's become one of the safer starting points in 2026, precisely because it does less, more predictably. For a wider view of how agent frameworks fit into the current AI stack, see our complete AI guide.
Read next:
Sources
- Colvin, S. (2024). "Introducing Pydantic AI." Pydantic blog.
- Pydantic AI official documentation, pydantic.dev/docs/ai/overview.
- Pydantic AI GitHub repository, github.com/pydantic/pydantic-ai.
- Pydantic company announcement and about page, pydantic.dev/about, pydantic.dev/articles/company-announcement.
- OpenAI (2025). "Introducing the Agents SDK." OpenAI blog.
- Liu, J. (2023–2026). Instructor library documentation.
Follow new articles
Add SciHub101 to your feed reader to see new science and technology stories.
About the Author
SciHub101 is an independent science and technology publication founded and edited by Buğra Sözeri, a software engineer and app developer based in Türkiye. AI tools assist with drafting and research. Source documentation in the existing archive is uneven, and articles are being reviewed and corrected. The Editorial Team byline identifies editorial responsibility; it does not certify specialist or independent human review. Where we compare products, anything built by our own team is labelled as such — see the editorial guidelines and About page for how corrections and disclosures work.
Related Articles

LangChain vs LlamaIndex — LLM Framework Compared
LangChain and LlamaIndex both build LLM apps in Python. Differences, strengths, code examples, and when to pick each in 2026.

Windsurf vs Cursor — AI Code Editor Comparison
Windsurf (now Devin Desktop) and Cursor are top AI-first IDEs. Compare Devin Local vs Composer 2.5, pricing, models, and which fits your workflow.

Function Calling vs Tool Use in LLMs Explained
Function calling and tool use are the same concept under different names. Learn how LLMs invoke external tools, with examples from OpenAI, Anthropic, and Google.

How AI Models Are Trained: The Full Pipeline Explained
Training a large language model is not a single act — it is a five-stage industrial process that begins with petabytes of scraped text, passes through weeks of GPU computation consuming millions of dollars of electricity, and ends with a model that can hold a coherent conversation. Each stage has a specific job, a distinct failure mode, and a measurable cost. Here is how the whole pipeline works, from raw data to deployed product.

How to Use Claude AI: Anthropic's Chatbot Explained, Features, Strengths, and Limitations
Claude is an AI chatbot built by Anthropic and available on Claude.ai, through the API, and via third-party integrations. It excels at long-form writing, coding, analysis of complex documents, and reasoning through multi-step problems. Unlike ChatGPT, Claude does not train on conversations, offers higher context limits, and produces more thorough outputs. This guide explains what Claude is, how to access it, what it does better than competitors, its actual limitations, and how to prompt it effectively.

How to Write AI Prompts That Actually Work
The quality of an AI model's output is determined largely by the quality of the input it receives. A prompt is not a search query — it is the only communication channel between the user and the model. Every word specifies or leaves ambiguous something about the desired output. This guide covers the principles behind effective prompting, the techniques with the most research evidence behind them, and the common mistakes that produce frustrating, generic results.