C.W.K.
Stream
Lesson 01 of 10 · published

Structured Outputs — JSON Mode and Beyond

~18 min · outputs, json, structured

Level 0Apprentice
0 XP0/100 lessons0/14 achievements
0/120 XP to next level120 XP to go0% complete

The model can now be forced to obey a schema

In 2023 you asked the model nicely to return JSON and prayed. In 2026 every major provider supports a structured-output mode where the model is constrained at the decoding level — every token sampled is checked against the schema, and tokens that would produce invalid JSON are simply not emitted. Output structure is no longer a prompt instruction; it's a server-side guarantee.

Per-provider features

  • OpenAIresponse_format = {"type": "json_schema", "json_schema": {...}}. Strict mode enforces the schema exactly.
  • Anthropic — JSON mode via tool calling (define a single tool whose input_schema is the desired output, force its use). Native structured-output endpoint exists in newer SDKs.
  • Geminiresponse_mime_type = "application/json" with response_schema.

What the prompt still does

Even with structured outputs enforced, the prompt does the heavy lifting: it explains the meaning of each field, the relationship between fields, and the policy for null/empty values. The schema is the shape; the prompt is the semantics. Don't treat structured output as a replacement for instruction.

Code

OpenAI strict JSON schema·python
client.chat.completions.create(
    model="gpt-5.5",
    messages=[...],
    response_format={
        "type": "json_schema",
        "json_schema": {
            "name": "verdict",
            "strict": True,
            "schema": {
                "type": "object",
                "properties": {
                    "verdict": {"type": "string", "enum": ["approve", "reject", "hold"]},
                    "reason_codes": {"type": "array", "items": {"type": "string"}, "maxItems": 3}
                },
                "required": ["verdict", "reason_codes"],
                "additionalProperties": False
            }
        }
    }
)
Anthropic — tool-as-schema pattern·python
tools = [{
    "name": "emit_verdict",
    "description": "Return the underwriting verdict.",
    "input_schema": {
        "type": "object",
        "properties": {
            "verdict": {"type": "string", "enum": ["approve", "reject", "hold"]},
            "reason_codes": {"type": "array", "items": {"type": "string"}}
        },
        "required": ["verdict", "reason_codes"]
    }
}]

client.messages.create(
    model="claude-opus-4-7",
    tools=tools,
    tool_choice={"type": "tool", "name": "emit_verdict"},
    messages=[...]
)

External links

Exercise

Pick a prompt that returns JSON via 'please return JSON.' Migrate it to a structured-output mode with a strict schema. Run on 50 inputs. Compare parse-failure rates.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.