OpenAI Function Arguments Parse Error
Your code treated tool_call.function.arguments as an object but it's a JSON string. Always JSON.parse() / json.loads() before use. This is by design — arguments arrive as a raw string to preserve the model's exact output.
Parse arguments as JSON before using: args = JSON.parse(tool_call.function.arguments). It's a string by design. Wrap in try/catch (with strict mode off) or use SDK helpers that auto-parse. Enable strict: true to guarantee valid JSON.
The Symptoms You're Seeing
Python:
args = tool_call.function.arguments
location = args["location"]
→ TypeError: string indices must be integers, not 'str'
TypeScript:
const args = tool_call.function.arguments;
const location = args.location;
→ undefined (accessing property on string returns undefined)
→ or: Property 'location' does not exist on type 'string'
Console log shows:
args: '{"location":"Karachi","units":"celsius"}'
← that's a STRING, not objectWhy arguments Is a String
OpenAI returns arguments as a raw string to preserve the model's exact output. If arguments were pre-parsed to an object, invalid JSON would raise inside the SDK; the string form lets your code inspect and repair before parsing. Once you enable strict: true, the string is guaranteed valid, but it's still a string.
What Actually Causes This Error
function_call.arguments was also a string; developers forget in refactor.Fixes That Work (Tested Nov 2026)
1Parse Explicitly Every Time
2Safe Parser Helper
3Streaming Assembly (must parse at end)
Preventing This Error Going Forward
- Always
json.loads(tc.function.arguments). String → dict. No exceptions. - Create a parse helper and use it everywhere. Central point of correctness.
- Type your dispatchers with parsed args. Interfaces enforce the parsing step.
- In streaming, accumulate strings then parse at end. Never parse partial JSON.
- Enable
strict: true. Guarantees the string parses successfully.
Researcher · AI Error Hub
Frequently Asked Questions
Some ORM-style libraries built on top of OpenAI (instructor, marvin) auto-parse. The official OpenAI SDK returns raw strings by design. Check your specific SDK's docs.
Same behavior — arguments is a string on tool_call submission. Parse the same way. Consistent across chat.completions and beta.threads endpoints.
Yes, for functions with no parameters. arguments = "{}". Still a string, still parseable. Handle empty dict result in your dispatcher.
Wrap in try/except. Log the raw arguments string for debugging. Consider retry with more specific instructions or migrate to strict mode.
No — Claude returns tool_use.input as a parsed object directly. This is OpenAI-specific behavior. When porting code, watch for this difference.
Related Errors
Function Calling Best Practices
Weekly deep dives on tool schemas, parsing patterns, and reliability. 12,000+ developers subscribe.
Subscribe Free →