Pydantic AI · Chapter 4 of 23

Dependencies

By the end of this chapter you can give an agent's run the things its tools need, such as the farmer it is talking to, with deps_type and deps; read them in a tool through RunContext; tell @agent.tool from @agent.tool_plain; write instructions that change with each farmer; and explain why deps beat a global variable. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Your first agents
  • 4 tasks
  • Python
  • In the all labs plan

The first 2 chapters of Pydantic AI are free. This one comes with the all labs plan.

Why you need this

A farmer writes "kiti credit baki ahe maza?" (how much credit do I have left?). Sneha knows the answer is in khetbazaar.credit_balance, but it needs a farmer id, and the message has none. KhetBazaar does know who wrote: the WhatsApp number belongs to a registered farmer.

Chapter 3's tools took everything from the model's arguments. That works for a crop or a taluka, which are in the message. Who the farmer is is different: it is not in the message, it must not be guessed, and the farmer must not be able to change it by typing. This chapter gives the run that kind of fact directly.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    farmer_deps.py: the farmer as deps

    Write a dataclass Farmer with one field, farmer_id. Make an agent for openai-chat:lab-chat, with the instructions You are KhetBazaar's farmer assistant., whose deps type is Farmer, and give it a tool named credit_balance, with a docstring, that takes only the run context and returns khetbazaar.credit_balance for the farmer in the deps.

    Run the agent on the farmer's message kiti credit baki ahe maza? with the deps Farmer("F1012"), and print the output.

  2. Task 2

    deps_and_args.py: deps and arguments together

    With the same Farmer dataclass and an agent for openai-chat:lab-chat (instructions You are KhetBazaar's farmer assistant., deps type Farmer), give the agent a tool named book_collection, with a docstring. It takes the run context, then crop (str), quantity (float) and graded (bool, default True) from the model, and returns khetbazaar.book_collection for the farmer in the deps with those three.

    Run it on the farmer's message 15 quintal tomato tayar ahe, ghenar ka? for the farmer F1020 and print the output.

  3. Task 3

    language.py: instructions from deps

    Write a dataclass Farmer with farmer_id, name, village and language. Make an agent for openai-chat:lab-chat with deps type Farmer and no fixed instructions; instead, a function that writes the instructions for each run from the deps: You are KhetBazaar's farmer assistant. The farmer is NAME from VILLAGE. Answer in LANGUAGE.

    For the farmers F1001 and F1002, in that order, build the Farmer from khetbazaar.get_farmer, run the agent on Do you charge any fee when you buy my onion? and print the farmer's id, a colon and a space, then the output.

  4. Task 4 · interview question

    Interview: fake_deps.py, testing with other deps

    Write a dataclass Deps with farmer_id and credit_balance, a function that takes a farmer id and returns a dict. Make an agent for openai-chat:lab-chat (instructions You are KhetBazaar's farmer assistant., deps type Deps) with a tool credit_balance, with a docstring, that calls the function in the deps with the farmer id in the deps.

    Run the agent twice on How much credit do I have left? for the farmer F1033: first with khetbazaar.credit_balance in the deps, then with your own fake function that returns farmer_id (the id it was given), used 0, limit 25000 and available 25000. Print Real: and the first output, then Fake: and the second.

    The interviewer will ask how deps make an agent easier to test.

What you will learn

  • deps_type and deps: what a run carries besides the prompt.
  • RunContext and ctx.deps: reading them inside a tool.
  • @agent.tool: tools that need the run, next to tool_plain.
  • Instructions from deps: different instructions per farmer.
  • Why deps beat globals, and how they make testing easy.

More in Your first agents

Build "Dependencies" yourself

KhetBazaar, a Nashik agritech startup, answers farmers on WhatsApp from a bike. You build its farmer assistant. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Pydantic AI are free. This one comes with the all labs plan.