ai icon indicating copy to clipboard operation
ai copied to clipboard

serializeToolCallArguments in @ai-sdk/openai drops string arguments and crashes on BigInt/cycles

Open nikolas-sapa opened this issue 1 month ago • 3 comments

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:

  1. Pre-serialized JSON string arguments are discarded or corrupted:

    • In convert-to-openai-chat-messages.ts:
      function serializeToolCallArguments(input: unknown): string {
        return JSON.stringify(
          typeof input === 'object' && input !== null && !Array.isArray(input)
            ? input
            : {},
        );
      }
      
      When an assistant message contains already serialized JSON string arguments (e.g. input: '{"city":"Tokyo"}' from an OpenAI response turn or raw tool call history), typeof input === 'object' evaluates to false, 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.
  2. Unhandled TypeError on BigInt or circular structures: JSON.stringify(input) without a replacer crashes with TypeError: Do not know how to serialize a BigInt if arguments contain BigInt values, or TypeError: Converting circular structure to JSON if 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

  1. If input is already a string, return it as-is.
  2. If input is an object, serialize with a replacer handling BigInt (val.toString()) and cycles (WeakSet).

nikolas-sapa avatar Oct 25 '26 13:10 nikolas-sapa

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.

ai-sdk-factory[bot] avatar Oct 25 '26 13:10 ai-sdk-factory[bot]

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 serializeToolCallArguments and reasons through their behavior by input type.
  • The reproduction directly invokes @ai-sdk/openai/internal with constructed edge-case messages.
  • The proposed fix addresses implementation details such as a JSON replacer and WeakSet cycle detection.
  • No production application, deployment, or real-user incident is described.

ai-sdk-factory[bot] avatar Oct 25 '26 13:10 ai-sdk-factory[bot]

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.

gr2m avatar Oct 27 '26 18:10 gr2m