Why Large Language Models Generate Malformed and Broken JSON
Large Language Models generate responses token-by-token using probabilistic next-token prediction rather than deterministic syntax trees. LLMs frequently commit syntax errors such as wrapping keys in single quotes, omitting escape slashes before quotation marks within string values, including trailing commas, or injecting Python object representations (such as True, False, or None) instead of standard JSON literals (true, false, null).
// ❌ Broken LLM Output (Markdown fences, single quotes, Python None, trailing comma)
```json
{
'task': 'analyze_metrics',
'confidence': 0.94,
'assigned_agent': None,
'tags': ['rag', 'llm',],
}
```
// ✅ Repaired Strict RFC 8259 JSON
{
"task": "analyze_metrics",
"confidence": 0.94,
"assigned_agent": null,
"tags": ["rag", "llm"]
}