Pydantic AI: the type-safe framework for LLM agents

Samuel Colvin, creator of Pydantic, releases Pydantic AI on 2 December 2024: an agent framework built on Pydantic v2 with validated structured outputs, dependency injection and typed tool calling. MIT licence.

Open SourceAI Open SourcePydantic AIType-SafeAgenticLLMAI

From data validation to agents

On 2 December 2024, Samuel Colvin, creator of the Pydantic library, announces Pydantic AI, a framework for building LLM-based agents with a strongly typed programming model. The project inherits the philosophy of Pydantic v2 — runtime validation driven by type hints — and extends it to the agentic domain. The licence is MIT.

Pydantic has become a de facto standard in the Python ecosystem: it is embedded in FastAPI and used internally by OpenAI, Anthropic, LangChain and many other AI libraries for schema definition. Pydantic AI capitalises on this position by offering an agent framework that uses Pydantic models as a lingua franca for input, output and tools.

Validated structured outputs

The distinctive feature of Pydantic AI is validation of structured outputs. An agent declares its return type as a Pydantic class and the framework constrains the language model to produce responses that match the schema. Validation happens at the boundary: if the model output is invalid, the framework automatically retries with a structured error message sent back to the model.

The mechanism relies on the underlying providers’ native function calling, while keeping a unified abstraction. The same agent code runs on OpenAI, Anthropic, Google Gemini, Groq, Mistral and Ollama without modification, with automatic conversion of the Pydantic schema into the format expected by each provider.

Dependency injection and typed tools

Pydantic AI introduces a dependency injection system based on a generic deps_type parameter: tool functions receive a typed context containing HTTP clients, database connections, configuration and other resources. This choice eases testing and makes side effects explicit, a pattern borrowed from the FastAPI world.

Tools are decorated Python functions: the framework derives the JSON schema of parameters from type annotations, in the same way FastAPI derives the OpenAPI schema of routes. Streaming of partial responses is natively supported, with incremental field validation as tokens are generated.

Observability with Logfire

Pydantic AI integrates natively with Logfire, the observability platform built by the same team. Logfire provides distributed tracing based on OpenTelemetry and allows inspection of model calls, token consumption and the agent’s decision chain. The integration is not mandatory: Pydantic AI exposes standard OpenTelemetry hooks usable with any backend.

Link: ai.pydantic.dev

Need support? Under attack? Service Status
Need support? Under attack? Service Status