Even strict mode misses things
Structured-output strict mode guarantees JSON syntactic validity. It does not guarantee that the values are semantically valid: enum mismatch, value out of range, business-rule violations. You still need a validator and a repair loop.
The pattern
- Generate.
- Validate against a Pydantic / Zod / JSON Schema validator with semantic rules.
- If invalid, send the model the validation error and ask for a fix.
- Cap retries — usually 2 or 3.
- If still invalid, return a structured error to the user.
Why this is a prompt issue
Repair loops are cheaper when the prompt explains the validation rules upfront. "reason_codes must come from the enum [low_confidence, missing_evidence, policy_block, ok]; if no enum value applies, use 'ok' with notes" is much cheaper than letting the model discover the rule via a rejection.