High severity intermediate · Fix: 5-10 min

ValueError

builtins.ValueError

What this error means
Cerebras LLMs in JSON mode sometimes return output that is not valid JSON, causing parsing failures in downstream code.

Stack trace

traceback
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)
QUICK FIX
Add explicit prompt instructions to return only raw JSON and validate output with a try/except block around json.loads().

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

1

Cerebras LLM outputs extra preamble or trailing text around JSON

✓ Fix

Adjust the prompt to explicitly instruct the model to return only raw JSON without any extra text or formatting.

2

Model truncation or incomplete JSON output due to token limits

✓ Fix

Increase max tokens or chunk the prompt to ensure the full JSON response is generated completely.

3

Using a Cerebras model version that does not fully support strict JSON mode

✓ Fix

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

Broken - triggers the error
python
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)
Fixed - works correctly
python
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)
Added explicit prompt instructions to ensure the model returns raw JSON and wrapped json.loads() in try/except to catch and log malformed output.

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.

Python 3.9+ · cerebras >=0.1.0 · tested on 0.2.5
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.