GPT-4o returns JSON with trailing comma in tool call

GPT-4o returns JSON with trailing comma in tool call

While parsing function‑call output from GPT‑4o the JSON loader threw:

json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 98 (char 97)

The model emitted:

{
  "name": "getStockPrice",
  "arguments": { "ticker": "AMD", },
  "id": "abc"
}

Note the trailing comma after "AMD".

Added a pre‑parse scrub:

import re, json
clean = re.sub(r",\s*([}\]])", r"\1", raw)
data = json.loads(clean)

Also set response_format={"type":"json_object"} in the request; 4o obeys the flag half the time.

If strict JSON needed wrap the model output with jsonfix library; it auto‑removes trailing commas and comments.

comments powered by Disqus