JSONDecodeError
json.decoder.JSONDecodeError
Stack trace
Traceback (most recent call last):
File "app.py", line 42, in <module>
response = client.chat.completions.create(...)
File "/usr/local/lib/python3.9/site-packages/mistral/client.py", line 120, in create
parsed_args = json.loads(function_call_args)
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 JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) Why it happens
Mistral's function calling expects the model to return a strictly valid JSON string as the function call arguments. If the model returns extra text, markdown fences, or malformed JSON, the JSON parser fails with a JSONDecodeError. This often happens if the prompt or model instructions do not enforce strict JSON output.
Detection
Catch JSONDecodeError exceptions around the function calling parse step and log the raw model output to identify unexpected formatting before the crash.
Causes & fixes
Model returns JSON wrapped in markdown code fences or extra text
Modify the prompt to instruct the model to return only raw JSON without any markdown or extra characters.
Model outputs invalid JSON due to missing quotes, trailing commas, or syntax errors
Use a more instruction-tuned model or add explicit examples in the prompt showing valid JSON output.
Function calling arguments are extracted incorrectly from the model response
Ensure the code extracts only the function call arguments string exactly as returned by the model before parsing.
Code: broken vs fixed
import os
from mistral import MistralClient
client = MistralClient(api_key=os.environ['MISTRAL_API_KEY'])
response = client.chat.completions.create(
model="mistral-7b",
messages=[{"role": "user", "content": "Call function with args"}],
functions=[...],
function_call="my_function"
)
# This line raises JSONDecodeError
args = json.loads(response.choices[0].message.function_call.arguments)
print(args) import os
import json
from mistral import MistralClient
client = MistralClient(api_key=os.environ['MISTRAL_API_KEY'])
response = client.chat.completions.create(
model="mistral-7b",
messages=[{"role": "user", "content": "Call function with args. Return ONLY raw JSON without markdown fences."}], # Added prompt instruction
functions=[...],
function_call="my_function"
)
raw_args = response.choices[0].message.function_call.arguments
# Strip markdown fences if present
if raw_args.startswith("```json") and raw_args.endswith("```"):
raw_args = raw_args[7:-3].strip()
args = json.loads(raw_args) # Now safe to parse
print(args) # Shows parsed function call arguments Workaround
Wrap the JSON parsing in try/except JSONDecodeError, then use regex to extract JSON substring from the raw string and parse it with json.loads() as a fallback.
Prevention
Use structured function calling with strict response_format enforcement or instruction-tuned models that guarantee valid JSON outputs, avoiding parser errors.