Skip to content

fix(ai): wrap unparseable tool call input in object to prevent provider rejection#16539

Open
FrancoKaddour wants to merge 3 commits into
vercel:mainfrom
FrancoKaddour:fix/invalid-tool-input-object-fallback
Open

fix(ai): wrap unparseable tool call input in object to prevent provider rejection#16539
FrancoKaddour wants to merge 3 commits into
vercel:mainfrom
FrancoKaddour:fix/invalid-tool-input-object-fallback

Conversation

@FrancoKaddour

Copy link
Copy Markdown

Fixes #14442

When a tool call's input cannot be parsed as JSON, the catch block in parseToolCall falls back to storing toolCall.input (a raw string) as the input field of the returned tool call object. In multi-turn conversations this raw string ends up in the message history. Providers like Amazon Bedrock expect toolUse.input to be a JSON object and reject the next request with:

The format of the value at messages.X.content.Y.toolUse.input is invalid.

Fix: when safeParseJSON fails, store { rawInvalidInput: toolCall.input } so the field is always a JSON-serialisable object regardless of what the model returned. The original invalid string is preserved inside the wrapper for debugging.

Updated the two existing inline snapshot tests that asserted "input": "invalid json" to reflect the new shape.

Recurse only through known JSON Schema structural keywords (properties,
patternProperties, \, allOf, anyOf, etc.) instead of every object key.
Prevents deletion of user-defined tool or response parameters whose name
happens to be 'pattern'.
…jection

When a tool call's input is not valid JSON, the catch block fallback
stored toolCall.input (a raw string) directly as the tool call's input
field. On multi-turn conversations this raw string ends up in the
message history. Providers like Amazon Bedrock expect toolUse.input to
be a JSON object and reject the request with a format error on the
next turn.

Fix: when safeParseJSON fails, store { rawInvalidInput: toolCall.input }
so the field is always a JSON-serialisable object.

Fixes vercel#14442
name: tool.name,
description: tool.description,
parameters: tool.inputSchema,
parameters: removePatternKeyword(tool.inputSchema),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removePatternKeyword returns unknown, which is not assignable to the strongly-typed parameters: JSONSchema7 field of OpenAIResponsesFunctionTool, causing a tsc type error that breaks the build.

Fix on Vercel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invalid tool call input stored as raw string causes API rejection on next step

1 participant