What it does
PydanticAI is an agent framework from the Pydantic team. It combines model integrations, typed tools, dependency injection, and structured output validation in ordinary Python code.
Why it’s interesting
Output schemas make agent results easier to consume in application code. Validation can trigger model retries, though exhausted retries and provider failures still need error handling.
Real-world use case
Create an order-support agent that calls an internal API and returns a validated OrderSummary model. Inject an API client through a typed runtime context.
What I can learn
- 01
Typed dependency injection
pass runtime services through RunContext.
- 02
Tool reflection
derive schemas from Python signatures and annotations.
- 03
Validation and retries
turn invalid model output into feedback and handle retry limits.
One thing to explore
Agent tools and runtime context
Follow the implementation and connect it to the learning points above.
Explore the source (opens in a new tab)10-Minute Challenge
Return a typed forecast without an API key
Before you start: A supported Python installation and a virtual environment; no model API key is needed.
- Install pydantic-ai in a Python virtual environment.
- Save the example as demo.py and run python demo.py.
- Change the test output and inspect the returned CityForecast instance.
pip install pydantic-aifrom pydantic import BaseModelfrom pydantic_ai import Agentfrom pydantic_ai.models.test import TestModel
class CityForecast(BaseModel): city: str temperature_c: float condition: str
agent = Agent( TestModel(custom_output_args={ 'city': 'Bengaluru', 'temperature_c': 24.0, 'condition': 'Partly cloudy', }), output_type=CityForecast,)result = agent.run_sync('Describe the weather in Bengaluru.')print(result.output)print(result.output.city)