serializeToolCallArguments in @ai-sdk/openai drops string arguments and crashes on BigInt/cycles
Description
In @ai-sdk/openai, serializeToolCallArguments is used in both convert-to-openai-chat-messages.ts and convert-to-openai-responses-input.ts to serialize assistant tool call inputs for OpenAI API requests.
Several issues occur with the current implementation:
-
Pre-serialized JSON string arguments are discarded or corrupted:
- In
convert-to-openai-chat-messages.ts:
When an assistant message contains already serialized JSON string arguments (e.g.function serializeToolCallArguments(input: unknown): string { return JSON.stringify( typeof input === 'object' && input !== null && !Array.isArray(input) ? input : {}, ); }input: '{"city":"Tokyo"}'from an OpenAI response turn or raw tool call history),typeof input === 'object'evaluates tofalse, discarding all arguments and returning"{}". - In
convert-to-openai-responses-input.ts:function serializeToolCallArguments(input: unknown): string { return JSON.stringify(input === undefined ? {} : input); }JSON.stringify('{"city":"Tokyo"}')double-stringifies the JSON string ("\"{\\\"city\\\":\\\"Tokyo\\\"}\""), causing OpenAI API rejection.
- In
-
Unhandled
TypeErroron BigInt or circular structures:JSON.stringify(input)without a replacer crashes withTypeError: Do not know how to serialize a BigIntif arguments contain BigInt values, orTypeError: Converting circular structure to JSONif arguments contain circular structures.
Reproduction
import { convertToOpenAIChatMessages } from '@ai-sdk/openai/internal';
// 1. String arguments discarded
const msg1 = [{
role: 'assistant',
content: [{ type: 'tool-call', toolCallId: '1', toolName: 'test', input: '{"param":"val"}' }]
}];
console.log(convertToOpenAIChatMessages({ prompt: msg1 }).messages[0].tool_calls[0].function.arguments);
// Actual: "{}" (arguments discarded)
// Expected: '{"param":"val"}'
// 2. BigInt crash
const msg2 = [{
role: 'assistant',
content: [{ type: 'tool-call', toolCallId: '2', toolName: 'test', input: { id: 9007199254740993n } }]
}];
convertToOpenAIChatMessages({ prompt: msg2 });
// Throws: TypeError: Do not know how to serialize a BigInt
Proposed Fix
- If
inputis already a string, return it as-is. - If
inputis an object, serialize with a replacer handlingBigInt(val.toString()) and cycles (WeakSet).
Identify issue type
Identified as Bug
Confidence: High
Rationale
The issue describes concrete failures in existing OpenAI provider tool-call serialization, including corrupted output and unexpected crashes. The proposed fix does not change the primary classification from bug.
Evidence
- Pre-serialized string tool arguments are converted to "{}" in chat messages or double-stringified in responses input.
- The report provides a concrete reproduction where expected arguments are discarded.
- BigInt and circular inputs cause unhandled JSON serialization TypeErrors.
Identify bug discovery
Discovery source: discovery:audit (high confidence).
The report appears to originate from direct inspection and testing of AI SDK serialization code, with no evidence of a production application failure.
- The author identifies the exact internal helper and both source files where it is used.
- The description compares two concrete implementations of
serializeToolCallArgumentsand reasons through their behavior by input type. - The reproduction directly invokes
@ai-sdk/openai/internalwith constructed edge-case messages. - The proposed fix addresses implementation details such as a JSON replacer and
WeakSetcycle detection. - No production application, deployment, or real-user incident is described.
If you find bugs through an agent audit, please share the prompts you used instead. Please see our updated guidelines at https://github.com/vercel/ai/issues/17243. If you have any questions, comment on #24118.