Pydantic AI
What it is
Pydantic AI is a Python agent framework from Pydantic, the company behind the Pydantic validation library. Its docs call it the Python AI SDK: a typed, extensible agent loop with every model a string swap away. The same agent can run behind a web frontend, in a terminal, on a voice call, on a durable background queue, in GitHub Actions, or as a plain object you call run() on.
The core idea is type safety. An agent is generic in its dependency and result types, so you can say what the agent needs and what it must return, and Pydantic validates the result. If the model returns data that fails validation, Pydantic AI feeds the errors back to the model and asks it to retry. That makes it a good fit for data extraction and other work where the output must match a schema.
Who made it
Pydantic AI is made by Pydantic. The documentation is at pydantic.dev, and the source is on GitHub under the pydantic organisation.
- Official docs: https://pydantic.dev/docs/ai/overview/
- GitHub: https://github.com/pydantic/pydantic-ai
What it does
Pydantic AI agents can use tools, call any model, and return typed, structured output. It supports virtually every model and provider — OpenAI, Anthropic, Google, Bedrock, Azure, Groq, Mistral, xAI, Ollama, and many more — swappable with a string, such as openai:gpt-4o, or through the Pydantic AI Gateway, which gives you one key for many models with failover and cost monitoring.
Around the core agent loop, Pydantic offers the Pydantic AI Harness, which adds capabilities such as memory, guardrails, sub-agents, planning, context management, and persistence, up to a complete coding agent; Pydantic Evals, which grades an agent’s outputs and tool-call trajectories; and Pydantic Graph, which gives typed control flow when one agent loop is not enough. Pydantic Logfire, a separate commercial platform, provides tracing, evals, and cost tracking with OpenTelemetry instrumentation. The framework itself sends no usage telemetry and runs in your own process.
How it works
An agent is a container for a system prompt, one or more tools — called function tools in the docs — an optional structured result type, and a dependency type. You create it with Agent('openai:gpt-4o') or another model string, add tools with a decorator, and run it with agent.run(), agent.run_sync(), or agent.run_stream() for streaming. A run can represent a whole conversation, with many messages exchanged, and you can pass messages from earlier runs to continue it.
Tools can use dependencies, such as a database connection or a user ID, which you pass when you run the agent. The agent loop calls the model, runs any tools it requests, and repeats until it can return a result that matches the output type. Model settings, usage limits, and cancellation are part of the agent’s configuration.
How to start
- Install Pydantic AI with
pip install pydantic-aioruv— check the Installation page in the docs for the current command. It needs Python 3.11 or newer, according to the docs. - Set your model API key as an environment variable, for example
OPENAI_API_KEY. - Write a first agent: import
Agentfrompydantic_ai, create it with a model string, and run it withagent.run_sync('Your question'). - Add a tool by decorating a function, and add an
output_typewith a Pydantic model when you need structured output, such as a list of cities with names and populations. - Test with a small task, read the trace, and add guardrails or a fallback model before you give the agent tools that change data.
- When you need more, read the Harness docs for coding agents and durable workflows, and the Evals docs for testing.
Check the official docs for current package names, because the Harness and related packages are newer additions.
Who it is for
Pydantic AI is for Python developers who already use Pydantic and want type-safe agents with validated outputs. It suits data extraction, internal tools, coding agents, and production applications where the output schema matters. It is a code framework, not a no-code builder, and it is a good fit if you value type checking, validation, and running the agent in your own process without a hosted runtime.
Watch out for
Pydantic AI is a fast-moving project with new packages — Harness, Evals, Graph, and the Gateway — so check the docs for the version you installed and do not assume a feature from a blog post is in the core package. The framework is open source, but Logfire and the Gateway are commercial services with their own terms. Costs depend on the model you choose, so set usage limits and test with a small task. Give the agent only the tools it needs, keep API keys in environment variables, and add human approval for irreversible actions. If the model returns invalid data, the retry loop helps, but it can also use extra calls — set a retry limit.
Source
- Official docs: https://pydantic.dev/docs/ai/overview/
- GitHub: https://github.com/pydantic/pydantic-ai