Track 01 · Models & contextLesson 06 of 10

Typed output: schemas, validation and tool calls

The CRM wants JSON, every time, with a safe fallback when it isn't.

What you'll be able to do

What you'll be able to do

01
  • Explain what JSON mode, schema-enforced structured outputs and grammar-constrained decoding each guarantee, and what none of them guarantee.
  • Design a Pydantic model that is the single source of truth for a wire format, generates a strict-compatible JSON Schema, and gives the model a legal way to say "I don't know".
  • Implement the function-calling cycle, including tool_choice, parallel calls and error returns, without letting model output execute anything you did not allowlist.
  • Implement a validation-and-repair loop with a bounded retry budget and a safe fallback.
  • Defend a schema versioning plan to a CRM owner worried the agent will corrupt their data.
Before you start

Before you start

The short version

The short version

A model is an untrusted upstream service that happens to speak English. Hand its prose to a CRM and you have built a parser that fails silently. Structured outputs fix the shape of what comes back: JSON mode guarantees parseable JSON, schema-enforced modes guarantee the JSON matches your schema, and both work by constraining which tokens the model may sample. None of them guarantee the values are right. So you define the contract once in code (a Pydantic model), validate every response against it plus the business rules a schema cannot express, feed errors back for a bounded number of repairs, and route to a human when the budget runs out. Function calling is the same mechanism pointed at your code: the model proposes a call as structured arguments; your code validates, authorises and executes it, then returns the result.

  • Shape is guaranteed, truth is not. Strict mode removes parse errors and missing keys, not wrong answers.
  • The typed model is the source of truth. Generate the schema from it, validate against it, version it.
  • Always give the model a legal "unknown". A schema with no escape hatch forces fabrication.
  • Repair loops are bounded and end at a human queue, never at "write it anyway".
  • The model proposes the call; your code decides whether it runs.

Pro lesson

The rest of this lesson is in Pro.

The mental model, worked example, field note, failure table, exercise and self-check, with the voice tutor.

Pro opens next

Stage 2 · One trustworthy model call
  • 3 lessons, with the voice tutor
  • Halden week 2 · The dirty data pack lands
  • The lab kit, to run Halden weeks on your machine

Also in Pro

  • Every stage and specialisation, in full
  • Every lab and every role project
  • Halden weeks 2–10, on your record
  • Every drill, live in the playground
  • All solutions, plus the server judge
  • Lab Assistant and every Teach with AI