Request structured, usable outputs
Define an output contract, use schema-constrained output where available, and parse and validate everything.
- Design an output contract with types, enums and null for unknown
- Extract JSON robustly from a reply
- Validate values, not just shape
When code consumes a model’s output, write an output contract: the fields, their types, allowed values (enums), and what to return when something is unknown (null, never a guess).
- Most APIs now offer structured outputs: you pass a JSON Schema and decoding is constrained to match it - the most reliable option for shape.
- Otherwise, ask for JSON (or XML tags), show an example, and say “respond with only the JSON”.
- Either way, parse and validate in code: structure can be right while values are wrong (an invented date, a category outside your enum).
- On failure, retry with the specific error (“
prioritymust be one of low, medium, high”) or route to a fallback.
Try it
Up to 15 XPWould this reply pass?
The contract is {"category": "billing" | "bug" | "feature", "priority": "low" | "medium" | "high", "due_date": "YYYY-MM-DD" or null}. Sort each reply.
“{"category": "bug", "priority": "high", "due_date": null}”
“{"category": "Bug", "priority": "high", "due_date": null}”
“{"category": "billing", "priority": "urgent", "due_date": "2026-11-01"}”
“Sure! Here’s the JSON: {"category": "feature", "priority": "low", "due_date": null}”
“{"category": "billing", "priority": "medium", "due_date": "next Friday"}”
1import json
2reply = '{"category": "bug", "priority": "high", "due_date": null}'
3ticket = json.loads(reply)
4print(ticket["priority"] in {"low", "medium", "high"}, ticket["due_date"])True None
Key takeaways
Define fields, types, enums and null-for-unknown.
Prefer schema-constrained structured outputs; otherwise extract carefully.
Validate values as well as shape, and retry with the specific error.
Lesson quiz
6 questions · pass with 5 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Practice: write Python
Write Python in the editor and run it against sample inputs. Python runs locally in your browser using a WebAssembly runtime.
Extract JSON from a reply
Replies are separated by lines containing only ===. For each, take the text from the first { to the last } and parse it. Print it with json.dumps(data, sort_keys=True), or error: no JSON found, or error: invalid JSON.
- Four replies
Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.
Validate an output contract
Each line is a JSON object. Check category ∈ {billing, bug, feature}, priority ∈ {low, medium, high}, and due_date is null or a YYYY-MM-DD string. Print valid or invalid: and every problem, comma-separated, in that field order (missing FIELD or bad FIELD).
- Four tickets
Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…