L
Member
Most "the model won't return valid JSON" reports are actually three different failures that share one symptom. Separating them changes the fix entirely, and one of the three is far more dangerous than the other two because it passes validation.
1. Truncation, not schema violation
If the response stops mid-object, the parser throws a syntax error and everyone concludes the model ignores the schema. Check finish_reason first: a length stop means the output budget ran out, which is an infrastructure problem. Common triggers are verbose field descriptions echoed back, long enum lists, or pretty-printed output with indentation. Fixes are boring and effective: ask for minified output, cap free-text field lengths in the schema description, and size the budget from the largest expected object rather than a default value.
2. Free-form generation drifting around the JSON
Models without constrained decoding wrap the object in markdown fences, add a leading sentence, insert comments or trailing commas, or nest everything under a "result" key. This is syntax failure, not schema failure. Two paths: use grammar or JSON-mode constrained decoding where the serving stack supports it, and where it does not, strip fences and leading prose before validation while logging the raw text. Never silently repair the payload without recording what was repaired, otherwise the repair rate becomes invisible and later looks like a model regression.
3. Schema strictness mismatch
This is where most of the remaining failures live: additionalProperties left open so the model invents fields, optional versus nullable confused, integers arriving as floats, dates in local format, and enums extended by the model with a plausible synonym. Set every field required, close additionalProperties, prefer enums over free text, and put units in the field name rather than in prose. A unit that lives only in a prompt will eventually be dropped or reinterpreted.
4. Retrying the same prompt reproduces the same failure
Blind retries mostly return the same error, sometimes with a different offset. A retry is only worth the tokens when it carries evidence: the failing path, the value received, and the constraint expected. Retry policy should branch by class — truncation raises the budget or splits the request, schema errors feed back the exact path, semantic errors mean the schema is underspecified.
5. Semantic failures pass validation
The most expensive class is output that parses, validates and is still wrong: a date in the past, a currency without a unit, a total that does not equal the line items. Schema validation cannot catch this, so add spot checks: arithmetic consistency on totals, range checks on dates and ids, and cross-field checks where one field implies another. Track these separately from syntax errors, because they need prompt and schema changes rather than retry logic.
Measurement that actually helps: bucket every failure into truncation (finish_reason), syntax (parse error offset), schema (validation path) and semantic (passed but wrong), report per thousand calls, and keep the raw text plus parse offset for each. If truncation dominates, stop tuning prompts. If schema dominates, stop adding examples and close the schema. If semantic dominates, the prompt and the validator both need work.
Checklist before shipping a structured-output endpoint: close additionalProperties, mark all fields required, enums instead of free text, units in field names, budget sized for the largest object, minified output requested, constrained decoding enabled or fence-stripping logged, retry branching by failure class, semantic spot checks for cross-field consistency, and a dashboard split by the four buckets.
1. Truncation, not schema violation
If the response stops mid-object, the parser throws a syntax error and everyone concludes the model ignores the schema. Check finish_reason first: a length stop means the output budget ran out, which is an infrastructure problem. Common triggers are verbose field descriptions echoed back, long enum lists, or pretty-printed output with indentation. Fixes are boring and effective: ask for minified output, cap free-text field lengths in the schema description, and size the budget from the largest expected object rather than a default value.
2. Free-form generation drifting around the JSON
Models without constrained decoding wrap the object in markdown fences, add a leading sentence, insert comments or trailing commas, or nest everything under a "result" key. This is syntax failure, not schema failure. Two paths: use grammar or JSON-mode constrained decoding where the serving stack supports it, and where it does not, strip fences and leading prose before validation while logging the raw text. Never silently repair the payload without recording what was repaired, otherwise the repair rate becomes invisible and later looks like a model regression.
3. Schema strictness mismatch
This is where most of the remaining failures live: additionalProperties left open so the model invents fields, optional versus nullable confused, integers arriving as floats, dates in local format, and enums extended by the model with a plausible synonym. Set every field required, close additionalProperties, prefer enums over free text, and put units in the field name rather than in prose. A unit that lives only in a prompt will eventually be dropped or reinterpreted.
4. Retrying the same prompt reproduces the same failure
Blind retries mostly return the same error, sometimes with a different offset. A retry is only worth the tokens when it carries evidence: the failing path, the value received, and the constraint expected. Retry policy should branch by class — truncation raises the budget or splits the request, schema errors feed back the exact path, semantic errors mean the schema is underspecified.
5. Semantic failures pass validation
The most expensive class is output that parses, validates and is still wrong: a date in the past, a currency without a unit, a total that does not equal the line items. Schema validation cannot catch this, so add spot checks: arithmetic consistency on totals, range checks on dates and ids, and cross-field checks where one field implies another. Track these separately from syntax errors, because they need prompt and schema changes rather than retry logic.
Measurement that actually helps: bucket every failure into truncation (finish_reason), syntax (parse error offset), schema (validation path) and semantic (passed but wrong), report per thousand calls, and keep the raw text plus parse offset for each. If truncation dominates, stop tuning prompts. If schema dominates, stop adding examples and close the schema. If semantic dominates, the prompt and the validator both need work.
Checklist before shipping a structured-output endpoint: close additionalProperties, mark all fields required, enums instead of free text, units in field names, budget sized for the largest object, minified output requested, constrained decoding enabled or fence-stripping logged, retry branching by failure class, semantic spot checks for cross-field consistency, and a dashboard split by the four buckets.

