Pixel Myth
← Field notes
AI

Structured outputs: goodbye broken JSON

AI field note

We deleted 400 lines of JSON-repair code from three projects. That code was pure embarrassment and it's no longer needed.

For years, production code included JSON repair logic to handle model output failures. Language models would generate what appeared to be valid JSON that nonetheless failed to parse. Escaped quotes were handled wrong, numeric values appeared inside string quotes, object structures were malformed. Every project had a version of this brittle code.

We built increasingly sophisticated parsers that attempted five different interpretations before giving up entirely. Linting logic. Autocorrection through heuristics. The code did work but maintenance was a constant burden across multiple projects, wasting developer time and effort across organizations.

Structured output guarantees change that entire dynamic completely. The model generates output that conforms to a schema you specify and control. Either the output validates against that schema correctly or the request fails entirely. No intermediate state of partial correctness exists.

Errors stop being about format issues and start being about content correctness. Either the JSON is correctly structured or the provider had an error. Retry logic becomes simple and predictable without complex conditional branches for format recovery and error handling.

Error handling simplifies to a straightforward pattern: if the output validates then process it normally. If validation fails then that indicates a provider error and you retry with backoff. No complex guessing about what the model meant or intended.

Forcing you to define the output schema first clarifies what the problem actually requires and what success means. You cannot fudge the specification or postpone defining requirements anymore.