AI Engineering Tutorial · Prompting · Lesson 10 of 25
Structured Output and JSON Validation
Get JSON your code can use, and validate it, because model output is untrusted input.
Ask for structure
When code consumes the result, ask for JSON with a defined schema. Many APIs can enforce a JSON schema ("structured outputs") or return arguments through tool calls, which is more reliable than asking in prose.
Always validate
Even with structured outputs, validate before use: required fields, types, allowed values, ranges. On failure, retry once with the error message, then fall back (default value or human review).
import json
ALLOWED = {"sentiment": {"positive", "negative", "mixed"}}
def parse(raw):
try:
data = json.loads(raw)
except json.JSONDecodeError as e:
return None, f"invalid JSON: {e.msg}"
if data.get("sentiment") not in ALLOWED["sentiment"]:
return None, f"sentiment must be one of {sorted(ALLOWED['sentiment'])}"
if not isinstance(data.get("score"), (int, float)) or not 0 <= data["score"] <= 1:
return None, "score must be a number between 0 and 1"
return data, None
for raw in ['{"sentiment": "positive", "score": 0.92}', '{"sentiment": "great", "score": 2}', "Sure! Here you go"]:
data, error = parse(raw)
print(data if data else f"retry with: {error}")
Output
{'sentiment': 'positive', 'score': 0.92}
retry with: sentiment must be one of ['mixed', 'negative', 'positive']
retry with: invalid JSON: Expecting valueIn real projects use a schema library such as Pydantic (Python) or Zod (TypeScript). See Structured outputs.
Test yourself
A model returns JSON that passes json.loads. Is it safe to use?
Yes, valid JSON is always correctNot yet: validate fields, types and allowed values firstOnly if temperature was 0Only for small outputs
Show answer
Answer: Not yet: validate fields, types and allowed values first
Syntactically valid JSON can still have missing fields or wrong values.