Production Python agent framework with type-safe Pydantic validation, durable execution, A2A communication, and Logfire observability, model-agnostic.
Before you start
Python 3.10+ • pip install pydantic-ai
Define a Pydantic model for the shape of output you want from the agent. PydanticAI wraps the model call and validates the response before returning it, bad shape = exception, not a silently broken dict.
PydanticAI supports typed tools and dependency injection, making agents testable. Define tools as typed Python functions. The agent calls them and the return values are validated too.
PydanticAI integrates with Logfire for structured tracing of every agent run, tool call, and validation step. Set LOGFIRE_TOKEN and call logfire.configure() before running, then write a test that passes bad input and asserts the validation error, proving your schema contract works.
PydanticAI's core building blocks, result models, tools, dependency injection, and Graph for durable workflows.
| Command / Skill | What it does | When to use it |
|---|---|---|
result_type=YourModel | Declare the output shape as a Pydantic model, agent validates and returns typed output, not raw text | Any agent where downstream code needs to parse the response, always use this over string outputs |
@agent.tool | Decorator that exposes a typed Python function as a tool the agent can call | Giving the agent access to external data, APIs, or computations, type hints become the tool schema |
RunContext[Deps] | Typed dependency injection container, share HTTP clients, DB connections, and config across tools | Any agent with tools that need shared resources, enables testing without mocking the LLM |
agent.run() / run_sync() | Async and sync entry points, run() for FastAPI/async contexts, run_sync() for scripts and notebooks | Choose based on your runtime: async app = run(), script = run_sync() |
PydanticAI Graphadvanced | Durable graph-based workflows, checkpointed state, resumable runs, human-in-the-loop nodes | Multi-step workflows that must survive failures or need a human approval checkpoint between steps |
Logfire integrationadvanced | Built-in observability, traces every agent run, tool call, and validation event with structured logs | Set LOGFIRE_TOKEN before writing any agent logic, debugging without it is significantly harder |