LearnQuestion bank

About 2% of responses fail JSON parsing in production. Walk through finding and fixing the cause.

Applied LLMTechnicalEasy

Answer it yourself first, out loud or on paper, then compare.

Answer

Model answer, written in a candidate's voice

In short

  • Log and categorise every parse failure: truncation at max tokens, code fences, trailing commentary, bad escapes, schema drift, plain-text refusals.
  • Segment failures by input length, language, model version, prompt/template, and endpoint to find clusters.
  • Fix by cause: constrained decoding or tool use, raise max tokens or shorten output, validate-and-retry with the parser error, explicit refusal handling and fallback.
  • Alert on parse-failure rate by segment, not just the global average.
  • Add real failures to regression tests and replay them after prompt, model, or schema changes.

In full

I would treat the 2% as a signal to find the dominant failure mode, not to patch the parser. First I would capture the raw response, request metadata, parser error, and timestamp for every failure, then bucket them by truncation at max tokens, code fences, trailing commentary, bad escapes, schema drift, and plain-text refusals. Next I would segment by input length, language, model version, prompt/template, and endpoint to see whether failures cluster around long inputs, a specific language, or a model rollout. Then I would fix by cause: use constrained decoding or tool use where possible, raise max tokens or shorten/summarise the output, validate the response and retry with the exact parser error, and handle refusals explicitly with a fallback instead of letting them look like malformed JSON. Finally I would alert on parse-failure rate by segment and add the real failing cases to regression tests so prompt, model, or schema changes cannot silently reintroduce them.

Corrected by an independent AI reviewer.

What a strong answer covers

  • Collect and categorise failures: truncation at max tokens, code fences, trailing commentary, bad escapes, schema drift, plain-text refusals
  • Correlate with input length, language, model version
  • Fix: constrained decoding/tool use, raise max tokens or shorten output, validate-and-retry with the error, explicit refusal handling
  • Alert on parse-failure rate; add failures to regression tests

Common mistakes

  • Silently dropping failed responses
  • an ever-growing regex repair function