llm
parsing_error
ai_generated
true
json.decoder.JSONDecodeError: Expecting value when parsing LLM output
ID: llm/json-parse-error
82%Fix Rate
88%Confidence
3Evidence
2023-01-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| any | active | — | — | — |
Root Cause
LLM output is not valid JSON despite being asked for JSON. Markdown code blocks, trailing text, or truncated output.
genericWorkarounds
-
95% success Use structured output / JSON mode if available
OpenAI: response_format={'type': 'json_object'}; Anthropic: tool_use for structured output -
88% success Strip markdown code fences before parsing
text = re.sub(r'^```json\n?|```$', '', text.strip()); data = json.loads(text)
-
82% success Use a lenient JSON parser like json-repair
from json_repair import repair_json; data = json.loads(repair_json(text))
Dead Ends
Common approaches that don't work:
-
Use eval() or ast.literal_eval() to parse LLM output
90% fail
eval() is a security vulnerability; literal_eval fails on non-Python JSON. Use json.loads.
-
Prompt harder with ONLY output JSON and nothing else
68% fail
Prompting alone is unreliable. Models can still add markdown fences or explanatory text.