json.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/openai/api_resources/chat_completion.py", line 50, in create
return super().create(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/openai/api_resources/abstract/engine_api_resource.py", line 123, in create
response = self._request("post", url, params=params)
File "/usr/local/lib/python3.9/site-packages/openai/api_resources/abstract/engine_api_resource.py", line 88, in _request
resp = self._client.request(method, url, params)
File "/usr/local/lib/python3.9/site-packages/openai/api_requestor.py", line 120, in request
result = self._interpret_response(result, stream)
File "/usr/local/lib/python3.9/site-packages/openai/api_requestor.py", line 362, in _interpret_response
self._interpret_response_line(line)
File "/usr/local/lib/python3.9/site-packages/openai/api_requestor.py", line 376, in _interpret_response_line
parsed = json.loads(line)
File "/usr/local/lib/python3.9/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "/usr/local/lib/python3.9/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/lib/python3.9/json/decoder.py", line 355, 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
The function calling feature expects the LLM to return a valid JSON string as the function arguments. If the LLM outputs extra text, markdown fences, or malformed JSON, the JSON decoder fails to parse it, causing this error.
Detection
Catch json.JSONDecodeError exceptions when parsing function call arguments and log the raw LLM output to identify unexpected formatting or invalid JSON.
Causes & fixes
LLM response includes markdown code fences or extra text around the JSON arguments
Modify the prompt to instruct the model to return only raw JSON without markdown fences, or preprocess the response to strip fences before JSON parsing.
The LLM outputs invalid JSON due to missing quotes, trailing commas, or other syntax errors
Use a more instruction-tuned model that reliably outputs valid JSON, or add a retry mechanism with prompt adjustments to enforce strict JSON formatting.
Function calling arguments are expected as a JSON object but the LLM returns a JSON array or other type
Ensure the prompt and function schema specify the exact expected JSON structure, and validate the LLM output matches it before parsing.
Code: broken vs fixed
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Call function with args"}],
functions=[{"name": "my_func", "parameters": {"type": "object", "properties": {"param1": {"type": "string"}}}}],
function_call={"name": "my_func"}
)
# This line raises JSONDecodeError because response.choices[0].message.function_call.arguments is not valid JSON
args = response.choices[0].message.function_call.arguments # json.JSONDecodeError here import os
import json
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Call function with args. Return ONLY raw JSON, no markdown fences."}], # Added prompt instruction
functions=[{"name": "my_func", "parameters": {"type": "object", "properties": {"param1": {"type": "string"}}}}],
function_call={"name": "my_func"}
)
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 json.JSONDecodeError, then extract JSON substring using regex from the raw LLM output and parse it manually as a fallback.
Prevention
Use OpenAI's function calling with strict response_format settings or instruction-tuned models that guarantee valid JSON output, avoiding parser errors at the API level.