Skip to content
AI Agents3 min read

By · Editorial policy

Valid JSON, Wrong Answer: Validate LLM Output

Validate output at three levels: whether it parses, whether its fields match the expected contract, and whether those fields agree with business rules and source evidence. A well-formed JSON object can still encode a dangerous recommendation.

What is the practical answer?

Validate output at three levels: whether it parses, whether its fields match the expected contract, and whether those fields agree with business rules and source evidence. A well-formed JSON object can still encode a dangerous recommendation. An invoice model returns a string for total where a numeric field is required. That is a structural error. Another returns numeric line items and a numeric total that disagree. That is a business-rule error. A third returns an allowed currency and an internally consistent amount but references a source page where neither appears. That needs evidence or human review. Use a schema with required fields, allowed values, and type constraints to catch the first category. Write deterministic checks for the second category. For source-dependent decisions, require source identifiers or excerpts with enough context to validate the result.

Evidence: JSON Schema constraints and validation · Reddit discussion of recurring production mistakes

  1. Receive structured model outputInput
  2. Check types and required fieldsProcess
  3. Verify rules and evidenceDecision
  4. Release validated resultExternal action
Schema compliance is one checkpoint between model output and a consequential action.

Show three different kinds of failure#

Use a schema with required fields, allowed values, and type constraints to catch the first category. Write deterministic checks for the second category. For source-dependent decisions, require source identifiers or excerpts with enough context to validate the result. Preserve distinctions between missing, unsupported, and unverified claims in the downstream state.

What you findWhat to do
JSON fails structural validationclassify the missing or incompatible field.
Output conflicts with business rulesroute to review or reject the write.
Schema and rules pass but source evidence is absentrequire provenance before relying on the value.

Choose a recovery path for each failure#

A single constrained retry may help with harmless formatting problems. Conflicting totals or missing source evidence should often go to review instead of asking the model repeatedly until it returns a passing-looking value. Store the validation rule and redacted evidence that blocked the action so the next operator can diagnose why.

Make each validator explain what it rejected#

Store categories such as invalid_type, inconsistent_total, and source_not_found. A downstream operator needs to see whether the problem came from parsing, a company policy, or evidence. Include a safe pointer to the source passage and the validator version. Redact payload contents in logs where they contain sensitive data.

Set a retry budget only for failures that a retry can plausibly resolve. A temporary model provider failure differs from a conflict between two source amounts. Send unresolved records to an authorized review path with the original context. Check the destination in your test fixture to confirm a rejected result never mutated external data even if the model call itself returned success.

Verify it with a failure drill#

Feed three fixtures into the same validator: total as text, line items that disagree with total, and a source citation that lacks the extracted value.

  • Verify that each receives a distinct failure reason and none reaches the downstream write. Add a clean fixture and confirm it can proceed.

Sources and further reading

Official references support technical claims; community discussions are used only as problem signals.