Pydantic AI
Type: full-code · Vendor: Pydantic Services Inc. · Language: Python · License: MIT · Status: active · Status in practice: emerging · First released: 2024-12-02
Provide a Python-first, model-agnostic agent framework that brings the 'FastAPI feeling' to GenAI by making Pydantic-validated structured output, type-safe dependency injection, and a graph-based async agent loop the primary surface for production LLM applications.
Description. Pydantic AI is the MIT-licensed Python agent framework from the Pydantic team. The framework is built around an Agent class generic in both a dependency type and an output type, so each agent run is a strongly typed function from inputs to a Pydantic-validated result. Its distinguishing thesis: structured output via Pydantic models is the primary action format. Model-agnostic across major providers (OpenAI, Anthropic, Gemini, DeepSeek, Cohere, Mistral, Bedrock, Ollama, Groq, OpenRouter, …); MCP-first toolset shape; durable execution via TemporalAgent, DBOSAgent, PrefectAgent, RestateAgent wrappers.
Agent loop shape. Agent class generic in deps_type and output_type. Agent.run() returns AgentRunResult; Agent.iter() exposes the run as a pydantic-graph of nodes — UserPromptNode → ModelRequestNode → CallToolsNode → End. UsageLimits caps requests/tokens/tool-calls before each request and raises UsageLimitExceeded on breach.
Primary use cases
- type-safe agents with Pydantic-validated final answers
- tool-using agents with dependency injection (deps_type / RunContext.deps)
- MCP-tool integration in Python
- durable agents via Temporal/DBOS/Prefect/Restate wrappers
Key concepts
- Agent (docs) — Primary interface; generic in DepsT and OutputT (Agent[Foobar, list[str]]).
- deps_type + RunContext (docs) — Type-safe dependency injection — HTTP clients, DB sessions, API keys reach tools/system-prompts via RunContext.deps.
- output_type → structured-output (docs) — Three methods to coerce LLM output to a schema: Tool Output, Native Output, Prompted Output.
- @agent.tool / @agent.tool_plain → tool-use (docs) — Tool registration; tool_plain skips RunContext access.
- Toolset (incl. MCP) → mcp (docs) — A collection of tools registered to an agent in one go; MCP servers fit this shape.
- UsageLimits → step-budget (docs) — Caps requests/tokens/tool_calls; raises UsageLimitExceeded if breached.
- ModelRetry (docs) — Exception tools/validators raise to request a model retry with feedback.
Patterns this full-code implements —
- ★★Structured Output
The framework's defining shape. Three strategies — Tool Output, Native Output, Prompted Output. Validation failure forces a retry.
- ★★Model Context Protocol
Three MCP transports: MCPServerStdio (subprocess), MCPServerStreamableHTTP, plus exposing Agents as MCP servers themselves.
- ★★ReAct
Node graph (UserPromptNode → ModelRequestNode → CallToolsNode → End) implements the canonical reason→tool→observation→reason cycle.
- ★★Step Budget
UsageLimits caps requests, tokens, and tool calls before each request; UsageLimitExceeded raised on breach.
- ★★Tool Use
@agent.tool decorator + RunContext access; @agent.tool_plain for context-free tools; schemas auto-extracted from function signature + docstring.
- ★★Evaluator-Optimizer
pydantic_evals is an evaluation harness (Datasets, Cases, Evaluators, ReportEvaluators) that measures but does NOT close the loop to regenerate. Per-tool retries are handled by ModelRetry.
- ★★Agent Resumption
Durability delegated to Temporal/DBOS/Prefect/Restate via the durable_exec module — TemporalAgent / DBOSAgent / PrefectAgent / RestateAgent wrappers. No built-in checkpointer.
Neighbourhood
Click any neighbour to follow the lineage. Scroll to zoom, drag to pan.