fix(ai): wrap invalid tool call input in JSON object to prevent API rejection#15863
Open
chatman-media wants to merge 1 commit into
Open
fix(ai): wrap invalid tool call input in JSON object to prevent API rejection#15863chatman-media wants to merge 1 commit into
chatman-media wants to merge 1 commit into
Conversation
…ejection
When safeParseJSON fails to parse a tool call's input, the fallback
previously stored the raw string as `input`. Providers like Amazon Bedrock
require toolUse.input to be a JSON object and reject requests containing a
raw string, so the model never sees the tool-error and cannot retry.
Now the fallback wraps the unparseable value in `{ rawInvalidInput: <string> }`
so conversation history stays structurally valid for all providers.
Closes vercel#14442
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #14442
Root cause
In
packages/ai/src/generate-text/parse-tool-call.ts, the outercatchblock that handles all tool-call parse errors contains:When a model emits malformed JSON for a tool call's input and
safeParseJSONfails, the raw string is stored asinputon the invalid tool-call result. This raw string then flows throughto-response-messages.tsandconvert-to-language-model-prompt.tsinto the assistant message for the next API step. Providers like Amazon Bedrock requiretoolUse.inputto be a JSON object (the Bedrock provider castsinput: part.input as JSONObject), so the API rejects the request with:The model never sees the
tool-errorresult and has no opportunity to retry.Fix
Wrap the fallback in a valid JSON object so conversation history stays API-valid:
The
invalid: trueflag and the originalerrorare still surfaced on the tool-call result, so the model can observe the failure and retry.Tests
"input": "invalid json"(raw string) — they now correctly assert"input": { "rawInvalidInput": "invalid json" }.'should wrap unparseable input in a JSON object so API stays valid'— it fails without the fix (raw string returned) and passes with it (wrapped object returned,invalidanderrorstill set).Verification:
pnpm --filter ai test:node src/generate-text/parse-tool-call.test.ts— 22/22 pass with fix, 3/22 fail without it.🤖 Generated with Claude Code