Skip to module content
Module 10 ยท ~13 min

Structured Output & JSON Mode

When output feeds a machine, shape is everything.

Reading progress
0/5 ยท 0%

The big idea

๐Ÿ’กKey idea
Prose is for humans; JSON is for machines โ€” the moment an AI output needs to be parsed, routed, or mapped by another system, structure stops being optional. Good schemas are small, use enums for anything that drives branching, and explicitly allow "unknown" rather than forcing the model to guess.
Quick check
1 question ยท instant feedback
0/1
  1. When does structured output matter most?

Deep dive

7/7 open

A human reading a summary can tolerate ambiguity, varying phrasing, and inconsistent formatting โ€” a downstream automation step cannot. If an AI step outputs "the customer seems fairly interested" one run and "moderate interest level" the next, any code or no-code logic trying to route on that field breaks or requires increasingly fragile parsing rules.

This is the ambiguity tax: every bit of unstructured phrasing your pipeline has to interpret is a place where automation quietly turns into manual babysitting. Structured output removes the tax by making the shape of the answer as reliable as its content.

JSON (JavaScript Object Notation) is just a simple, textual way to represent structured data: objects are collections of named fields wrapped in curly braces, each field has a name and a value, and values have types โ€” strings, numbers, booleans, lists, or other nested objects. You don't need to write JSON by hand to use this pattern; you need to understand it well enough to design a schema and read the output.

The key mental shift is thinking in fields rather than sentences: instead of asking the model to describe a lead's interest level, you ask for a field called intent with one of a small set of allowed values.

Most modern AI tools support this directly: you can describe a schema in the prompt itself ("respond only with JSON matching this shape..."), or use a dedicated JSON mode / structured-output feature that constrains the model's output to match a schema you define programmatically. The latter is more reliable because it's enforced by the tool, not just requested in the prompt.

Either way, the discipline is the same: define the exact shape you want before you ask, rather than hoping the model infers a sensible structure on its own.

Good schemas are small โ€” a handful of fields, not twenty โ€” with clear, unambiguous names. Wherever a field will drive routing or branching logic, prefer an enum (a fixed list of allowed values like "high|medium|low") over free text, because enums make downstream logic deterministic: a router can branch on exact string matches instead of trying to parse infinite phrasings.

A lead-classifier schema like segment (smb, mid, or enterprise), intent (high, medium, or low), a free-text summary, and a next_action field is a good example โ€” enums where routing needs certainty, free text where nuance matters more than structure.

It's tempting to constrain everything, but rigid schemas with no room for uncertainty create a subtler problem than messy prose: hallucinated compliance. If a field is required and the model doesn't actually know the answer, it will often fill the box anyway rather than leave it empty, because it's been asked to always produce a value in that shape.

The fix is to explicitly design in an "unknown" or "other" option for fields where the model might genuinely lack the information โ€” an invoice extraction schema with vat_rate as a number or the string "unknown" means unreadable invoices route to human review instead of silently getting a guessed rate baked into your data.

Even well-designed JSON output needs defensive handling on the receiving end: strip any markdown code fences the model might wrap around the JSON, validate that required fields are actually present before trusting them, and set sensible defaults for anything genuinely missing rather than letting a downstream step crash.

This validation layer is cheap to build once and saves you from the failure mode where a single malformed response silently corrupts an entire automation run.

In practice this looks like: an AI step in n8n, Zapier, or a similar tool produces JSON output, that JSON gets parsed, and individual fields map directly onto columns in a spreadsheet or fields in another system โ€” no regex, no string-splitting, no fragile parsing logic in between.

This is the same pattern used across most production AI workflows: constrain the model's output shape, validate it, then let deterministic code do the routing. The AI's job is judgment; the schema's job is making that judgment usable by everything downstream.

Quick check
1 question ยท instant feedback
0/1
  1. Enums are the better choice over free text for fields that:

Pitfalls & takeaways

Failure modes

  • Feeding prose AI output directly into an automation that expects consistent fields
  • Designing 20-field schemas with rigid formats for data the model can't reliably provide
  • Not allowing an "unknown" or "other" value, so the model guesses instead of flagging uncertainty
  • Skipping validation and letting malformed JSON break a downstream step silently
  • Using free text for fields that actually drive routing or branching logic

Durable takeaways

  • Structure output the moment it needs to be parsed or routed by a machine, not just read by a human
  • Use enums for anything driving branching logic, and keep schemas small and clearly named
  • Explicitly allow "unknown" fields to prevent hallucinated compliance on data the model can't reliably provide
Quick check
1 question ยท instant feedback
0/1
  1. Allowing an "unknown" value in a schema field prevents:

Do the work

๐Ÿ‹๏ธProve you learned it

Take one automation you run that currently has a prose AI step. Design a schema of six fields or fewer, with at least one enum and at least one field that explicitly allows "unknown," enforce it in the prompt or JSON mode, and remap the downstream steps to read the new fields. Break it on purpose with a weird input and confirm the unknown path routes correctly instead of producing a silent bad guess.

0 chars
Quick check
1 question ยท instant feedback
0/1
  1. Over-constrained schemas with too many rigid required fields tend to cause:

Sources

  • ยท OpenAI Cookbook (cookbook.openai.com โ€” structured outputs)
  • ยท Anthropic docs (docs.claude.com โ€” tool use/JSON)
  • ยท Simon Willison (simonwillison.net) on structured extraction