Week 1 gave you an agent that returns a string. You can measure it, normalise it, pass it on. One problem: a string can contain anything. How do you build a workflow that depends on the agent returning exactly a name and an email?
I'd parse the string — split on a colon, look for an @ sign. But if the model formats it differently one day, the parser breaks.
result_type solves that. You define a Pydantic model with the exact fields you expect, pass it to Agent(model, result_type=...), and the agent is constrained to return that exact shape — not a paragraph, not a guess, a typed Python object.
But how does the AI know to return a specific shape? Does it read the class definition?
PydanticAI translates your class into a JSON schema and adds it to the model's instruction set. The model must fill the schema — it cannot freestyle. If it can't fill a required field, it fails rather than returning garbage. That's the reliability upgrade Week 2 teaches.
class Contact(BaseModel) + result_type=Contact + .model_dump()result_type=Literal["high","medium","low"] for constrained string outputresult_type=list[str] for action-item extractionGoal: by the end of the week you can make the AI return typed Python objects you can store, compare, and pass to other functions — not strings you have to parse.
Create a free account to get started. Paid plans unlock all tracks.
Week 1 gave you an agent that returns a string. You can measure it, normalise it, pass it on. One problem: a string can contain anything. How do you build a workflow that depends on the agent returning exactly a name and an email?
I'd parse the string — split on a colon, look for an @ sign. But if the model formats it differently one day, the parser breaks.
result_type solves that. You define a Pydantic model with the exact fields you expect, pass it to Agent(model, result_type=...), and the agent is constrained to return that exact shape — not a paragraph, not a guess, a typed Python object.
But how does the AI know to return a specific shape? Does it read the class definition?
PydanticAI translates your class into a JSON schema and adds it to the model's instruction set. The model must fill the schema — it cannot freestyle. If it can't fill a required field, it fails rather than returning garbage. That's the reliability upgrade Week 2 teaches.
class Contact(BaseModel) + result_type=Contact + .model_dump()result_type=Literal["high","medium","low"] for constrained string outputresult_type=list[str] for action-item extractionGoal: by the end of the week you can make the AI return typed Python objects you can store, compare, and pass to other functions — not strings you have to parse.