Agents from scratch · Chapter 7 of 18

Validating arguments

By the end of this chapter you can check every tool call before it runs: one Pydantic model per tool, rules written once and shared, an error that names every wrong field, and a loop in which the model reads that error and corrects itself. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Agents that hold up
  • 4 tasks
  • Python
  • In the all labs plan

The first 2 chapters of Agents from scratch are free. This one comes with the all labs plan.

Why you need this

In chapter 5 you made the loop survive bad calls: a wrong keyword became an error result instead of a crash. But run_tool still runs whatever GharFix's function will take, and GharFix's functions were written to be forgiving with people typing into a form. Meenakshi found what that means on a Monday morning: a booking whose phone number the technician could not call, made from a call nobody had checked. Watch how much book_slot accepts:

>>> gharfix.book_slot("T18", "2026-10-24", "evening", "fan-repair", "Hebbal",
...                   "Sana Desai", "+91 96576-74999")["booking_id"]
'GF100001'

It took the last ten digits and booked. That is kind to a person; it is risky for an agent, whose arguments are text a model guessed. A tool call is the model's best reading of a message, and it can be a wrong key, a word where a number goes, a date in the wrong shape, a slot GharFix does not have. Vikram's rule for the team: never run a call you have not validated.

What you build: 4 tasks

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

  1. Task 1

    tool_args.py: a Pydantic model per tool

    Write one Pydantic model for the arguments of each of GharFix's eight tools, with the same field names as gharfix.TOOLS, and a dictionary ARGS from each tool's name to its model.

    Every model refuses keys it does not have and needs every required argument. All values are text, as GharFix's functions expect them, and a number where text belongs is refused. Besides that:

    service is one of plumbing, electrical, carpentry or ac; slot is one of morning, afternoon or evening; a date is a real day written YYYY-MM-DD (2026-02-30 is refused); technician_id is T and two digits; booking_id is GF and six digits; phone is exactly 10 digits, the first from 6 to 9. find_technicians's date and slot may be left out.

    The check validates good arguments for every tool, and arguments with each of these problems.

  2. Task 2

    validated_call.py: validate, then run

    Write validated_call(name, arguments) in validated_call.py: name is the tool the model asked for and arguments the JSON text it wrote. Validate the text with the tool's model from ARGS (import it from tool_args.py) before anything runs. When it is valid, run GharFix's function with the validated values and return the result as JSON text; a function's own error result is returned unchanged.

    Otherwise return JSON text of an object with an error key, and run nothing: for a name that is not a tool, a sentence naming it; for arguments that fail validation (text that is not JSON included), one sentence that names every field that failed and gives Pydantic's message for it, so the model can correct all of them at once. It never raises.

    The check calls it with good calls, a call GharFix itself refuses, an unknown tool, text that is not JSON, the confused model's wrong keyword, and a booking whose phone has a space in it.

  3. Task 3

    confused.py: the error the model reads

    Run Meenakshi's job J02 through the loop with the model lab-chat-confused, a system message (GharFix's booking assistant; it must say today is 2026-10-22), GharFix's tools, and your validated_call for every call, exactly:

    Book a plumber in Bellandur 26 Oct morning: the bathroom drain is blocked. I am Nikhil Prasad, phone 9962962975.

    For every call, print one line as it runs: the tool's name, a colon and a space, then ok, or the text of the result's error. When the model answers, print the answer, then Requests: and the number of requests the run took. Never send more than 10.

  4. Task 4 · interview question

    Interview: fix_validator.py, a slot that slips through

    fix_validator.py validates book_slot and reschedule_booking calls, and lets customers' own words for a slot through (shaam is evening, subah is morning). Meenakshi found a morning booking for a customer who had asked for raat, at night: GharFix has no night slot, so that call should never have run.

    Fix it so that a slot that is not one of the words in SLOT_WORDS (in any case, with spaces around it) is refused: validated_call returns an error that names the slot field and says what it got, and books or moves nothing. The customers' words must keep working, and a refused call's error must say which field failed and why, for any field.

    Keep validated_call(name, arguments), SLOT_WORDS and the two models' fields.

What you will learn

  • A Pydantic model per tool: the contract every call must meet.
  • Rules written once (a date, a slot, a booking ID) and shared.
  • Keeping values in the shape the function expects.
  • The error the model reads: every problem, by field, in one result.

More in Agents that hold up

Build "Validating arguments" yourself

GharFix, a Bengaluru home-services company, needs an assistant that books, reschedules and refunds safely. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Agents from scratch are free. This one comes with the all labs plan.