OpenAI Function Call Returned Invalid JSON
The model called your function but the arguments field contains malformed JSON — trailing commas, unquoted keys, incomplete objects, or truncation. Fix by enabling Structured Outputs (strict: true) for guaranteed valid JSON, plus fallback JSON repair.
Enable Structured Outputs by adding strict: true to your function definition. This guarantees the model returns JSON matching your schema exactly — no truncation, no drift, no invalid syntax. Requires additionalProperties: false and all fields in required. Works on GPT-4o, GPT-4o-mini, GPT-5, o-series.
The Error Symptoms You're Seeing
tool_call.function.arguments =
'{"location": "Karachi", "units":' ← truncated
json.JSONDecodeError: Expecting ',' delimiter: line 1 column 42
Or invalid but parseable-looking:
'{"location": "Karachi", "units": "celsius",}' ← trailing comma
'{location: "Karachi"}' ← unquoted key
'{"count": NaN}' ← non-JSON value
Or extra content around JSON:
'Here are the arguments: {"location": "Karachi"}'
'```json\n{"location": "Karachi"}\n```'What Actually Causes This Error
Fixes That Work (Tested Nov 2026)
1Enable Structured Outputs (strict: true)
Every property in the object must appear in required. additionalProperties: false is mandatory. Not all JSON Schema features supported: no minLength, maxLength, pattern, format on strings. See Structured Outputs Schema Invalid for the full subset.
2Validate + Repair Fallback
3Retry with Feedback on Failure
Preventing This Error Going Forward
- Always use
strict: truein production. Guaranteed valid JSON. - Include
additionalProperties: falseand fullrequired. Mandatory for strict. - Simplify schemas. Deep nesting stresses even good models.
- Set max_tokens generously. Truncation = invalid JSON.
- Add fallback json_repair. Defense in depth for legacy code.
- Log tool_call.function.arguments raw. Post-mortem debugging.
Researcher · AI Error Hub
Frequently Asked Questions
GPT-4o (Aug 2024+), GPT-4o-mini, GPT-5, GPT-5-mini, o-series (o1, o3). Older models like gpt-3.5-turbo don't support strict. Check the model reference in OpenAI docs.
First request with a new schema: ~1-2s schema compilation. Subsequent requests with same schema: no overhead. Compilation is cached per schema per organization for a period.
Yes — combine freely. tool_choice: "auto", "required", or specific function all work with strict. Forcing a specific strict tool = highest reliability.
Not in strict mode. Every property in your object must be required. Workaround: make the type a union with null, e.g. {"type": ["string", "null"]} and require the field.
No additional cost. Same input/output token pricing. The reliability improvement is free.
Related Errors
Reliable Function Calling in Production
Weekly deep dives on Structured Outputs, function schemas, and JSON validation. 12,000+ developers subscribe.
Subscribe Free →