Your extract_contact pulls two fields from text. What if you need three — category, urgency, and a one-line summary — all in one call?
I'd add them to the Pydantic model. Same pattern, more fields. Category would be 'billing', 'bug', 'feature', 'general'.
Exactly. Add fields to the model and the agent extracts all of them in one call — not three separate calls:
from pydantic import BaseModel
class Ticket(BaseModel):
category: str
urgency: str
one_line_summary: str
result = Agent(model, result_type=Ticket).run_sync(text)
return result.output.model_dump()The agent extracts category, urgency, and summary all in one pass? Not three agent calls?
One call. The model reads the text once and fills all three fields simultaneously. Three calls would be slower and cost three times the tokens. Multi-field extraction is one of the biggest wins of structured output. Here's the full function:
from pydantic import BaseModel
class Ticket(BaseModel):
category: str
urgency: str
one_line_summary: str
def triage_ticket(text: str) -> dict:
result = Agent(model, result_type=Ticket).run_sync(text)
data = result.output.model_dump()
print(f"Triage: {data}")
return dataA Pydantic model is a CRM record schema the AI fills in. That's a beautiful abstraction.
And once you have the dict, you can route it: high urgency goes to Slack, billing category goes to your finance email, features go to the product backlog. The AI did the triage; you write the routing logic.
30 tickets a day, each triaged to the right queue automatically. That's 45 minutes of morning ops replaced by one function.
You now have the full structured output toolkit: single string, Literal enum, list, two-field model, multi-field model. Week 3 applies these at scale — batch processing across lists of inputs.
from pydantic import BaseModel
class Ticket(BaseModel):
category: str
urgency: str
one_line_summary: str
result = Agent(model, result_type=Ticket).run_sync(text)
return result.output.model_dump()The model fills all fields in one pass. Three separate agent calls would be 3× the cost and 3× the latency.
Once you have the dict, add routing logic:
urgency == 'high' → Slack alertcategory == 'billing' → finance email queuecategory == 'feature' → product backlogThe AI triages; Python routes.
Define the Pydantic model above the entry-point function in your solution. The validator checks that Ticket is defined before triage_ticket uses it.
Your extract_contact pulls two fields from text. What if you need three — category, urgency, and a one-line summary — all in one call?
I'd add them to the Pydantic model. Same pattern, more fields. Category would be 'billing', 'bug', 'feature', 'general'.
Exactly. Add fields to the model and the agent extracts all of them in one call — not three separate calls:
from pydantic import BaseModel
class Ticket(BaseModel):
category: str
urgency: str
one_line_summary: str
result = Agent(model, result_type=Ticket).run_sync(text)
return result.output.model_dump()The agent extracts category, urgency, and summary all in one pass? Not three agent calls?
One call. The model reads the text once and fills all three fields simultaneously. Three calls would be slower and cost three times the tokens. Multi-field extraction is one of the biggest wins of structured output. Here's the full function:
from pydantic import BaseModel
class Ticket(BaseModel):
category: str
urgency: str
one_line_summary: str
def triage_ticket(text: str) -> dict:
result = Agent(model, result_type=Ticket).run_sync(text)
data = result.output.model_dump()
print(f"Triage: {data}")
return dataA Pydantic model is a CRM record schema the AI fills in. That's a beautiful abstraction.
And once you have the dict, you can route it: high urgency goes to Slack, billing category goes to your finance email, features go to the product backlog. The AI did the triage; you write the routing logic.
30 tickets a day, each triaged to the right queue automatically. That's 45 minutes of morning ops replaced by one function.
You now have the full structured output toolkit: single string, Literal enum, list, two-field model, multi-field model. Week 3 applies these at scale — batch processing across lists of inputs.
from pydantic import BaseModel
class Ticket(BaseModel):
category: str
urgency: str
one_line_summary: str
result = Agent(model, result_type=Ticket).run_sync(text)
return result.output.model_dump()The model fills all fields in one pass. Three separate agent calls would be 3× the cost and 3× the latency.
Once you have the dict, add routing logic:
urgency == 'high' → Slack alertcategory == 'billing' → finance email queuecategory == 'feature' → product backlogThe AI triages; Python routes.
Define the Pydantic model above the entry-point function in your solution. The validator checks that Ticket is defined before triage_ticket uses it.
Create a free account to get started. Paid plans unlock all tracks.