All Guides
Beginner • ~1h10~1h10 reading timeFree Guide

Structured AI Outputs

Learn to constrain response formats to ensure automatic exploitability through strict JSON schemas.

The Parsing Problem

When you ask an AI "extract the customer name and email from this message," you get something like:

The customer name is John Smith and their email is john@example.com.

That is easy for a human to read, but how does your code extract "John Smith" from that sentence? You need regex, NLP, or another AI call. Every intermediary step is a failure point.

Structured vs Unstructured: A Direct Comparison

Output Format Comparison

FormatHuman ReadableMachine ParseableError RateBest Use Case
Plain TextExcellentPoor34%Conversations, explanations
MarkdownExcellentFair18%Documentation, reports
CSVGoodGood10%Tabular data, spreadsheets
JSONFairExcellent2-8%APIs, databases, pipelines
JSON + SchemaFairExcellent2%Production systems

← Scroll horizontally →

How to Request Structured Output

The key is to be explicit about the FORMAT, not just the TASK. Here are the three levels of structure.

From Unstructured to Fully Structured

Three levels of output control

Level 1, Natural Language Request

The weakest approach. You ask for data but do not specify format. The model picks its own, usually prose.
code
Extract the product details from this description:

'The Sony WH-1000XM5 wireless headphones cost $349 and feature 30-hour battery life with ANC.'

💡 The model will respond in prose: 'The product is Sony WH-1000XM5, priced at $349...'

1/3
Quick Check

Why is providing a JSON schema better than just saying 'respond in JSON'?

Real-World Applications

Structured outputs are not just for developers. They power everyday workflows.

Advanced Techniques

Click to expand

Common Pitfalls

  1. →Asking for JSON but accepting prose, Always validate the output is valid JSON before parsing.
  2. →Inconsistent key naming, Use snake_case consistently. Provide the schema every time.
  3. →No error handling, Models occasionally produce invalid JSON. Wrap parsing in try/catch.
  4. →Over-complex schemas, 50-field schemas confuse models. Split into multiple focused extractions.
  5. →Forgetting "no markdown fences", Models often wrap JSON in json.... Add "Return ONLY raw JSON" to your prompt.
Click to expand

Test Your Understanding

Structured AI Outputs Knowledge Check

0 / 5 answered

Q1.What is the main advantage of structured AI outputs over plain text?

5 questions remaining

Continue Learning

You now understand WHY structured outputs matter. In the next article, you will learn the specific TECHNIQUES to get reliable JSON from any LLM, including validation, retry strategies, and error handling.


Continue to Reliable JSON Output from LLMs for production-grade extraction techniques.


The Reliability Stack

There are five layers to reliable JSON extraction. Each layer catches failures the previous one misses.

The 5-Layer Reliability Stack

From prompt design to production validation

Layer 1, Prompt Engineering

The foundation. Your prompt must include the exact JSON schema, clear instructions to return ONLY JSON, and a statement about handling missing data (null, not omit).
code
Extract info into this exact JSON. Use null for missing fields. Return ONLY raw JSON, no markdown fences, no explanation.

Schema:
{"name": "string", "age": "number|null", "email": "string|null"}

💡 Explicit schema + null handling + 'ONLY JSON' prevents 80% of failures.

1/5

Techniques by Model

JSON Extraction Techniques by Provider

ProviderNative JSON ModeSchema EnforcementBest Practice
OpenAI (GPT-5.4)response_format: json_objectStructured Outputs (strict)Use Structured Outputs for critical fields
Anthropic (Claude)tool_use with input_schemaModerate enforcementCombine tool_use with prompt-level schema
Google (Gemini)responseSchema parameterStrong enforcementDefine schema in API config + prompt
Open Source (Llama)Grammar-based (llama.cpp)Strong with grammarUse GBNF grammar for guaranteed structure

← Scroll horizontally →

Quick Check

What does JSON mode guarantee?

The Schema-First Approach

Error Recovery Patterns

Click to expand

Advanced: Handling Edge Cases

Click to expand

Test Your Understanding

Reliable JSON Extraction Check

0 / 5 answered

Q1.What are the 5 layers of the JSON reliability stack?

5 questions remaining

Next Steps

You now know how to get reliable JSON from any LLM. In the next workshop, you will put it all together, building a full CV extractor that takes resume text and outputs structured candidate data.


Continue to the workshop: AI CV Extractor Workshop to build a real structured extraction pipeline.


The Workshop Goal

By the end of this workshop, you will have a prompt pipeline that:

  1. →Takes raw CV/resume text as input
  2. →Extracts structured candidate data into a predefined JSON schema
  3. →Handles edge cases (missing data, ambiguous entries, multiple roles)
  4. →Validates output and retries on failure

Step 1: Define Your Schema

Before writing any prompt, define the exact output structure you need.

Designing the CV Schema

From requirements to JSON structure

Identify Required Fields

Start with what the HR team actually needs. Do not extract everything, focus on fields that drive decisions.
code
Required: name, email, phone, location
Experience: company, role, duration, key achievements
Education: institution, degree, year
Skills: technical skills, soft skills, languages

💡 Start with the minimum viable schema. Add fields only when needed.

1/3

Step 2: Build the Extraction Prompt

Click to expand

Step 3: Test with Real Examples

Quick Check

Why should the schema use 'string|null' instead of just 'string'?

Step 4: Handle Edge Cases

Edge Cases and Solutions

Edge CaseProblemSolution
Missing Contact InfoNo email or phone listedReturn null, flag for manual review
Multiple Roles at One CompanyPromotions within same companyList as separate experience entries with same company name
Non-Standard DatesApproximate dates like "3 years" or "since 2020"Convert to best estimate: "2020-01 to Present"
Mixed LanguagesCV in French with English job titlesExtract as-is, preserve original language
Very Long CVsAcademic CVs with 20+ publicationsLimit to latest 5 roles, note "additional items omitted"

← Scroll horizontally →

Step 5: Validation and Retry

Building the Validation Pipeline

Catch errors before they reach your database

JSON Parse Check

First, verify the output is valid JSON. If the model included markdown fences or extra text, strip them before parsing.
code
try {
  const data = JSON.parse(response);
} catch (e) {
  // Strip markdown fences and retry parse
  const cleaned = response.replace(/```json?\n?/g, '').replace(/```/g, '');
  const data = JSON.parse(cleaned);
}

💡 Always strip markdown fences, models add them even when told not to.

1/4

Limitations

  1. →No cross-referencing, The extractor trusts what the CV says. It cannot verify employment claims.
  2. →Layout dependent, Heavily formatted CVs (columns, tables, graphics) may lose structure when converted to text.
  3. →Bias risk, AI may assign higher confidence to CVs that match patterns in its training data.
  4. →Privacy, Always send CV data through secure, compliant channels. Never log PII in development.
Click to expand

Test Your Understanding

CV Extractor Workshop Check

0 / 5 answered

Q1.What is the first step in building a CV extractor?

5 questions remaining

What's Next

You have built a complete structured extraction pipeline! In the next module, you will learn advanced reasoning techniques, Chain-of-Thought and Self-Consistency, to tackle problems that require multi-step logic.


Continue to Chain-of-Thought and Self-Consistency to master AI reasoning patterns.

Your progress

Progress is self-reported and stored in your browser. The final completion record is not a qualification or an independent assessment.

View progress and completion record

Learn AI, from prompts to agents

15 free guidesQuizzes, diagrams and workshopsNo account needed
GO DEEPER — FREE GUIDE

Chain-of-Thought Reasoning

Master CoT, Tree of Thought, and self-consistency prompting. Free advanced prompt engineering course.

One practical AI workflow in your inbox

Methods, resources and new exercises. Free, unsubscribe at any time.