JSON Schemas for Model Output
In this article
- Explain why every value an output schema allows is a case the application's code has to handle.
- Turn an instruction's output into a schema with one field per decision, using enums, true/false and null where each fits.
- Spot a combination of fields that should never occur and split it into tagged variants so it no longer fits.
- Cut fields no code acts on, including a confidence number the model writes itself.
- Terms defined here: structured output, output schema, discriminated union.
Overview
A reply written as a few lines of text works until the code reading it meets Billing, or billing / account, or a polite sentence before the answer: cases nobody wrote code for, and nothing in the format rules them out.
Brightline, an invented home-internet provider (its tickets are made up too), runs a ticket sorter that asks a model to put each support ticket in one of four categories, billing, technical, account or other, and to say whether a person must see it now. So far the reply comes back as two or three lines of text: the category, escalate: yes or escalate: no, and one sentence of reason only when yes.
The fix is to agree on the reply's shape in advance and write it down, in four moves: one field per decision the code makes; only values the code can act on; a stated meaning for a missing value; separate shapes where one case has fields the other lacks. One test drives all four: each value the shape allows is a state the code must handle, so the right shape is the smallest in which every real result fits and no impossible one does.
Sign in to keep reading
The rest of this article opens with an account. An account also keeps your progress and quiz results on every device, and lets you follow news for up to five topics.
Still to read in this article
- A reply as named fields
- What a schema states
- Start from the decisions
- Fixed choices: enums and true/false
- Absent, null and required
- One shape per case
- Open text and catch-all text
- The final schema
- Common mistakes
- Sources
- Check yourself, 5 questions