ai icon indicating copy to clipboard operation
ai copied to clipboard

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

Open atran3 opened this issue 7 months ago • 5 comments

Bug Description

When parseToolCall fails to parse a tool call's JSON input, the catch block stores the raw string as input:

// parse-tool-call.ts (line 82-83)
const parsedInput = await safeParseJSON({ text: toolCall.input });
const input = parsedInput.success ? parsedInput.value : toolCall.input;
//                                                       ^^^^^^^^^^^^^^
//                                              raw string, not a JSON object

This raw string flows through to-response-messages.ts and convert-to-language-model-prompt.ts into the assistant message for the next API step. Providers like Amazon Bedrock require toolUse.input to be a JSON object (the Bedrock provider does input: part.input as JSONObject in convert-to-bedrock-chat-messages.ts), so the API rejects the request before the model ever sees the tool-error result.

Expected Behavior

The model should see the tool-error and be able to retry. The invalid tool call's input should be wrapped in a valid JSON object (e.g., { rawInvalidInput: toolCall.input }) so the conversation history remains valid for the next API call.

Reproduction

  1. Use a model via Amazon Bedrock with tool calling
  2. Have the model generate a tool call with malformed JSON input (can be simulated with middleware that corrupts tool-call chunks)
  3. The SDK correctly marks the tool call as invalid: true and generates a tool-error
  4. On the next step, the API rejects with: "The format of the value at messages.X.content.Y.toolUse.input is invalid. Provide a json object for the field and try again."

Suggested Fix

In parse-tool-call.ts, wrap the fallback input in an object:

- const input = parsedInput.success ? parsedInput.value : toolCall.input;
+ const input = parsedInput.success ? parsedInput.value : { rawInvalidInput: toolCall.input };

Environment

  • ai version: 6.0.48
  • Provider: @ai-sdk/amazon-bedrock
  • Model: Claude (Anthropic via Bedrock)

atran3 avatar Apr 14 '26 17:04 atran3

I think this was resolved by #14643 (merged after this was filed). toResponseMessages now guards the invalid-tool-call case in packages/ai/src/generate-text/to-response-messages.ts:

input: part.invalid && typeof part.input !== 'object' ? {} : part.input,

So in the multi-step flow the assistant message for the next step gets {} instead of the raw unparsed string. By the time it reaches convert-to-language-model-prompt.ts the input is already an object, so providers that require toolUse.input to be a JSON object (Bedrock, etc.) no longer get a bare string.

I couldn't reproduce the original failure against latest main for that reason. If anyone still hits this on the latest canary, a minimal repro would help — otherwise this looks safe to close.

Zawwarsami16 avatar Jun 13 '26 06:06 Zawwarsami16

@Zawwarsami16 it's hard to give you a repro, but it is still broken even in ai@beta - I'm testing this pr now: https://github.com/vercel/ai/pull/15280

Edit: pnpm patched with the diff from that pr and it works now

zachrip avatar Jun 18 '26 22:06 zachrip

Identify issue type

{
  "evidence": [
    "Description reports malformed JSON tool-call input is stored as a raw string and later sent in the next assistant message.",
    "Amazon Bedrock rejects the next request because `toolUse.input` must be a JSON object.",
    "Reporter expected the SDK to preserve valid conversation history so the model can receive the `tool-error` and retry.",
    "A later comment says the issue may have been resolved by another PR, but another commenter states it is still broken in `ai@beta` and that a patch from a PR fixes it."
  ],
  "rationale": "The issue describes a concrete failure in existing multi-step tool-call handling that causes provider API rejection, with reproduction steps and an observed error. Even though fixes are discussed, the primary report is broken SDK behavior.",
  "type": "bug",
  "confidence": "high"
}

lgrammel avatar Jul 02 '26 08:07 lgrammel

Bug Reproduction

Bug was reproduced.

Created runnable reproduction scripts at packages/ai/reproductions/reproduce-14442.ts and reproductions/check-14442.ts. Ran cd packages/ai && pnpm tsx reproductions/reproduce-14442.ts; it fails because a malformed tool-call input is stored in the invalid tool-call content as the raw string "{ city: San Francisco, }" instead of a JSON object wrapper like { rawInvalidInput: ... }.

Branch: bug-reproduction/14442-1

lgrammel avatar Jul 02 '26 13:07 lgrammel

:rocket: Published in:

Package Version
ai 7.0.17 github npm
@ai-sdk/amazon-bedrock 5.0.13 github npm
@ai-sdk/angular 3.0.17 github npm
@ai-sdk/anthropic 4.0.9 github npm
@ai-sdk/anthropic-aws 2.0.1 github npm
@ai-sdk/gateway 4.0.13 github npm
@ai-sdk/google 4.0.9 github npm
@ai-sdk/google-vertex 5.0.12 github npm
@ai-sdk/harness 1.0.20 github npm
@ai-sdk/harness-claude-code 1.0.20 github npm
@ai-sdk/harness-codex 1.0.21 github npm
@ai-sdk/harness-deepagents 1.0.19 github npm
@ai-sdk/harness-opencode 1.0.20 github npm
@ai-sdk/harness-pi 1.0.20 github npm
@ai-sdk/langchain 3.0.17 github npm
@ai-sdk/llamaindex 3.0.17 github npm
@ai-sdk/mcp 2.0.8 github npm
@ai-sdk/mistral 4.0.6 github npm
@ai-sdk/otel 1.0.17 github npm
@ai-sdk/policy-opa 1.0.17 github npm
@ai-sdk/react 4.0.18 github npm
@ai-sdk/rsc 3.0.17 github npm
@ai-sdk/sandbox-just-bash 1.0.20 github npm
@ai-sdk/sandbox-vercel 1.0.20 github npm
@ai-sdk/svelte 5.0.17 github npm
@ai-sdk/tui 1.0.17 github npm
@ai-sdk/vue 4.0.17 github npm
@ai-sdk/workflow 1.0.17 github npm
@ai-sdk/workflow-harness 1.0.20 github npm
@ai-sdk/xai 4.0.8 github npm

github-actions[bot] avatar Jul 07 '26 23:07 github-actions[bot]