The parser returned without an exception, the JSON matched the schema, and the record was wrong anyway. An invoice's subtotal and tax had traded places, and every type check upstream was satisfied. If you rely on LLM structured output in production, this is the failure that schema validation cannot catch. The model produced well-formed data that means something different from what the source said.
Most teams stop at "does it parse and does it match the schema." That layer is necessary. It only proves that the model followed a grammar. It says nothing about whether the content is true to the input. This post catalogs the failure modes we plan for beyond schema validity, and the invariant-checking layer we run after parsing.
Why LLM structured output fails after the schema passes
JSON mode, constrained decoding and function-calling schemas all reduce syntactic failures. They do nothing for semantic ones. A schema says "amount": number. It cannot say amount is in minor units, or that it should be less than the sum of the line items. The model fills the slots with whatever is statistically plausible, and plausible is not the same as correct.
These are the categories we design around.
Swapped and shifted fields
Two fields with the same type and similar names are an invitation: start_date and end_date, from and to, subtotal and total. When the source text is ambiguous, or the field order in the prompt differs from the order in the document, models swap them. The output is perfectly typed. The failure is also non-deterministic. The same prompt can be right 49 times and wrong on the 50th, so a handful of manual test runs will not surface it.
Plausible-but-hallucinated enums
If you constrain a field to an enum, constrained decoding forces a valid member. That is the problem. When the input does not contain the information, the decoder still has to pick something, and it picks the most likely member, not "unknown." Without an explicit unknown or not_stated option, you have turned "I can't tell" into a confident wrong answer. Unconstrained string fields have the mirror-image failure: the model invents a category like "billing_dispute_escalated" that looks right and matches nothing in your downstream switch statement.
Unit and scale mismatches
Currency in major versus minor units, durations in seconds versus milliseconds, percentages as 0.15 versus 15, weights in kg versus lb. The field is a number either way. The model follows whatever convention the source text used, which may not be the convention in your prompt. A value that is off by a factor of 100 will sail through a number check and corrupt aggregates for weeks before anyone notices.
Echoed placeholders and copied examples
If your prompt includes a sample output, the model will occasionally return the sample. The example values are valid and plausible, so nothing flags them. Using obviously fake example values (and checking for them) is cheap insurance.
Truncated or merged entities
Long lists get silently shortened, or two distinct items get merged into one. The array is valid and just shorter than it should be.
The invariant layer we run after parsing
The design principle is that parsing produces a candidate, not a fact. Between the parser and anything that persists data or triggers a side effect, we run a set of invariant checks that encode what must be true about the domain regardless of what the model said. The shape in most backends is roughly this:
- Parse and schema-validate. Reject malformed output early. This is the cheap layer.
- Cross-field invariants. Pure functions over the parsed object:
start <= end,sum(line_items) == subtotal,subtotal + tax == totalwithin a tolerance,discount <= subtotal. A swapped-field bug usually violates at least one of these. - Range and magnitude checks. Per-field plausible bounds drawn from your own data, not the model's. A quantity of 4,000 on an order form where the historical maximum is 40 gets quarantined, which catches unit-scale errors.
- Grounding checks against the source. For extraction tasks, every extracted string or number should be findable in the input, after normalization. If the model returns an amount that appears nowhere in the document, it was generated, not extracted.
- Enum hygiene. Always include an explicit unknown value, and treat a spike in any single enum member as a signal. A distribution shift in output categories is often the first visible symptom of a prompt or model regression.
- Disposition. A failed invariant has three possible outcomes: retry with the violation fed back as a message, route to human review, or reject. Which one applies depends on the cost of being wrong, not on convenience.
Two implementation notes. First, the checks return structured violations ({rule, field, expected, actual}), not booleans. A retry prompt that says "subtotal plus tax must equal total; you returned 118.00 + 100.00 = 218.00 but total was 100.00" fixes most errors in one pass. Second, the invariants live in plain, deterministic code with ordinary unit tests. They should not be another LLM call. Using a model to judge a model's output is sometimes appropriate, but it should not be the only line of defense for anything with consequences.
Security implications
Semantic validation is also an AI security control, not only a data-quality one. If model output can influence an ID, a permission, a file path or a query, then "valid JSON" is attacker-influenced input, especially when the model has read untrusted text. Treat it like any other untrusted payload in application security: check that referenced IDs exist and belong to the requesting user, allow-list actions, and never let a model-chosen field decide authorization. A prompt-injected document that makes the model emit "role": "admin" will pass your schema perfectly.
What we'd tell a team starting out
Log the raw model output next to the parsed result and the invariant outcomes. Track the violation rate per rule over time, because a rising rate after a model or prompt change is your regression test. Start with the cross-field rules, since they are the cheapest and catch the most. Then add grounding checks wherever the task is extraction.
At orithLabs, we build and audit production systems where LLM output feeds real workflows, and this validation layer is one of the first things we look for when reviewing an existing AI integration. If you are debugging output that looks right but isn't, or want a second set of eyes on where model output crosses a trust boundary in your backend, we are happy to talk it through with the engineers who would do the work.