Structured Output: JSON & Schemas

Structured output means asking the AI to answer in a predictable, machine-readable shape, most often JSON , instead of a chatty paragraph. When the shape is fixed, your code can read each field reliably.

Learn Structured Output: JSON & Schemas in our free Prompt Engineering course — a beginner-friendly interactive lesson with worked examples, a practice…

Part of the free Prompt Engineering course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

The trick is to ask for JSON, provide a schema or example, and validate what comes back. This lesson shows you how to get clean, parseable output every time.

Freeform prose is hard for a program to read. A fixed JSON shape maps straight to fields. Same goal, two outputs:

Show the model the exact fields and types you expect. A worked example is the simplest schema:

Many AI tools also offer a JSON mode, structured outputs, or a response_format option that constrains the model to valid JSON automatically, which is even more reliable than prose alone.

Never trust output blindly. Check it matches your schema first. If it does not parse, ask the model to fix it:

📋 Copy-paste structured-output template

⏱ Test Yourself — Timed Quiz

10 quick questions, 12 seconds each. Instant feedback — beat the clock!

Practice quiz

What is 'structured output' from an AI?

  • A reply formatted in a predictable, machine-readable shape like JSON
  • A reply with lots of emojis
  • A reply that is always one word
  • A spoken answer

Answer: A reply formatted in a predictable, machine-readable shape like JSON. Structured output means a predictable, parseable shape such as JSON, not freeform prose.

Why ask for JSON instead of freeform text?

  • It looks prettier
  • It is always shorter
  • Your code can reliably parse it into fields and values
  • It avoids all errors forever

Answer: Your code can reliably parse it into fields and values. JSON gives a predictable structure your program can read field by field.

What does providing a schema or example do?

  • Hides the output
  • Shows the AI exactly which fields and types you expect, improving reliability
  • Nothing useful
  • Makes the AI slower

Answer: Shows the AI exactly which fields and types you expect, improving reliability. A schema or worked example pins down the exact shape you want back.

A common phrase to get clean JSON is…

  • 'use big words'
  • 'write a poem'
  • 'be creative'
  • 'Return ONLY valid JSON, no extra text'

Answer: 'Return ONLY valid JSON, no extra text'. Telling the model to return only JSON with no commentary reduces parsing failures.

What is 'JSON mode' or 'response_format' / 'structured outputs'?

  • A feature that constrains the model to emit valid JSON matching your schema
  • A typing game
  • A way to delete data
  • A slower model

Answer: A feature that constrains the model to emit valid JSON matching your schema. These features make the API enforce a JSON shape rather than relying on prose instructions alone.

If parsing the AI's JSON fails, a good strategy is…

  • Give up entirely
  • Validate the output and retry, asking it to fix the JSON
  • Ignore the error
  • Restart your computer

Answer: Validate the output and retry, asking it to fix the JSON. Validate, and on failure retry with a request to correct the malformed JSON.

Why does 'return ONLY JSON' plus a schema beat freeform?

  • It does not
  • It uses fewer letters
  • It is more polite
  • It removes commentary AND pins the exact fields, so parsing is reliable

Answer: It removes commentary AND pins the exact fields, so parsing is reliable. Combining a no-prose instruction with an explicit schema gives the most reliable parse.

Which output is easiest for a program to consume?

  • An audio clip
  • A chatty paragraph describing the user
  • { "name": "Ada", "age": 36 }
  • A hand-drawn diagram

Answer: { "name": "Ada", "age": 36 }. A clean JSON object maps directly to fields your code can read.

Including a worked example in your prompt mainly helps because…

  • It changes the model
  • The model can copy the exact shape, keys, and types you want
  • It is required by law
  • It wastes space

Answer: The model can copy the exact shape, keys, and types you want. A concrete example demonstrates the precise structure you expect.

Validation in this context means…

  • Checking the returned data actually matches the expected schema before you use it
  • Complimenting the AI
  • Translating to another language
  • Deleting the response

Answer: Checking the returned data actually matches the expected schema before you use it. Validation confirms the output has the right fields and types before your code trusts it.