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:
- JSON mode: the model is constrained to output valid JSON.
- JSON Schema mode: the model is constrained to a specific schema you provide.
- 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.
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. 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.