You want to extract company name and email from every inbound lead email. If the AI returns prose — 'The contact is John at Acme, john@acme.com' — what do you do with it?
I'd have to parse it manually. Which defeats the purpose.
That's why we use result_type=Contact. You define the schema — the AI must return an object that matches it:
from pydantic import BaseModel
class Contact(BaseModel):
name: str
email: str
result = Agent(model, result_type=Contact).run_sync(text)
return result.output.model_dump()So the LLM literally has to return this exact shape. It's constrained by the Pydantic model?
Constrained and validated. If the model can't extract a required field, it fails instead of returning garbage. Here's the full function:
from pydantic import BaseModel
class Contact(BaseModel):
name: str
email: str
def extract_contact(text: str) -> dict:
result = Agent(model, result_type=Contact).run_sync(text)
data = result.output.model_dump()
print(f"Extracted: {data}")
return dataSo the LLM literally has to return this exact shape. That's the difference between a demo and something I'd trust in production.
Welcome to Week 2. Free-form string output is fine for exploration. Typed records are how you build tools you'd actually put in a workflow.
I can point this at my inbound email list and get a dict with 'name' and 'email' for every message. That's a CRM import in Python.
.model_dump() converts the Pydantic model to a plain Python dict — the form you can serialise to JSON, pass to a database, or use in any downstream function. Always call it before returning.
result_typefrom pydantic import BaseModel
class Contact(BaseModel):
name: str
email: str
result = Agent(model, result_type=Contact).run_sync(text)
return result.output.model_dump()result_type=Contact tells the agent to return a Contact object, not a stringresult.output is now a Contact instance, not a string.model_dump() converts it to a plain Python dictDefine the Pydantic model above the entry-point function in your solution — the validator checks that the class is defined before it's used.
You want to extract company name and email from every inbound lead email. If the AI returns prose — 'The contact is John at Acme, john@acme.com' — what do you do with it?
I'd have to parse it manually. Which defeats the purpose.
That's why we use result_type=Contact. You define the schema — the AI must return an object that matches it:
from pydantic import BaseModel
class Contact(BaseModel):
name: str
email: str
result = Agent(model, result_type=Contact).run_sync(text)
return result.output.model_dump()So the LLM literally has to return this exact shape. It's constrained by the Pydantic model?
Constrained and validated. If the model can't extract a required field, it fails instead of returning garbage. Here's the full function:
from pydantic import BaseModel
class Contact(BaseModel):
name: str
email: str
def extract_contact(text: str) -> dict:
result = Agent(model, result_type=Contact).run_sync(text)
data = result.output.model_dump()
print(f"Extracted: {data}")
return dataSo the LLM literally has to return this exact shape. That's the difference between a demo and something I'd trust in production.
Welcome to Week 2. Free-form string output is fine for exploration. Typed records are how you build tools you'd actually put in a workflow.
I can point this at my inbound email list and get a dict with 'name' and 'email' for every message. That's a CRM import in Python.
.model_dump() converts the Pydantic model to a plain Python dict — the form you can serialise to JSON, pass to a database, or use in any downstream function. Always call it before returning.
result_typefrom pydantic import BaseModel
class Contact(BaseModel):
name: str
email: str
result = Agent(model, result_type=Contact).run_sync(text)
return result.output.model_dump()result_type=Contact tells the agent to return a Contact object, not a stringresult.output is now a Contact instance, not a string.model_dump() converts it to a plain Python dictDefine the Pydantic model above the entry-point function in your solution — the validator checks that the class is defined before it's used.
Create a free account to get started. Paid plans unlock all tracks.