Built by the team behind Pydantic — the validation library already running inside FastAPI and most other Python AI tools. The core idea: treat an LLM call like a typed function call, not a string-in-string-out black box.
Core concepts
- Typed, validated output: Pass
Agent()anoutput_typeand get back a validated Pydantic model instead of a raw string, with automatic retries if the model returns something that doesn't fit. Pydantic-AI Docs @agent.tooland@agent.tool_plain: Register tools two ways —tool_plainfor stateless functions,toolwhen a tool needsRunContextfor dependency injection.RunContextdependency injection: Pass a database connection, an authenticated user, or an HTTP client into every tool call without reaching for global state.TestModel: A substitute for a real LLM in unit tests — deterministic, fast, offline test runs without burning API credits on every CI run.- Provider-neutral: Supports OpenAI, Anthropic, Google, Groq, Mistral, Bedrock, and any OpenAI-compatible endpoint — swapping providers is usually one string.
Why it matters
Production agents need predictable structures for downstream APIs and UI. TestModel also lets CI run without spending tokens.
Where people get stuck
- Loose schemas with dozens of optional fields. Models fill them with noise, so keep only what you use.
- Skipping TestModel in CI. Tool routing regresses silently whenever a prompt changes.
- One giant model class that mixes validation with business rules. Keep them apart.
Try this
Define a Pydantic model for a tool result, write a TestModel test that checks the parsing and run pytest in CI.
Interview question
Why validate LLM output with Pydantic?
Models get structure wrong. Validation fails fast before bad data reaches a database, a payment or a user, and the error message feeds a retry.
Resources
YouTube learning
Core concepts from video walkthroughs:

