SESSION 5
Session 5 — Structured Outputs & Function Calling
Force the model into JSON, call tools, and orchestrate multi-tool flows.
3 hours•20 exercises · 3 phases
What you'll be able to do by the end
- ✓ Force any model into exact JSON shapes with strict JSON Schema
- ✓ Define and invoke Python functions as tools from a chat conversation
- ✓ Build a tool-calling loop that handles multi-step reasoning and parallel tools
- ✓ Catch tool errors and refusals gracefully
- ✓ Extract typed Pydantic objects from unstructured text with parse()
- ✓ Know when to use response_format vs tools vs parse()
Prerequisites — tick these off first
Finished Session 1 — or at least H1, H3, H5Session5 folder has its own .venv, requirements installed, .env copiedComfortable reading Python dicts and list/dict comprehensionsOPENAI_API_KEY set in Session5/.env
The 3-phase arc
Phase 1 tightens the output shape. Phase 2 teaches the model to call your code. Phase 3 modernises it all with Pydantic. Do them in order the first time.
Phase 1
Schema
Phase 2
Tools
Phase 3
Pydantic
Exercises by phase
Phase 1 — Output Formatting & Safety
From plain text → JSON → strict schema → safe parsing.
01
Plain Response Baseline
10 min
02
JSON Mode Basics
10 min
03
Schema Output — force a specific shape
15 min
04
Strict Required Fields
10 min
05
Parse & Validate Structured Output
10 min
06
Refusal Detection
10 min
07
Incomplete Output Handling
10 min
08
Fixing Schema Mistakes
10 min
09
Enum & Nullable Fields
10 min
10
Format vs Tools — when to use which
10 min
Phase 2 — Function Calling
Teach the model to request your Python functions.
Phase 3 — SDK Parse Methods
Modern Pydantic-based structured output with zero boilerplate.