Structured output you can trust
Notebook· 01.04· 120–150 min· Lesson 13 of 129
The notebook opens in your lab, in a new tab. If your server is stopped, it starts first (a few seconds).
By the end you can
- Define output schemas with Pydantic v2 (descriptions,
Literalenums, nullable fields, validators),TypedDict, and raw JSON Schema, and know what each gives you. - Use
with_structured_outputand choose a method:function_calling,json_schema(strict or best-effort) orjson_mode, per provider and model. - Find each integration's default method in code, and spot where docs and code disagree.
- Handle failures without crashing:
include_raw=True,parsing_error, and a retry that feeds the error back to the model. - Design schemas that work in strict mode (all fields required, nullable instead of optional, closed objects).
Before you start
Lessons 01.01–01.03. Basic Pydantic helps but is taught here.
Python in this lesson
Python guideNew heretypingpydanticClass fields with defaultsPydantic models (BaseModel)TypedDictAnnotated[…]Literal[…]inspect@classmethod and @staticmethod
