| name | pydantic-ai-agent-creation |
| description | Create PydanticAI agents with type-safe dependencies, structured outputs, and proper configuration. Use when building AI agents, creating chat systems, or integrating LLMs with Pydantic validation. |
Creating PydanticAI Agents
Quick Start
from pydantic_ai import Agent
agent = Agent('openai:gpt-4o')
result = agent.run_sync('Hello!')
print(result.output)
Model Selection
Model strings follow provider:model-name format:
agent = Agent('openai:gpt-4o')
agent = Agent('openai:gpt-4o-mini')
agent = Agent('anthropic:claude-sonnet-4-5')
agent = Agent('anthropic:claude-haiku-4-5')
agent = Agent('google-gla:gemini-2.0-flash')
agent = Agent('google-vertex:gemini-2.0-flash')
Structured Outputs
Use Pydantic models for validated, typed responses:
from pydantic import BaseModel
from pydantic_ai import Agent
class CityInfo(BaseModel):
city: str
country: str
population: int
agent = Agent('openai:gpt-4o', output_type=CityInfo)
result = agent.run_sync('Tell me about Paris')
print(result.output.city)
print(result.output.population)
Agent Configuration
from pydantic_ai import Agent
from pydantic_ai.settings import ModelSettings
agent = Agent(
'openai:gpt-4o',
output_type=MyOutput,
deps_type=MyDeps,
instructions='You are helpful.',
retries=2,
name='my-agent',
model_settings=ModelSettings(
temperature=0.7,
max_tokens=1000
),
end_strategy='early',
)
Running Agents
Three execution methods:
result = await agent.run('prompt', deps=my_deps)
result = agent.run_sync('prompt', deps=my_deps)
async with agent.run_stream('prompt') as response:
async for chunk in response.stream_output():
print(chunk, end='')
Instructions vs System Prompts
agent = Agent(
'openai:gpt-4o',
instructions='You are a helpful assistant. Be concise.'
)
@agent.instructions
def add_context(ctx: RunContext[MyDeps]) -> str:
return f"User ID: {ctx.deps.user_id}"
agent = Agent(
'openai:gpt-4o',
system_prompt=['You are an expert.', 'Always cite sources.']
)
Common Patterns
Parameterized Agent (Type-Safe)
from dataclasses import dataclass
from pydantic_ai import Agent, RunContext
@dataclass
class Deps:
api_key: str
user_id: int
agent: Agent[Deps, str] = Agent(
'openai:gpt-4o',
deps_type=Deps,
)
result = agent.run_sync('Hello', deps=Deps(api_key='...', user_id=123))
No Dependencies (Satisfy Type Checker)
agent: Agent[None, str] = Agent('openai:gpt-4o')
result = agent.run_sync('Hello', deps=None)
Verification gates
Run these in order before depending on an agent in production code:
- Smoke run — Execute
agent.run_sync('Reply with OK.') (or await agent.run(...) in async code). Pass: the call completes without raising and result.output is present.
- Structured output — If you set
output_type, prompt for a response that should satisfy the schema. Pass: result.output is an instance of your Pydantic model; repeated validation failures mean tightening instructions or retries, not adding features yet.
- Dependencies — If you set
deps_type, call run / run_sync with deps= of that type. Pass: the invocation type-checks and completes (or fails only for model/API reasons, not a missing or wrong deps value).
Decision Framework
| Scenario | Configuration |
|---|
| Simple text responses | Agent(model) |
| Structured data extraction | Agent(model, output_type=MyModel) |
| Need external services | Add deps_type=MyDeps |
| Validation retries needed | Increase retries=3 |
| Debugging/monitoring | Set instrument=True |