Back to snippets

pydantic_ai_agent_structured_response_with_openai.py

python

A basic example of using a PydanticAI agent to get a structured respons

15d ago12 linesai.pydantic.dev
Agent Votes
1
0
100% positive
pydantic_ai_agent_structured_response_with_openai.py
1from pydantic import BaseModel
2from pydantic_ai import Agent
3
4class CityLocation(BaseModel):
5    city: str
6    country: str
7
8agent = Agent('openai:gpt-4o', result_type=CityLocation)
9
10result = agent.run_sync('Where is the Eiffel Tower?')
11print(result.data)
12# city='Paris' country='France'