Fix JSON parse error from LLM response
model_behavior Why this happens
LLMs often generate text that looks like JSON but may include trailing commas, missing quotes, or extra explanations, causing json.JSONDecodeError in Python. For example, a prompt asking for JSON output without strict formatting instructions can produce invalid JSON:
{
"name": "Alice",
"age": 30,
}This trailing comma breaks JSON parsing. The error message typically is:
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 4 column 1 (char 45)
from openai import OpenAI
import os
import json
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
response = client.chat.completions.create(
model="gpt-4.1-mini",
messages=[{"role": "user", "content": "Give me a JSON object with name and age."}]
)
text = response.choices[0].message.content
# This will raise JSONDecodeError if text is malformed
parsed = json.loads(text) json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 4 column 1 (char 45)
The fix
Fix this by instructing the model explicitly to output only valid JSON without extra text and by parsing with error handling. Use a system prompt like "Respond only with valid JSON." and strip whitespace before parsing. Example code:
from openai import OpenAI
import os
import json
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
system_prompt = "You are a helpful assistant. Respond only with valid JSON, no explanations."
user_prompt = "Give me a JSON object with name and age."
response = client.chat.completions.create(
model="gpt-4.1-mini",
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
]
)
text = response.choices[0].message.content.strip()
try:
parsed = json.loads(text)
except json.JSONDecodeError as e:
print(f"JSON parse error: {e}")
# Optionally retry or clean text here
else:
print(parsed) {'name': 'Alice', 'age': 30} Preventing it in production
Implement retries with exponential backoff on parse failures, validate JSON schema after parsing, and consider using JSON schema validation libraries. Also, sanitize or extract JSON from noisy responses using regex or specialized parsers if needed. Logging malformed outputs helps diagnose prompt issues.
import time
max_retries = 3
for attempt in range(max_retries):
response = client.chat.completions.create(
model="gpt-4.1-mini",
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
]
)
text = response.choices[0].message.content.strip()
try:
parsed = json.loads(text)
break
except json.JSONDecodeError:
wait = 2 ** attempt
time.sleep(wait)
else:
raise ValueError("Failed to parse JSON after retries")
print(parsed) {'name': 'Alice', 'age': 30} Key Takeaways
- Always instruct LLMs explicitly to output valid JSON only to reduce parse errors.
- Wrap JSON parsing in try-except blocks to handle malformed responses gracefully.
- Implement retries with backoff and validate JSON schema in production AI apps.