Day 10's Contact had two fields. What if you need to extract three structured fields from a research feedback message — category, urgency level, and a one-line summary?
Define a Ticket BaseModel with three fields, pass it as result_type. The same pattern as Contact — just more fields. result.output.model_dump() gives me all three as a dict.
Exactly. The Pydantic pattern scales to any number of fields. class Ticket(BaseModel): category: str; urgency: str; one_line_summary: str — the model must fill all three:
def triage_ticket(text: str) -> dict:
class Ticket(BaseModel):
category: str
urgency: str
one_line_summary: str
agent = Agent(model, result_type=Ticket)
result = agent.run_sync(text)
return result.output.model_dump()What values does the model assign to urgency? I didn't specify the options like I did with Literal.
With urgency: str, the model uses its own judgment about what 'urgency' means. You can guide it with a system prompt: system_prompt='urgency must be high, medium, or low'. Or combine patterns: use Literal for constrained fields and str for free-text fields:
def triage_ticket(text: str) -> dict:
class Ticket(BaseModel):
category: str
urgency: str
one_line_summary: str
agent = Agent(model, result_type=Ticket)
result = agent.run_sync(text)
data = result.output.model_dump()
print(f"Triage: {data}")
return dataI can triage every piece of student feedback in my research dataset — category tells me the topic, urgency tells me priority, one-line summary tells me what to read first. That's a complete triage layer in one function.
The pattern is the same at any scale. Two fields, three fields, ten fields — result_type enforces the shape. The work is in choosing field names and types that match what the model can reliably extract from your domain text.
My advisor reviews research feedback manually. I can pre-triage 200 items and send her only the high-urgency ones.
Pre-triage is a legitimate use case — but validate the high-urgency bucket manually before acting on it. Structured output enforces shape, not accuracy. A miscategorised 'high urgency' item that gets missed is worse than no automation.
class Ticket(BaseModel):
category: str
urgency: str
one_line_summary: str
agent = Agent(model, result_type=Ticket)
result = agent.run_sync(text)
return result.output.model_dump()Use Literal for constrained fields, str for free-text fields, int for numeric fields. A single model can mix types: category: str; urgency: Literal["high","medium","low"]; word_count: int.
Structured output enforces shape, not correctness. The model fills all fields — but a miscategorised "high urgency" item that gets actioned is worse than no automation. Always validate a stratified sample.
The pattern is identical at any number of fields. Two fields, three fields, ten fields — result_type enforces the schema.
Day 10's Contact had two fields. What if you need to extract three structured fields from a research feedback message — category, urgency level, and a one-line summary?
Define a Ticket BaseModel with three fields, pass it as result_type. The same pattern as Contact — just more fields. result.output.model_dump() gives me all three as a dict.
Exactly. The Pydantic pattern scales to any number of fields. class Ticket(BaseModel): category: str; urgency: str; one_line_summary: str — the model must fill all three:
def triage_ticket(text: str) -> dict:
class Ticket(BaseModel):
category: str
urgency: str
one_line_summary: str
agent = Agent(model, result_type=Ticket)
result = agent.run_sync(text)
return result.output.model_dump()What values does the model assign to urgency? I didn't specify the options like I did with Literal.
With urgency: str, the model uses its own judgment about what 'urgency' means. You can guide it with a system prompt: system_prompt='urgency must be high, medium, or low'. Or combine patterns: use Literal for constrained fields and str for free-text fields:
def triage_ticket(text: str) -> dict:
class Ticket(BaseModel):
category: str
urgency: str
one_line_summary: str
agent = Agent(model, result_type=Ticket)
result = agent.run_sync(text)
data = result.output.model_dump()
print(f"Triage: {data}")
return dataI can triage every piece of student feedback in my research dataset — category tells me the topic, urgency tells me priority, one-line summary tells me what to read first. That's a complete triage layer in one function.
The pattern is the same at any scale. Two fields, three fields, ten fields — result_type enforces the shape. The work is in choosing field names and types that match what the model can reliably extract from your domain text.
My advisor reviews research feedback manually. I can pre-triage 200 items and send her only the high-urgency ones.
Pre-triage is a legitimate use case — but validate the high-urgency bucket manually before acting on it. Structured output enforces shape, not accuracy. A miscategorised 'high urgency' item that gets missed is worse than no automation.
class Ticket(BaseModel):
category: str
urgency: str
one_line_summary: str
agent = Agent(model, result_type=Ticket)
result = agent.run_sync(text)
return result.output.model_dump()Use Literal for constrained fields, str for free-text fields, int for numeric fields. A single model can mix types: category: str; urgency: Literal["high","medium","low"]; word_count: int.
Structured output enforces shape, not correctness. The model fills all fields — but a miscategorised "high urgency" item that gets actioned is worse than no automation. Always validate a stratified sample.
The pattern is identical at any number of fields. Two fields, three fields, ten fields — result_type enforces the schema.
Create a free account to get started. Paid plans unlock all tracks.