Urgent.News

What's breaking now, across thousands of outlets.

More in AI

LLM Returns Invalid JSON Once in a While: Structured Outputs vs Validate-and-Retry

TL;DR: If json.loads() on model output fails only occasionally, there are three distinct causes: truncation, stray prose or code fences around the JSON, and schema drift (valid JSON, wrong shape).

  • LLM JSON parsing failures can stem from truncation, extraneous text, or schema mismatches.
  • Structured outputs and typed models help eliminate extraneous text and schema mismatches.
  • Validation-and-retry approach with logging raw responses is recommended for handling failures.

More from Friday 6 November →