Loading...
Loading...
Build AI agents with Pydantic AI — tools, capabilities, structured output, streaming, testing, and multi-agent patterns. Use when the user mentions Pydantic AI, imports pydantic_ai, or asks to build an AI agent, add tools/capabilities, stream output, define agents from YAML, or test agent behavior.
npx skill4agent add pydantic/skills building-pydantic-ai-agentspydantic_aiAgentRunContextToolpydanticBaseModelfrom pydantic_ai import Agent
agent = Agent(
'anthropic:claude-sonnet-4-6',
instructions='Be concise, reply with one sentence.',
)
result = agent.run_sync('Where does "hello world" come from?')
print(result.output)
"""
The first known use of "hello, world" was in a 1974 textbook about the C programming language.
"""import random
from pydantic_ai import Agent, RunContext
agent = Agent(
'google-gla:gemini-3-flash-preview',
deps_type=str,
instructions=(
"You're a dice game, you should roll the die and see if the number "
"you get back matches the user's guess. If so, tell them they're a winner. "
"Use the player's name in the response."
),
)
@agent.tool_plain
def roll_dice() -> str:
"""Roll a six-sided die and return the result."""
return str(random.randint(1, 6))
@agent.tool
def get_player_name(ctx: RunContext[str]) -> str:
"""Get the player's name."""
return ctx.deps
dice_result = agent.run_sync('My guess is 4', deps='Anne')
print(dice_result.output)
#> Congratulations Anne, you guessed correctly! You're a winner!from pydantic import BaseModel
from pydantic_ai import Agent
class CityLocation(BaseModel):
city: str
country: str
agent = Agent('google-gla:gemini-3-flash-preview', output_type=CityLocation)
result = agent.run_sync('Where were the olympics held in 2012?')
print(result.output)
#> city='London' country='United Kingdom'
print(result.usage())
#> RunUsage(input_tokens=57, output_tokens=8, requests=1)from datetime import date
from pydantic_ai import Agent, RunContext
agent = Agent(
'openai:gpt-5.2',
deps_type=str,
instructions="Use the customer's name while replying to them.",
)
@agent.instructions
def add_the_users_name(ctx: RunContext[str]) -> str:
return f"The user's name is {ctx.deps}."
@agent.instructions
def add_the_date() -> str:
return f'The date is {date.today()}.'
result = agent.run_sync('What is the date?', deps='Frank')
print(result.output)
#> Hello Frank, the date today is 2032-01-02.from pydantic_ai import Agent
from pydantic_ai.models.test import TestModel
my_agent = Agent('openai:gpt-5.2', instructions='...')
async def test_my_agent():
"""Unit test for my_agent, to be run by pytest."""
m = TestModel()
with my_agent.override(model=m):
result = await my_agent.run('Testing my agent...')
assert result.output == 'success (no tool calls)'
assert m.last_model_request_parameters.function_tools == []from pydantic_ai import Agent
from pydantic_ai.capabilities import Thinking, WebSearch
agent = Agent(
'anthropic:claude-opus-4-6',
instructions='You are a research assistant. Be thorough and cite sources.',
capabilities=[
Thinking(effort='high'),
WebSearch(),
],
)Hooksfrom pydantic_ai import Agent, RunContext
from pydantic_ai.capabilities.hooks import Hooks
from pydantic_ai.models import ModelRequestContext
hooks = Hooks()
@hooks.on.before_model_request
async def log_request(ctx: RunContext[None], request_context: ModelRequestContext) -> ModelRequestContext:
print(f'Sending {len(request_context.messages)} messages')
return request_context
agent = Agent('openai:gpt-5.2', capabilities=[hooks])Agent.from_filefrom pydantic_ai import Agent
# agent.yaml:
# model: anthropic:claude-opus-4-6
# instructions: You are a helpful research assistant.
# capabilities:
# - WebSearch
# - Thinking:
# effort: high
agent = Agent.from_file('agent.yaml')| I want to... | Documentation |
|---|---|
| Create or configure agents | Agents |
| Bundle reusable behavior (tools, hooks, instructions) | Capabilities |
| Intercept model requests, tool calls, or runs | Hooks |
| Define agents in YAML/JSON without Python code | Agent Specs |
| Use template strings in agent instructions | Template Strings |
| Let my agent call external APIs or functions | Tools |
| Organize or restrict which tools an agent can use | Toolsets |
| Give my agent web search with automatic provider fallback | WebSearch Capability |
| Give my agent URL fetching with automatic provider fallback | WebFetch Capability |
| Give my agent web search or code execution (builtin tools) | Built-in Tools |
| Search with DuckDuckGo/Tavily/Exa | Common Tools |
| Ensure my agent returns data in a specific format | Structured Output |
| Pass database connections, API clients, or config to tools | Dependencies |
| Access usage stats, message history, or retry count in tools | RunContext |
| Choose or configure models | Models |
| Automatically switch to backup model when primary fails | Fallback Model |
| Show real-time progress as my agent works | Streaming Events and Final Output |
| Work with messages and multimedia | Message History |
| Reduce token costs by trimming or filtering conversation history | Processing Message History |
| Keep long conversations manageable without losing context | Summarize Old Messages |
| Use MCP servers | MCP |
| Build multi-step graphs | Graph |
| Debug a failed agent run or see what went wrong | Model Errors |
| Make my agent resilient to temporary failures | Retries |
| Understand why my agent made specific decisions | Using Logfire |
| Write deterministic tests for my agent | Unit testing with TestModel |
| Enable thinking/reasoning across any provider | Thinking · Thinking Capability |
| Systematically verify my agent works correctly | Evals |
| Use embeddings for RAG | Embeddings |
| Use durable execution | Durable Execution |
| Have one agent delegate tasks to another | Agent Delegation |
| Route requests to different agents based on intent | Programmatic Agent Hand-off |
| Require tool approval (human-in-the-loop) | Deferred Tools |
| Use images, audio, video, or documents | Input |
| Use advanced tool features | Advanced Tools |
| Validate or require approval before tool execution | Advanced Tools |
| Call the model without using an agent | Direct API |
| Expose agents as HTTP servers (A2A) | A2A |
| Handle network errors and rate limiting automatically | Retries |
| Use LangChain or ACI.dev tools | Third-Party Tools |
| Publish reusable agent extensions as packages | Extensibility |
| Build custom toolsets, models, or agents | Extensibility |
| Debug common issues | Troubleshooting |
| Migrate from deprecated APIs | Changelog |
| See advanced real-world examples | Examples |
| Look up an import path | API Reference |
| Topic | What it covers |
|---|---|
| Decision Trees | Tool registration, output modes, multi-agent patterns, capabilities, testing approaches, extensibility |
| Comparison Tables | Output modes, model provider prefixes, tool decorators, built-in capabilities, agent methods |
| Architecture Overview | Execution flow, generic types, construction patterns, lifecycle hooks, model string format |
"provider:model-name""openai:gpt-5.2""anthropic:claude-sonnet-4-6""google-gla:gemini-3-pro-preview"run()run_sync()run_stream()run_stream_sync()run_stream_events()iter()logfire.instrument_pydantic_ai()logfire.instrument_httpx(capture_all=True)TestModelFunctionModel@agent.toolRunContext@agent.tool_plaintool_plain'openai:gpt-5.2''gpt-5.2'TestModelagent.override()agent.modelwith agent.override(model=TestModel()):strstroutput_typestr.onon_hooks.on.run_errorhooks.on.model_request_errorhooks.on.on_run_errorhistory_processorshistory_processors=[...]history_processor=| Task | Section |
|---|---|
| Add capabilities (Thinking, WebSearch, etc.) | Add Capabilities to an Agent |
| Intercept model requests and tool calls | Intercept Agent Lifecycle with Hooks |
| Define agents from YAML/JSON config files | Define Agents Declaratively with Specs |
| Enable thinking/reasoning across providers | Enable Thinking Across Providers |
| Trim or filter conversation history | Manage Context Size |
| Stream events and show real-time progress | Show Real-Time Progress |
| Auto-switch providers on failure | Handle Provider Failures |
| Write deterministic tests | Test Agent Behavior |
| Delegate tasks between agents | Coordinate Multiple Agents |
| Instrument with Logfire for debugging | Debug and Validate Agent Behavior |