Pydantic Model
Simple example of using Pydantic AI to construct a Pydantic model from a text input.
Demonstrates:
With dependencies installed and environment variables set, run:
python -m pydantic_ai_examples.pydantic_model
uv run -m pydantic_ai_examples.pydantic_model
This example uses an OpenAI model by default. To run it with Gemini, set PYDANTIC_AI_MODEL to a model ID with the google: prefix.
PYDANTIC_AI_MODEL="google:gemini-3.8-flash" python -m pydantic_ai_examples.pydantic_model
PYDANTIC_AI_MODEL="google:gemini-3.8-flash" uv run -m pydantic_ai_examples.pydantic_model
import os
import logfire
from pydantic import BaseModel
from pydantic_ai import Agent
from pydantic_ai.capabilities import Caching
# 'if-token-present' means nothing will be sent (and the example will work) if you don't have logfire configured
logfire.configure(send_to_logfire='if-token-present')
logfire.instrument_pydantic_ai()
class MyModel(BaseModel):
city: str
country: str
model = os.getenv('PYDANTIC_AI_MODEL', 'openai:gpt-5.2')
print(f'Using model: {model}')
agent = Agent(model, output_type=MyModel, capabilities=[Caching()])
if __name__ == '__main__':
result = agent.run_sync('The windy city in the US of A.')
print(result.output)
print(result.usage)