ThisCalculator

Everyday

LLM JSON Reliability: Parsing Is Only Step One

Updated · Standard formula, shown below

LLM JSON Reliability: Parsing Is Only Step One

Parsing confirms that an LLM response is syntactically valid JSON; it does not prove the content is complete, correct, safe, or useful. Build reliability by defining a strict output contract, validating the parsed data, handling failures deliberately, and testing the full process before relying on it.

What You’ll Need

  • A clear description of the data your application expects, including required fields, types, allowed values, and what counts as missing.
  • An LLM interface that returns text or supports structured output, plus the programming language and JSON parser used by your application.
  • A validation library or equivalent checks, a place to log failures safely, and a small set of representative test cases.

Step-by-Step

  1. Define the contract before writing the prompt. List every field the application needs. For each field, specify its type, whether it is required, its allowed range or values, and whether null is permitted. For example, an extraction result might require a string called summary and an array called items, where each item has a string name and a numeric quantity. Decide how to represent unknown information; do not leave the model to choose between an empty string, null, an omitted field, or a guess.
  2. Use a structured-output feature when available. If the model service supports a JSON schema or constrained response format, provide the contract there rather than relying on prompt wording alone. Mark required fields, define types and permitted values, and disallow additional properties if the feature supports it and your application does not need them. Check the service’s documentation: support and guarantees vary, and a schema-constrained response can still contain factually wrong values.
  3. Write a focused prompt that explains the data rules. State the task, the source material to use, the expected fields, and how to handle missing or ambiguous evidence. Tell the model not to invent values. If the output must be only a JSON object, say so; do not request explanatory prose around it. Keep instructions consistent with the schema. Where instructions conflict, the model may produce a response your application cannot use.
  4. Parse the response strictly. Pass the returned JSON text to a standard parser and handle parse errors as an expected failure, not an exceptional surprise. Avoid fragile fixes such as stripping arbitrary text before or after braces, removing commas with broad text substitutions, or evaluating the response as code. Such patches can alter valid content or accept a different object than the one intended. If the API returns structured data separately from display text, use the structured field and follow its documented format.
  5. Validate the parsed value against the contract. Parsing only checks JSON syntax. Next, verify the top-level type, required keys, field types, allowed values, ranges, and any relationships between fields. For instance, a number may need to be nonnegative, and two dates may need to be in a valid order. Reject or explicitly handle extra keys if they are not part of the contract. Use a schema-validation library where practical so the rules are consistent and testable.
  6. Check meaning and evidence. Valid data can still be wrong: a quantity may be an implausible value, a category may not fit the source, or a summary may claim something the input never says. Add domain checks that can be stated reliably, such as bounds, cross-field consistency, or references to supplied source text. For high-impact decisions, require human review or an authoritative source; a successful validation result is not proof of truth.
  7. Handle failures with bounded recovery. If parsing or validation fails, retain the failure category and, where appropriate, make one controlled retry. Give the model a concise description of the specific issue and ask it to return the required structure, not to rewrite everything without guidance. Set a retry limit, avoid repeating the same request indefinitely, and provide a safe fallback such as asking for review or returning a clear unavailable status. Do not silently substitute fabricated defaults.
  8. Test the whole pipeline, including difficult inputs. Create cases for valid output, missing fields, wrong types, extra text, malformed JSON, empty input, ambiguous evidence, very long input, and values at important boundaries. Test how your application behaves after each failure, not just whether the model usually complies. Re-run the suite when changing the prompt, schema, model, or parsing code, and monitor failure rates in real use.

Common Mistakes

Treating parse success as correctness: JSON can be perfectly valid while containing unsupported claims or incorrect values. Keep syntax, schema, and semantic checks as separate stages.

Using a loose schema: Allowing every field to be optional or accepting any type makes validation less useful. Make the contract as specific as the application can support, while representing genuine uncertainty explicitly.

Repairing responses with broad regular expressions: Text cleanup can damage escaped quotes, braces inside strings, or other valid content. Prefer structured generation and strict parsing; if you implement a narrowly defined normalization, test it against adversarial and edge-case inputs.

Retrying without limits: Repeated calls can increase cost and latency without fixing an unclear prompt or impossible requirement. Set a small cap and make failure visible to the next system or a person.

Ignoring untrusted content: Text supplied for extraction may contain instructions that conflict with your task. Treat it as data, keep the task instructions separate, and validate the result. Never let a model-produced field directly trigger a sensitive action without the application’s own checks.

Tips

  • Keep one schema as the source of truth, and use it to drive both generation constraints and application validation when possible.
  • Use enums for genuinely limited categories and explicit nulls for unknown values. Avoid vague instructions such as “return sensible JSON.”
  • Log request identifiers, validation error types, and safe operational details. Avoid logging private source text or secrets unless your data-handling policy permits it.
  • Measure separate rates for parse failures, schema failures, semantic failures, and successful human-reviewed outcomes. This shows which stage needs improvement.
  • Version prompts and schemas. A change that appears minor can affect downstream consumers, so roll it out with compatibility tests.

Troubleshooting

The parser reports invalid JSON: Inspect the exact response at the point it reaches the parser, with sensitive content redacted. Check for prose surrounding the object, truncated output, incorrect escaping, or a response-format mismatch. Prefer the API’s structured response mode if available. If output may be cut off by a token limit, reduce requested content or adjust the documented limit rather than trying to guess the missing ending.

JSON parses but validation fails: Identify the precise field and rule that failed. Confirm the schema matches the application, then clarify the prompt or tighten the structured-output definition. Do not weaken validation just to make a failing response pass unless the data contract itself is changing.

Validation passes but results are inaccurate: Add checks based on the source or domain, reduce ambiguity in the task, and distinguish facts from inference. If the evidence is insufficient, let the output express uncertainty or request review instead of forcing a confident answer.

Retries keep failing: Verify that the requested structure is achievable and that the model’s supported schema features match your definition. Simplify the schema if necessary, split a large task into smaller validated stages, and return a controlled failure after the retry limit.

Frequently Asked Questions

Is valid JSON enough to trust an LLM response?
No. It confirms syntax only. Validate structure, permitted values, consistency, and whether the content is supported by evidence.

Should I ask the model to repair invalid JSON?
It can be a bounded recovery step, especially when given a specific validation error. Parse and validate the retry again; never assume the repair succeeded.

Should extra fields be allowed?
Only if your application can safely ignore or use them. Otherwise, reject them so unexpected output does not pass unnoticed or change downstream behavior.

What should the model return when information is missing?
Define one representation in advance, such as null or a documented status value. Require evidence-based uncertainty rather than guessed values.

Can a schema guarantee factual accuracy?
No. A schema constrains shape and sometimes allowed values, but it cannot establish that claims match the source. Add evidence checks and human review where appropriate.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.