Lesson 01.07 · Phase 01 · LLM Foundations for Agents

Parsing and validating with schemas

Valid JSON is the start, not the finish. Check the shape, convert to real types, check the rules a schema can't express, and let the model fix what fails.

Learning objectives

  • Write a JSON Schema validator for nested objects and arrays whose errors carry a path, like $.items[1].qty.
  • Convert validated JSON into typed Python dataclasses using type hints (nested classes, lists, Optional, Literal).
  • Check meaning that no schema can express, such as "the total equals the sum of the line items".
  • Run a validate → feed errors back → retry loop, and measure first-try versus after-retry success on a real model.

Lesson 6 got the shape right with schema mode. But your code doesn't want a dict. It wants a Receipt with Items whose qty is an int, and it wants the receipt to make sense: quantities at least 1, prices not negative, and a total that matches the items.

Loading the full lesson…