GeekHub Learn
Module
Lesson 4.55 of 7 in this module2 min read Module 4: Prompt Engineering Fundamentals

JSON mode and structured outputs

If your AI app touches a database, an API, or any non-AI system, you need structured outputs. JSON mode is the single most important production feature you will learn this module.

A handwritten address on an envelope is fine for a postcard. A shipping label needs structured fields. JSON mode is the shipping label.

Modern LLM APIs support:

  1. JSON mode: the model is constrained to output valid JSON.
  2. JSON Schema mode: the model is constrained to a specific schema you provide.
  3. Function/tool calling: the model emits a structured call to a named function with typed arguments.

These are the bridges between fuzzy LLM output and deterministic software.

OpenAI structured outputs example:

from openai import OpenAI
client = OpenAI()

schema = {
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "skills": {"type": "array", "items": {"type": "string"}},
        "experience_years": {"type": "integer"},
    },
    "required": ["name", "skills", "experience_years"],
    "additionalProperties": False,
}

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "Extract resume fields as JSON."},
        {"role": "user", "content": "Aman Sharma, Python and React, 4 years."},
    ],
    response_format={
        "type": "json_schema",
        "json_schema": {"name": "Resume", "schema": schema, "strict": True},
    },
)
print(response.choices[0].message.content)

Returns valid, schema-conformant JSON every time.

Visualize it

Diagram: unstructured text -> LLM -> structured JSON -> database table. Show the "structured layer" as the bridge.

Try it now

In OpenAI Playground or any structured-output-enabled UI, set a JSON schema and watch the model conform.

Hands-on lab

Build a script that takes 5 messy resume blurbs and outputs JSON with name, email, skills, years_experience. Validate with jsonschema in Python.

Try it now

Why is "ask for JSON in the prompt" worse than using JSON Schema mode?

Common mistakes

  • Parsing model output with regex instead of JSON tools
  • Forgetting to handle the rare case the schema is missing fields
  • Building schemas without strict: true

Debugging tip

If you see invalid JSON in production, you are almost certainly not using JSON Schema mode. Switch and the problem usually disappears.

Challenge

Extend the resume parser to also extract education and previous companies. Aim for zero parse errors over 50 test inputs.

Where this shows up

  • Data extraction pipelines
  • Form auto-fill from chat
  • LLMs producing arguments to traditional functions (function calling)
  • Agentic systems that emit structured plans

From the field

In 2026 production, almost every serious LLM workflow uses structured outputs. Free-form text from an LLM into a database is now considered a junior mistake.

Recap

Structured outputs are how LLMs talk to the rest of your software. Learn JSON Schema mode and function calling. Skip them at your peril.


Quick recall

3 prompts · think before you flip

Prompt 1 of 3

Why use JSON Schema mode over a prompt that says "respond in JSON"?

Quiz time

1 question · tap an answer to check it

  1. 1. The most reliable way to get a specific JSON shape from an LLM is

Finished lesson 4.5?

Mark complete to update your module progress and unlock the streak.

Loading