Skip to main content
NewEpisode 3a is out: Carry the conversation onWatch →

Free with an account

This part is free. Sign in or create an account to watch it.

Sign up
Tools · 2b

Read email: what a tool needs

Tools often need things the model has nothing to do with: an address, a password, a client. You build them once at start into Deps and hand it to agent.run; Pydantic AI gives it to every tool that asks through RunContext, and the model never sees it. The opener shows two things already wrong in 2a: its tests swap a function inside bot.py, and a bot started without the calendar address spends a model request before the tool raises KeyError. Then eight stops follow one real question, "Did the dentist's office email me this week?": the new code (Deps, a read-only Mailbox, search_email) → @agent.tool registers with takes_ctx, so ctx is left out of the schema → agent.run keeps deps as user_deps → Gemini calls search_email with an empty text → build_run_context builds the RunContext and a plain def runs in a worker thread → every recorded IMAP command: EXAMINE, SEARCH, BODY.PEEK, and the email stays unread → the 7,029 characters sent to Gemini hold no password, no calendar address, no email address of yours → the reply. Then three tool calls in one answer: the same deps, all started within a millisecond. The email comes from one label in your own Gmail, read-only, with an app password.

Source code shown in the videos is quoted from pydantic/pydantic-ai (MIT licence), pinned at v2.71.0. This course is not affiliated with or endorsed by Pydantic; the explanations and assignments are original. Course code and assignments · pydantic/pydantic-ai at v2.71.0 · License

Up next2c · Search the web→