Loading
0x50Lesson 6 of 9

Request structured, usable outputs

Define an output contract, use schema-constrained output where available, and parse and validate everything.

20 min 6-question quiz 2 code exercises
By the end of this lesson you can
  • 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 (“priority must be one of low, medium, high”) or route to a fallback.

Try it

Up to 15 XP

Would this reply pass?

The contract is {"category": "billing" | "bug" | "feature", "priority": "low" | "medium" | "high", "due_date": "YYYY-MM-DD" or null}. Sort each reply.

0 of 5 sortedScore 0/0
  • “{"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"}”

parse.py
1import json
2reply = '{"category": "bug", "priority": "high", "due_date": null}'
3ticket = json.loads(reply)
4print(ticket["priority"] in {"low", "medium", "high"}, ticket["due_date"])
Output
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.

Exercise 1

Extract JSON from a reply

+25 XP

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
main.py
Loading editor…

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.

Exercise 2

Validate an output contract

+25 XP

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
main.py
Loading editor…

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…