Pydantic AI · Chapter 22 of 23

Capstone

By the end of this chapter you have built KhetBazaar's farmer assistant in one file: it reads a farmer's message as a typed request, answers policy questions with the rules it used, answers each farmer with tools that act for that farmer only, remembers each farmer, waits for a person before filing a claim, keeps answering when the main model is down, and comes with tests that need no model. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Production
  • 5 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

Pradnya has set the date: from the first of November the assistant answers Nashik's farmers on WhatsApp, at any hour. Ganesh will read every conversation for the first week. Sneha will check every claim it files and every rupee of credit it quotes.

You have built each piece on its own, one chapter at a time. A real assistant is all of them at once, and the hard part is where they meet: which farmer a tool acts for, where a farmer's conversation lives, what waits for Sneha and who resumes it, what happens when the model is down, and how you know it still works after next month's change. This chapter puts them together in assistant.py.

What you build: 5 tasks

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

  1. Task 1

    assistant.py: a farmer's message as a typed request

    Write read_request(message): an agent whose output type is a Pydantic model FarmerRequest with intent (a Literal of the nine intents of messages.jsonl: price, sell, weather, pest, order, claim, payment, complaint, praise), crop (onion, grape, tomato or pomegranate, or None), quantity (a number of quintals, or None) and mandi (Lasalgaon, Pimpalgaon or Nashik, or None). Return the run's output.

    Importing assistant.py must call no model: the check imports it and watches. It reads three farmers' messages and compares your object with what the model answered.

  2. Task 2

    assistant.py: policy answers with sources

    Write answer_policy(question): embed every numbered rule of /workspace/data/policy.md (the lines like 3.2 A claim must be reported...) and the question with lab-embed, find the 3 rules nearest the question (pgvector in Postgres as in chapter 10, or in Python), and have an agent answer from those 3 rules, each sent with its number and text. Return {"answer": the reply's text, "sources": the 3 rules' numbers, like "3.2"}.

    Embed the rules the first time they are needed, not at import. The check asks about reporting hail damage, returning an opened spray and credit for inputs, and compares your sources with the rules your request really sent.

  3. Task 3

    assistant.py: a chat for each farmer, and the claim approval

    Make a module-level agent named farmer_agent with the farmer's id as deps, output text or DeferredToolRequests, instructions naming the farmer from the deps, and four tools: mandi_price(crop, mandi), weather_forecast(taluka), credit_balance() for the farmer in the deps, and file_claim(crop, cause, acres, loss_date) for the farmer in the deps, which needs approval.

    chat(farmer_id, message) runs it with that farmer's conversation so far and returns the reply's text, or, when the run waits, Waiting for approval: and the waiting tools' names. decide(farmer_id, approved) resumes that farmer's waiting run with the decision and returns the reply's text, and raises ValueError when nothing of that farmer's waits. The check chats with three farmers and approves one claim and rejects another.

  4. Task 4

    assistant.py: never down

    Every agent uses the model named by the environment variable KHETBAZAAR_MODEL (lab-chat when it is not set) and falls back to lab-chat-mini when it fails. The check loads your file with KHETBAZAAR_MODEL=lab-chat-down and calls read_request and chat: each must try lab-chat-down first and get its answer from lab-chat-mini.

  5. Task 5

    test_assistant.py: tests for the claim approval

    Write pytest tests for chat and decide that override farmer_agent's model with a FunctionModel (chapter 17), so they send no request to the lab model. The check runs them on your assistant.py and on another correct one (both must pass), then on three copies with one careless change each, each of which must make a test fail: a claim is filed at once, without waiting for approval; a rejected claim is filed anyway; only the last waiting farmer is kept, so a decision lands on whoever waited last.

What you will learn

  • read_request (chapter 2): a farmer's message as a typed object.
  • answer_policy (chapter 10): the nearest policy rules, the answer from them, and their numbers as sources.
  • chat and decide (chapters 3, 4, 6 and 12): one agent, tools for the farmer in the deps, a conversation per farmer, a claim that waits.
  • A backup model (chapter 9) for every agent.
  • Tests with a FunctionModel (chapter 17) for the claim approval.

More in Production

Build "Capstone" 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.