ValueError
builtins.ValueError
Stack trace
Traceback (most recent call last):
File "app.py", line 42, in <module>
data = json.loads(llm_response)
File "/usr/lib/python3.9/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.9/json/decoder.py", line 337, in decode
raise ValueError("Expecting value: line 1 column 1 (char 0)") from None
ValueError: Expecting value: line 1 column 1 (char 0) Why it happens
Cerebras models in JSON mode may output extra text, incomplete JSON, or malformed JSON due to prompt ambiguity or model behavior. This causes Python's json.loads() to fail with a ValueError when parsing the response.
Detection
Monitor JSON parsing exceptions (ValueError) when loading LLM output and log the raw response to detect malformed or non-JSON output before it crashes.
Causes & fixes
Cerebras LLM outputs extra preamble or trailing text around JSON
Adjust the prompt to explicitly instruct the model to return only raw JSON without any extra text or formatting.
Model truncation or incomplete JSON output due to token limits
Increase max tokens or chunk the prompt to ensure the full JSON response is generated completely.
Using a Cerebras model version that does not fully support strict JSON mode
Upgrade to the latest Cerebras model version that supports strict JSON output or use a post-processing step to clean the output.
Code: broken vs fixed
import os
import json
from cerebras import CerebrasClient
client = CerebrasClient(api_key=os.environ['CEREBRAS_API_KEY'])
response = client.generate(prompt="Return data in JSON mode")
data = json.loads(response) # This line raises ValueError
print(data) import os
import json
from cerebras import CerebrasClient
client = CerebrasClient(api_key=os.environ['CEREBRAS_API_KEY'])
prompt = "Return ONLY raw JSON data without any extra text or formatting."
response = client.generate(prompt=prompt)
try:
data = json.loads(response) # Fixed: added prompt clarity and try/except
except ValueError:
print("Failed to parse JSON. Raw response:", response)
raise
print(data) Workaround
Wrap json.loads() in try/except ValueError, then use regex or string methods to extract JSON substring from the raw response and parse it manually.
Prevention
Use strict prompt engineering to enforce JSON-only output and validate responses immediately after generation; consider upgrading to Cerebras models with improved JSON mode support.