Lesson 09.08 · Phase 09 · Frameworks & SDKs
Pydantic AI typed agents
Pydantic AI treats an agent like a typed function: dependencies go in, a validated object comes out, and anything in between that fails validation goes back to the model as an error it can fix. The final answer is itself a tool call, finalresult, so the same validate-and-retry machinery covers tools and output.
Learning objectives
- Implement dependency injection through a RunContext, so tools get a database or client without globals.
- Deliver structured output through a finalresult tool and validate it field by field.
- Use ModelRetry from tools and output validators to make the model correct itself, with a bounded retry count.
- Enforce a request budget with usage limits, and test it all with a function standing in for the model.
Your refund agent returns JSON your backend parses. Most of the time it's fine. Sometimes the amount is a string, sometimes a field is missing, and once it refunded $120 on a $79 order. Your code either crashes on the bad JSON or, worse, accepts it.
Loading the full lesson…