g↗genai/ learner
Prompting and Working with AI / Lesson 5

Requesting structured outputs

Ask an AI model for tables or labeled fields so replies are easy to scan or reuse—and check both the shape and the facts before you rely on them.

Concept + interactive labBeginner friendly

Ask for a shape, then check it.

In Lesson 3.1, you practiced giving an AI a clear goal and audience. Often, you also need the answer in a predictable shape—such as a table for a spreadsheet or labeled fields for a tool—instead of a long paragraph.

You will use four terms in this lesson, in order:

  1. Structured output: A reply organized into predictable parts, such as a table or labeled fields, instead of free-flowing prose.
  2. JSON (JavaScript Object Notation): A plain-text format that pairs field names with values inside curly braces { }, commonly used when software reads the reply.
  3. Schema (field rules): Your list of required field names, allowed values, and what to write when a detail is missing.
  4. Validation: Checking a reply against your schema rules—and separately checking its values against your source text— before you use it.

Every example in this lesson starts from the same illustrative event message:

Illustrative source note“Hi! Our neighborhood bike repair clinic is on Saturday, May 16 at Oak Park Shelter. It's free for all ages—no registration needed, just drop by!”

Pick a format that fits who will use the reply.

Think of handing someone a blank sticky note versus a printed form with labeled boxes. Both hold the same information, but labeled boxes make every detail easy to find in the same spot.

A practical way to choose a structured format is to ask who will read the reply next:

  • Table (rows and columns): Typical choice when a person will scan, compare items, or paste into a spreadsheet.
  • JSON (labeled key-value fields): Typical choice when software or an automation tool needs to read specific fields by name.

These roles overlap in practice—people can read short JSON and tools can parse clean tables—so treat this as a practical guideline rather than a strict rule.

Diagram titled Requesting structured outputs: Turn a messy note into a predictable shape. Left to right flow: a blue Messy note card (Bike clinic, May 16, Oak Park, free) points to a lavender Prompt plus field rules card with chips event, date, location, and cost, which branches right into two green output cards: Table for people to scan and JSON for software to read. Bottom strip: Name the format and exact fields you need.
Start from the same source note, then request the format that fits the next step.

Here is the same bike repair clinic note turned into a table for a volunteer and into JSON for a calendar script:

eventdatelocationcost
Bike repair clinicSaturday, May 16Oak Park Shelterfree
{
  "event": "Bike repair clinic",
  "date": "Saturday, May 16",
  "location": "Oak Park Shelter",
  "cost": "free"
}

Outside this example, you might ask for a table to compare three apartments side by side for yourself, while an online store sends an order receipt as JSON so a shipping system can read the address field automatically.

Spell out the fields, values, and missing-data rule.

A paper form works best when it tells you exactly how to fill each box—for instance, “Write Yes, No, or Unknown.” A structured-output prompt needs the same clarity.

Include four details in your schema rules:

  1. Exact field names: Name every key (event, date, location, cost) so the model does not invent synonyms like place.
  2. Allowed values or types: State whether a field is short text or restricted to a fixed list—here, cost must be "free", "paid", or "unknown".
  3. Missing-data rule: Say what to write when a detail is absent (such as "unknown") so the model does not guess.
  4. Output boundary: Ask for only the table or JSON object, with no extra intro or closing sentences.

Vague prompt

Leaves keys and values up to chance

“Turn this event message into JSON so I can save it.”

Clear schema prompt

Names exact keys, values, and boundary

“Extract this note into a single JSON object with keys event, date, location, and cost. Set cost to "free", "paid", or "unknown". Use "unknown" for any missing detail. Return only the JSON object.”

In everyday life, a restaurant allergen card uses the same idea: each ingredient is marked yes, no, or unknown so staff never have to interpret a blank line or a vague note.

Asking for a format does not guarantee it.

A common misconception is that asking for JSON or a table guarantees a valid result. By default, a chat prompt asks a language model to predict text one token (small piece of text) at a time rather than locking it into a rigid form.

Even with a clear schema prompt, a reply can still break in common ways. Check every structured reply in two layers:

  1. Valid JSON syntax: The reply parses cleanly as a JSON object, with no extra intro text, missing quotes, or broken braces.
  2. Exact required fields: Every requested key is present with the exact spelling—no missing location or renamed place.
  3. Allowed values: Every text field is non-empty, and cost is one of "free", "paid", or "unknown".
  4. Source accuracy (separate check): Confirm the values match the original note. Valid JSON only proves the shape, not the facts.
Diagram titled Validating a JSON reply: Check the shape first, then check the facts. Left to right flow: a blue JSON reply card points to a lavender Format checks card listing 1. Valid syntax, 2. Required fields, and 3. Allowed values, which branches right into a green Valid shape card (Check facts next) and a red Format error card (Fix or retry). Bottom strip: Valid JSON proves the shape, not the facts.
Format checks catch broken structure; checking facts against the source is still a separate step.

In developer software, many AI services provide developer interfaces (APIs) with schema-constrained modes—settings that restrict the model's output to match a supplied schema—which strongly reduce syntax and field-name errors. Even then, those modes only enforce the shape; they cannot verify whether the values inside the fields are true.

  • If a reply parses as valid JSON, can I trust the facts inside it?

    No. Valid JSON only means the punctuation and structure follow JSON rules; a model can still copy the wrong date or location.

  • Why give a missing-data rule like “unknown”?

    Without an explicit fallback, a model may guess a plausible value just to fill every required field.

  • Do I need to be a programmer to ask for structured output?

    No. Anyone can ask for a table with named columns or a short list of labeled fields and check them before reusing the result.

Try it: Validate a JSON reply.

Illustrative simulation: these five replies are pre-written examples based on the bike repair clinic note, not live model calls. The validator runs fixed rules for this lesson's schema.

Try the lab in this order:

  1. Start on Clean reply and confirm all three format checks pass.
  2. Select Wrapper text, Renamed field, and Unallowed value to see which specific check fails each time.
  3. Select Wrong fact: notice all three format checks pass, then compare location with the source note.
  4. Edit one broken reply in the text box—for example, remove the intro line in Wrapper text—to watch the checks turn green live, then use Reset lab.

JSON Reply Validator

Source note (for fact check)“Hi! Our neighborhood bike repair clinic is on Saturday, May 16 at Oak Park Shelter. It's free for all ages—no registration needed, just drop by!”

Schema rules: Valid JSON object · Exact keys event, date, location, cost · Non-empty text · cost must be "free", "paid", or "unknown".

  1. 1. Valid JSON syntaxPass

    Parses cleanly as a single JSON object.

  2. 2. Exact required fieldsPass

    All four required fields (event, date, location, cost) are present with exact names.

  3. 3. Allowed valuesPass

    Every field is non-empty text and cost is one of "free", "paid", or "unknown".

Separate human check (not automated): This validator only checks the JSON shape. Compare the values above with the source note—for example, does location (Oak Park Shelter) match Oak Park Shelter?
All 3 format checks passed. The JSON shape is valid—now compare the values against the source note.
✎ Try it yourselfKnowledge check · Practice only

Check your understanding.

You ask an AI model to extract event details from an email into JSON with four required fields (event, date, location, cost), where every field must be non-empty text and cost must be "free", "paid", or "unknown". The reply parses as valid JSON, includes all four required fields with non-empty text values, and sets "cost": "free". What can you conclude?

Take this with you.

  • Choose a table when people will scan the reply, or JSON when software will read labeled fields.
  • Write clear schema rules: exact field names, allowed values, a missing-data rule like "unknown", and an output boundary.
  • Never assume asking guarantees the format: validate syntax, required fields, and allowed values, then check the facts against your source.