DEV Community

Cover image for Structured Output Is the Most Underrated AI Reliability Fix
sagar jain
sagar jain

Posted on

Structured Output Is the Most Underrated AI Reliability Fix

If your AI feature does anything besides show text to a human, make the model return a schema-validated structure and treat a validation failure as a normal, counted error path. This one change removes a whole family of production incidents: parsing failures, invented field names, the helpful preamble ("Sure! Here's the JSON:"), and the silent default that kicks in when a regex finds nothing.

Why does free text fail quietly?

Because the failure doesn't look like a failure. The pipeline gets a string, the regex finds nothing or finds the wrong thing, a variable ends up null, and the null flows into a default branch that was written for a different reason. No exception and no alert, just a log line that looks like every other log line.

The demo never shows this. In a demo the operator reads the output, so a stray sentence is charming. In production nobody reads the output; a machine does, and that is the exact reason a working demo is the problem: the same output that impressed a room breaks a parser.

A concrete one from our side. An internal routing step classified incoming requests into one of eight buckets. About three percent of responses came back with a trailing remark after the label ("Category: billing. Note that this could also be..."), our parser took the whole line as the label, matched nothing, and the request landed in the catch-all bucket. Catch-all was a legitimate destination, so nothing complained. It ran that way for weeks.

What the model does Free text plus a regex Schema-validated output
Adds a polite preamble Parse misses, field goes null Rejected, retried with the error
Invents a ninth category Lands in the catch-all bucket Enum violation, counted
Drops a required field A default fires downstream Validation error you can alert on
Behaves differently after an upgrade Nobody notices for weeks Failure rate moves the same day

What does "structured" mean in practice?

Structured output means the model returns data conforming to a schema you defined in code, and your application validates it before using it. In practice that is a Pydantic, Zod or JSON Schema definition, the provider's structured-output mode where one exists, and a validation step that runs on every single call.

The details that matter:

  • Use enums for closed sets. Eight categories means an enum with eight values plus one explicit unable_to_determine. Never let the model invent a ninth.
  • Keep schemas shallow. Deep nesting invites partial objects.
  • If you want reasoning, give it a field, and put that field before the answer fields. Order affects quality because the model generates top to bottom.
  • Mark as optional only what is genuinely optional. "Optional because the model sometimes forgets it" is a bug you're hiding from yourself.

The validation step is where the reliability comes from. The model can still be wrong, but now it's wrong inside a shape you can inspect and count.

How should validation failures behave?

A validation failure should behave like any other error in your system: retried once with the error fed back, then failed over to a deterministic default or a human queue, counted in a metric, and alerted on when the rate crosses a threshold. What it must never do is coerce silently.

The sequence we implement every time:

  1. Retry once, feeding the validation message back to the model ("field priority must be one of low, medium, high").
  2. On a second failure, fall back to a deterministic default or route the item to a human queue.
  3. Increment a counter tagged with the model id and the prompt version.
  4. Alert when the failure rate crosses something like one percent.

That last threshold is your earliest signal that a model update changed behaviour, which is worth more than the retry itself.

What about features where text is the product?

Wrap them anyway. Return {answer, citations[], refusal_reason} instead of a bare string, and the output becomes testable: citations exist when they should, refusals happen for the right reasons, the length stays inside a bound, and the same input returns the same shape twice. Every field is addressable, which makes logging and evals easier later.

We do this on every AI build at Shanti Infosoft, including the ones where the client insists the output is "just text," because six months in someone always wants to route on it or filter it. It's a standing rule on the AI development work we take on for exactly that reason. A schema on day one costs an hour. Retrofitting one after the regexes have spread through the codebase costs a sprint.

Which of your AI outputs is still being parsed with a regex, and what happens on the day the model adds one polite sentence?

Sagar Jain is the technical co-founder of Shanti Infosoft, where 80+ engineers build AI features that other people's systems have to consume without breaking.

Top comments (0)