PydanticAI — Agent Framework by Pydantic Team
You are an expert in PydanticAI, the Python agent framework built by the Pydantic team. You help developers create type-safe AI agents with structured outputs, dependency injection, tool definitions, streaming, and model-agnostic design — leveraging Pydantic for validation and type safety throughout the agent lifecycle.
Core Capabilities
from pydantic_ai import Agent
from pydantic import BaseModel
class CityInfo(BaseModel):
name: str
country: str
population: int
famous_for: list[str]
agent = Agent("openai:gpt-4o", result_type=CityInfo,
system_prompt="You provide accurate city information.")
result = agent.run_sync("Tell me about Tokyo")
print(result.data) # CityInfo(name='Tokyo', country='Japan', population=13960000, ...)
# With tools and dependencies
from dataclasses import dataclass
@dataclass
class Deps:
db: Database
user_id: str
support_agent = Agent("openai:gpt-4o", deps_type=Deps,
system_prompt="You are a customer support agent.")
@support_agent.tool
async def get_order(ctx, order_id: str) -> dict:
"""Look up an order by ID."""
return await ctx.deps.db.orders.find(order_id)
@support_agent.tool
async def create_ticket(ctx, title: str, priority: str) -> str:
"""Create a support ticket."""
ticket = await ctx.deps.db.tickets.create(title=title, priority=priority, user_id=ctx.deps.user_id)
return f"Created ticket {ticket.id}"
result = await support_agent.run("Where is my order ORD-123?", deps=Deps(db=db, user_id="u42"))
# Streaming
async with support_agent.run_stream("Help me with billing", deps=deps) as stream:
async for chunk in stream.stream():
print(chunk, end="", flush=True)
Installation
pip install pydantic-ai
Best Practices
- result_type — Use Pydantic models for structured output; validated automatically
- Dependency injection — Pass deps (DB, auth, config) via
deps_type; clean, testable architecture
- @agent.tool — Decorate functions as tools; type hints become the schema; docstring becomes description
- Model-agnostic — Works with OpenAI, Anthropic, Gemini, Groq, Mistral, Ollama
- Streaming —
run_stream() for real-time token delivery; structured result available at end
- Testing — Use
TestModel for deterministic testing without API calls
- Logfire integration — Built-in observability via Pydantic Logfire; trace every agent step
- System prompts — Dynamic system prompts via
@agent.system_prompt decorator; context-aware
1---2name: pydantic-ai3description: >-4 You are an expert in PydanticAI, the Python agent framework built by the5 Pydantic team. You help developers create type-safe AI agents with6 structured outputs, dependency injection, tool definitions, streaming, and7 model-agnostic design — leveraging Pydantic for validation and type safety8 throughout the agent lifecycle.9license: Apache-2.010compatibility: ''11metadata:12 source: https://skillmd.com/skills/eliferjunior/pydantic-ai13 author: terminal-skills14 version: 1.0.015 category: AI & Machine Learning16 tags:17 - agent18 - pydantic19 - python20 - type-safe21 - tools22 - structured-output23---2425# PydanticAI — Agent Framework by Pydantic Team2627You are an expert in PydanticAI, the Python agent framework built by the Pydantic team. You help developers create type-safe AI agents with structured outputs, dependency injection, tool definitions, streaming, and model-agnostic design — leveraging Pydantic for validation and type safety throughout the agent lifecycle.2829## Core Capabilities3031```python32from pydantic_ai import Agent33from pydantic import BaseModel3435class CityInfo(BaseModel):36 name: str37 country: str38 population: int39 famous_for: list[str]4041agent = Agent("openai:gpt-4o", result_type=CityInfo,42 system_prompt="You provide accurate city information.")4344result = agent.run_sync("Tell me about Tokyo")45print(result.data) # CityInfo(name='Tokyo', country='Japan', population=13960000, ...)4647# With tools and dependencies48from dataclasses import dataclass4950@dataclass51class Deps:52 db: Database53 user_id: str5455support_agent = Agent("openai:gpt-4o", deps_type=Deps,56 system_prompt="You are a customer support agent.")5758@support_agent.tool59async def get_order(ctx, order_id: str) -> dict:60 """Look up an order by ID."""61 return await ctx.deps.db.orders.find(order_id)6263@support_agent.tool64async def create_ticket(ctx, title: str, priority: str) -> str:65 """Create a support ticket."""66 ticket = await ctx.deps.db.tickets.create(title=title, priority=priority, user_id=ctx.deps.user_id)67 return f"Created ticket {ticket.id}"6869result = await support_agent.run("Where is my order ORD-123?", deps=Deps(db=db, user_id="u42"))7071# Streaming72async with support_agent.run_stream("Help me with billing", deps=deps) as stream:73 async for chunk in stream.stream():74 print(chunk, end="", flush=True)75```7677## Installation7879```bash80pip install pydantic-ai81```8283## Best Practices84851. **result_type** — Use Pydantic models for structured output; validated automatically862. **Dependency injection** — Pass deps (DB, auth, config) via `deps_type`; clean, testable architecture873. **@agent.tool** — Decorate functions as tools; type hints become the schema; docstring becomes description884. **Model-agnostic** — Works with OpenAI, Anthropic, Gemini, Groq, Mistral, Ollama895. **Streaming** — `run_stream()` for real-time token delivery; structured result available at end906. **Testing** — Use `TestModel` for deterministic testing without API calls917. **Logfire integration** — Built-in observability via Pydantic Logfire; trace every agent step928. **System prompts** — Dynamic system prompts via `@agent.system_prompt` decorator; context-aware