JSONDecodeError
json.decoder.JSONDecodeError
Stack trace
Traceback (most recent call last):
File "app.py", line 42, in <module>
response = fireworks_ai.call_function(payload)
File "/usr/local/lib/python3.9/site-packages/fireworks_ai/api.py", line 88, in call_function
data = json.loads(response.text) # <-- triggers JSONDecodeError
File "/usr/lib/python3.9/json/decoder.py", line 337, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) Why it happens
Fireworks AI expects the LLM to return a strictly valid JSON string for function calling. If the LLM response includes extra text, markdown fences, or is malformed, the JSON parser fails with JSONDecodeError. This often happens when the prompt or model does not enforce strict JSON output.
Detection
Monitor the JSON parsing step by catching JSONDecodeError exceptions and log the raw LLM response to detect malformed or unexpected JSON before the app crashes.
Causes & fixes
LLM response includes markdown fences or extra text around JSON output
Modify the prompt to instruct the model to return raw JSON only, or preprocess the response to strip markdown fences before parsing.
LLM returns incomplete or truncated JSON due to token limits or generation errors
Increase max tokens or use streaming with incremental JSON validation to ensure complete JSON output.
Using a base model that does not follow function calling JSON format strictly
Switch to an instruction-tuned model or a model version that supports strict function calling JSON output.
Incorrect handling of the response object, parsing response.text instead of response.json()
Use response.json() method if available to parse JSON safely instead of manually parsing response.text.
Code: broken vs fixed
import os
import json
from fireworks_ai import FireworksAI
client = FireworksAI(api_key=os.environ['FIREWORKS_API_KEY'])
payload = {'function': 'get_data', 'args': {}}
response = client.call_function(payload)
data = json.loads(response.text) # JSONDecodeError here
print(data) import os
import json
from fireworks_ai import FireworksAI
client = FireworksAI(api_key=os.environ['FIREWORKS_API_KEY'])
payload = {'function': 'get_data', 'args': {}}
response = client.call_function(payload)
raw_text = response.text
# Strip markdown fences if present
if raw_text.startswith('```') and raw_text.endswith('```'):
raw_text = '\n'.join(raw_text.split('\n')[1:-1])
data = json.loads(raw_text) # Fixed parsing
print(data) # Shows parsed JSON Workaround
Catch JSONDecodeError, extract JSON substring using regex from the raw response, then parse with json.loads() as a fallback.
Prevention
Use Fireworks AI's built-in function calling features with strict JSON response enforcement or use models that guarantee structured JSON outputs to avoid parsing errors.