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.

Create a free account to save this page, take notes and track your progress. Sign in

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).

Validate JSON from a modelTry it yourself
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 value

In 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?

  1. Yes, valid JSON is always correct
  2. Not yet: validate fields, types and allowed values first
  3. Only if temperature was 0
  4. Only 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.

Was this page helpful?