Skip to content

fix(ai): wrap malformed tool call input in JSON object instead of raw string#16280

Open
arinsoni wants to merge 1 commit into
vercel:mainfrom
arinsoni:arinsoni/fix-invalid-tool-call-raw-string
Open

fix(ai): wrap malformed tool call input in JSON object instead of raw string#16280
arinsoni wants to merge 1 commit into
vercel:mainfrom
arinsoni:arinsoni/fix-invalid-tool-call-raw-string

Conversation

@arinsoni

Copy link
Copy Markdown

Closes #14442


When parseToolCall falls through to its catch block because the model returned unparseable JSON as a tool call's input, the SDK stored the raw string directly as input on the resulting tool-call part:

const input = parsedInput.success ? parsedInput.value : toolCall.input;
//                                                       ^^^^^^^^^^^^^^
//                                              raw string, not a JSON object

This raw string is then forwarded into the assistant message for the next API step via to-response-messages.ts. Providers like Amazon Bedrock require toolUse.input to be a JSON object — they reject the request before the model ever sees the tool-error result, breaking the retry loop entirely.

Fix: when JSON parsing fails, wrap the raw string in { rawInvalidInput: toolCall.input } so the conversation history is always a valid JSON object for all providers.

Tests: updated two inline snapshots that asserted the old raw-string behavior to match the new wrapped-object shape.

This contribution was made with AI-agent assistance (Claude Code).

When parseToolCall falls through to the catch block with unparseable JSON,
the input was stored as a raw string. Providers like Amazon Bedrock require
toolUse.input to be a JSON object, so the next API step was rejected before
the model could see the tool-error and retry.

Wrap invalid input as { rawInvalidInput: <string> } so the conversation
history stays well-typed for all providers.

Closes vercel#14442

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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