Output Handling
Official scope (Exam Guide v1.0): structured output, response validation, defensive parsing, skepticism toward confident output.
Getting structure
| Mechanism | How | Guarantee |
|---|---|---|
| Structured outputs (JSON) | output_config.format with type: "json_schema" | Response text is valid JSON matching the schema |
| Strict tool use | strict: true on a tool | Tool inputs conform to the schema |
Both can be combined. Prompting for JSON alone asks; it does not guarantee. Prefill is not accepted on newer models.
What is not guaranteed
- Truncation:
max_tokenscan cut JSON off. Checkstop_reasonfirst. - Refusal: a 200 whose text may not match the schema.
- Enum case can vary; compare case-insensitively.
- Unsupported constraints (
minimum/maximum,minLength/maxLength, recursive schemas) are not enforced; validate client-side. Every object needsadditionalProperties: false. - First request with a new schema adds grammar-compile latency; grammars are cached for 24 hours.
Three validation layers (design guidance)
- Syntax and schema (parse, types, required fields).
- Business rules (ranges, cross-field checks, permitted values, ids that exist).
- Semantic / factual checks (evals, spot checks, cross-checks against sources).
Defensive parsing (design guidance)
Parse inside error handling; tolerate unexpected or extra fields where safe; validate types and ranges; never evaluate model text as code; accumulate the full stream before parsing; when validation fails, decide per case: retry with the error fed back (capped), safe default, human review, or fail loudly.
Sanitise before render or execute (design guidance)
Model output is untrusted input to the next component: escape for HTML, parameterise SQL, never pass to a shell or eval without checks.
Skepticism toward confident output (verified mitigations)
Fluent, certain-sounding text is not evidence. Allow "I don't know"; ask for direct quotes or citations and retract claims without support; restrict answers to supplied documents; use best-of-N consistency checks; verify high-stakes claims against a source. Design nullable fields and an "other"/"unclear" enum value so the model is not forced to invent.
Exam traps
- "Structured outputs guarantee correct data."
- Parsing without checking
stop_reason. - Relying on
minimumin the schema to enforce ranges. - Rendering model HTML directly.
- Unbounded validation retries.
References
- Structured outputs — checked Oct 16, 2026
- Define tools (strict) — checked Oct 16, 2026
- Handling stop reasons — checked Oct 16, 2026
- Reduce hallucinations — checked Oct 16, 2026
- Citations — checked Oct 16, 2026
Check your understanding
Valid JSON with a wrong customer id. Which layer missed it?Show answerHide answer
Answer
Business-rule or semantic validation.Structured output ends mid-object. What to check?Show answerHide answer
Answer
stop_reason(max_tokens).Model text inserted straight into a web page. Risk and fix?Show answerHide answer
Answer
Markup/script injection; escape or sanitise.Model states a precise figure with no source. Response?Show answerHide answer
Answer
Ask for a quote or citation, or verify; allow "I don't know".