Skip to content
Source
Sign inSign up
TanStack/ai

fix(ai-anthropic): send {} when a replayed tool call has invalid JSON arguments #1583

MergedTetnofa(GitHub profile, opens in a new tab)merged 3 commits intomain fromrefs/pull/1583/headmerged by tombeckenham View on GitHub

A stopped Anthropic tool-call stream used to break every later turn in that thread. Anthropic returned 400 Input should be an object because tool_use.input was a string. This PR sends {} when those arguments are not valid JSON.

The branch now includes main at 507ecd674. testing/e2e/src/routeTree.gen.ts is regenerated, so the new route is registered with the routes from main.

🎯 Changes

  • packages/ai-anthropic/src/adapters/text.ts: the catch in formatMessages sets parsedInput = {}. Before, it set the raw string. The same value is tool_use.input and server_tool_use.input.
  • Unit test: one valid tool call and one truncated tool call in history. The valid call keeps its input. The truncated call sends {}.
  • E2E spec anthropic-truncated-tool-input-wire.spec.ts with route /api/anthropic-truncated-tool-input-wire. Turn 1 stops inside input_json_delta. Turn 2 replays that history. The spec reads the second request body.
  • Patch changeset for @tanstack/ai-anthropic.
  • No docs change. The fix restores the request shape Anthropic already requires. It adds no API.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested code changes locally with pnpm run test:pr, or these tests do not apply to this pull request.
  • I fully understand the code in this pull request, including any code generated with AI assistance.
  • Docs: I updated docs/ for this change, or this change is not user-facing.
  • Changeset: I added a changeset (pnpm changeset), or this PR does not change a published package.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Root cause

Issue. A thread on the Anthropic adapter fails on every turn after a stream stops mid tool call. Anthropic returns 400 Input should be an object.

Cause. StreamProcessor.completeToolCall keeps the partial arguments on the finalized call, and that call is replayed into history. In AnthropicTextAdapter.formatMessages, JSON.parse throws on the partial string, and the catch assigns the raw string to tool_use.input.

Fix. The catch assigns {}. Anthropic accepts the request, and the thread continues.

Possible alternatives

  • Repair the partial JSON (for example with partial-json). This can keep the fields that streamed before the stop. It sends values the model never finished, and it adds more code. The other two parse sites in this file use {}, so this PR does the same.
  • Drop the truncated tool call from history. This leaves a tool_result without its tool_use when a result exists, and Anthropic rejects that too.
  • Do not finalize truncated calls in StreamProcessor. This is a core change that affects every provider. The adapter must still send a valid request for history that is already stored.
  • Keep the raw string, as #1602 does for OpenAI. Anthropic rejects a string input. That recovery path is this bug.

Testing

Commands run

  • Agent repro on clean main (6d8e6485, same catch as 507ecd674): fail. tool_use.input was "{\"orderId\": \"44".
  • Same repro on this branch after the merge (8ff011087): pass. tool_use.input is {}.
  • pnpm test:pr was not run after the merge.

Manual test

  1. On main, replay an assistant tool call whose arguments are '{"orderId": "44'.
  2. Make sure that tool_use.input is that string.
  3. On this branch, replay the same call.
  4. Make sure that tool_use.input is {}.

How this PR makes testing easy

The unit test and the E2E spec are the repro. The E2E route uses a capturing fetch and synthetic Claude streams. It needs no API key.

Linked issues

Fixes #1582

Risk / rollback

Low. Only arguments that are not valid JSON change, and those requests used to return 400. Revert the PR to undo the change.

Summary by CodeRabbit

  • Bug Fixes
    • Replayed Anthropic tool calls with malformed or truncated JSON arguments now send an empty input object instead of raw text. Valid arguments continue to be parsed as before.
  • Tests
    • Added regression and end-to-end coverage for replaying multiple tool calls when one call has truncated arguments, including verification of the outgoing request.
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      Review in Change Stack →

      No actionable comments were generated in the recent review. 🎉

      ℹ️ Recent review info
      ⚙️ Run configuration
      • Configuration used: Repository: TanStack/ai/.coderabbit.yaml
      • Review profile: CHILL
      • Plan: Advanced
      • Run ID: 4963c6a6-0eb2-4de4-afc6-279ee7eab86c
      📥 Commits

      Reviewing files that changed from the base of the PR and between 35f106270f7816d6e6df0953c7e41f22582deca8 and 8ff011087a749abe4af460c27f2ed44a04d731b2.

      📒 Files selected for processing (1)
      • testing/e2e/src/routeTree.gen.ts

      Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 2 remain after this review.


      📝 Walkthrough

      Walkthrough

      When the Anthropic adapter cannot parse assistant tool-call arguments as JSON, it now formats the replayed tool_use.input as an empty object. Unit and end-to-end tests check valid and truncated arguments, including the replayed request body.

      Changes

      Anthropic tool-call replay

      Layer / File(s)Summary
      Malformed argument fallback
      packages/ai-anthropic/src/adapters/text.ts, packages/ai-anthropic/tests/anthropic-adapter.test.ts, .changeset/fix-ai-anthropic-truncated-tool-input.md
      The adapter uses an empty object when tool-call arguments are invalid JSON. The unit test checks valid Berlin arguments and truncated Paris arguments. The changeset records the patch.
      Wire replay validation
      testing/e2e/src/routes/api.anthropic-truncated-tool-input-wire.ts, testing/e2e/src/routeTree.gen.ts, testing/e2e/tests/anthropic-truncated-tool-input-wire.spec.ts
      The test route streams a truncated tool call and captures request bodies for two turns. The generated route tree registers the endpoint. The end-to-end test checks that the replayed tool input is an empty object.

      Priority: ➖ Normal

      Estimated code review effort: 2 (Simple) | ~10 minutes

      Change: Bug fix · Severity of issue fixed: Medium

      Suggested reviewers: alemtuzlak

      Merge Risk: ⚪ Minimal · up to 8ff01

      Malformed replayed tool inputs are sent as empty objects, with unit and wire-level tests covering valid and truncated inputs. No actionable merge risk is evident from the supplied review context.

      Security Architecture Review

      Security architecture risk: ⚪ Minimal · up to 8ff01

      The malformed-input fallback changes history serialization, not tool execution or permissions. The new test endpoint uses synthetic responses, a dummy credential, and request-local state. No material security risk was identified in the inspected changes.

      Retained concerns No architecture-level concerns identified.

      Security review details

      Security Blast Radius

      • inferred — The production behavior change affects malformed historical tool arguments serialized through this adapter, including provider-executed server-tool history. The new endpoint adds access to a deterministic test computation, not real provider credentials or tool authority.

      Trust Boundaries and Controls

      • observed — The changed formatter constructs history blocks without invoking tools or mutating the supplied tool-call arguments. The test route supplies no tools and constructs StreamProcessor without execution callbacks, keeping incomplete-call bookkeeping separate from execution authority.

      Resilience and Maintainability Implications

      • inferred — Each POST owns its capture array, transport closure, adapter, and processor. Repeated or concurrent invocations therefore do not share mutable conversation state despite using fixed synthetic thread identifiers. A caught failure ends the invocation; there is no persistent reservation or privileged configuration requiring rollback.
      🚥 Pre-merge checks | ✅ 4 | ❌ 1

      ❌ Failed checks (1 warning)

      Check nameStatusExplanationResolution
      Docstring Coverage⚠️ WarningDocstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 5 files.Write docstrings for the functions missing them to satisfy the coverage threshold.
      ✅ Passed checks (4 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title clearly and concisely describes the main change: use an empty object when replayed Anthropic tool-call arguments contain invalid JSON.
      Description check✅ PassedThe description explains the cause, fix, tests, release impact, and rollback risk. It includes the required template sections and a changeset; the test checklist remains unchecked, consistent with the…
      Linked Issues check✅ PassedIssue [#1582] requires replayed truncated Anthropic tool-call arguments to produce an object in tool_use.input. packages/ai-anthropic/src/adapters/text.ts now uses {} when JSON parsing fails and…
      Out of Scope Changes check✅ PassedThe adapter change, regression tests, E2E route and spec, generated route registration, and changeset all support the fix for issue [#1582]. The available change summary shows no unrelated changes.
      • Fix all pre-merge checks with AI
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create a new PR
      • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

      Autopilot is currently an internal CodeRabbit preview.


      Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

      ❤️ Share
      Comment `@coderabbitai help` to get the list of available commands.
    2. Thanks for the PR, @Tetnofa! 🙌 @tombeckenham will take a look.

      Automated pre-review checks

      • ✅ CI passing
      • ✅ No merge conflicts
      • ✅ Changeset present
      • ✅ E2E test changes included
      Automated triage — a human review follows.
    3. github-actions[bot](GitHub profile, opens in a new tab) added labelswaiting-on: maintainer

    4. github-actions[bot](GitHub profile, opens in a new tab) added labelsmerge-conflicts

    5. github-actions[bot](GitHub profile, opens in a new tab) added labelswaiting-on: author

    6. github-actions[bot](GitHub profile, opens in a new tab) removed labelswaiting-on: maintainer

    7. 🦋 Changeset detected

      Latest commit: 8ff011087a749abe4af460c27f2ed44a04d731b2

      The changes in this PR will be included in the next version bump.

      This PR includes changesets to release 1 package
      NameType
      @tanstack/ai-anthropicPatch

      Not sure what this means? Click here to learn what changesets are.

      Click here if you're a maintainer who wants to add another changeset to this PR

    8. tombeckenham approved these changes

    9. View your CI Pipeline Execution ↗ for commit 8ff011087a749abe4af460c27f2ed44a04d731b2

      CommandStatusDurationResult
      nx run @tanstack/ai-grok-build:build✅ Succeeded1sView ↗

      ☁️ Nx Cloud last updated this comment at 2026-10-06 04:55:31 UTC

    10. nx-cloud[bot](GitHub profile, opens in a new tab)commented

      View your CI Pipeline Execution ↗ for commit 8ff011087a749abe4af460c27f2ed44a04d731b2

      CommandStatusDurationResult
      nx run-many --targets=build --exclude=examples/...✅ Succeeded8sView ↗

      ☁️ Nx Cloud last updated this comment at 2026-10-06 04:55:44 UTC

    11. Open in StackBlitz

      @tanstack/ai

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai@1583

      @tanstack/ai-acp

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-acp@1583

      @tanstack/ai-angular

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-angular@1583

      @tanstack/ai-anthropic

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-anthropic@1583

      @tanstack/ai-bedrock

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-bedrock@1583

      @tanstack/ai-byteplus

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-byteplus@1583

      @tanstack/ai-claude-code

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-claude-code@1583

      @tanstack/ai-client

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-client@1583

      @tanstack/ai-cloudflare

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-cloudflare@1583

      @tanstack/ai-code-mode

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-code-mode@1583

      @tanstack/ai-code-mode-snippets

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-code-mode-snippets@1583

      @tanstack/ai-codex

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-codex@1583

      @tanstack/ai-cohere

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-cohere@1583

      @tanstack/ai-compaction

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-compaction@1583

      @tanstack/ai-devtools-core

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-devtools-core@1583

      @tanstack/ai-durable-stream

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-durable-stream@1583

      @tanstack/ai-elevenlabs

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-elevenlabs@1583

      @tanstack/ai-event-client

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-event-client@1583

      @tanstack/ai-fal

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-fal@1583

      @tanstack/ai-gemini

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-gemini@1583

      @tanstack/ai-grok

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-grok@1583

      @tanstack/ai-grok-build

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-grok-build@1583

      @tanstack/ai-groq

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-groq@1583

      @tanstack/ai-isolate-cloudflare

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-cloudflare@1583

      @tanstack/ai-isolate-daytona

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-daytona@1583

      @tanstack/ai-isolate-node

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-node@1583

      @tanstack/ai-isolate-quickjs

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-quickjs@1583

      @tanstack/ai-isolate-quickjs-bun

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-quickjs-bun@1583

      @tanstack/ai-llmgateway

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-llmgateway@1583

      @tanstack/ai-lovable

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-lovable@1583

      @tanstack/ai-mcp

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-mcp@1583

      @tanstack/ai-memory

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-memory@1583

      @tanstack/ai-mistral

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-mistral@1583

      @tanstack/ai-octane

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-octane@1583

      @tanstack/ai-ollama

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-ollama@1583

      @tanstack/ai-ollaya

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-ollaya@1583

      @tanstack/ai-openai

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-openai@1583

      @tanstack/ai-opencode

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-opencode@1583

      @tanstack/ai-openrouter

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-openrouter@1583

      @tanstack/ai-perplexity

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-perplexity@1583

      @tanstack/ai-persistence

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-persistence@1583

      @tanstack/ai-preact

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-preact@1583

      @tanstack/ai-react

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-react@1583

      @tanstack/ai-react-ui

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-react-ui@1583

      @tanstack/ai-reactor

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-reactor@1583

      @tanstack/ai-remix

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-remix@1583

      @tanstack/ai-sandbox

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox@1583

      @tanstack/ai-sandbox-blaxel

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-blaxel@1583

      @tanstack/ai-sandbox-boxd

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-boxd@1583

      @tanstack/ai-sandbox-cloudflare

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-cloudflare@1583

      @tanstack/ai-sandbox-daytona

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-daytona@1583

      @tanstack/ai-sandbox-docker

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-docker@1583

      @tanstack/ai-sandbox-e2b

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-e2b@1583

      @tanstack/ai-sandbox-local-process

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-local-process@1583

      @tanstack/ai-sandbox-sprites

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-sprites@1583

      @tanstack/ai-sandbox-upstash-box

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-upstash-box@1583

      @tanstack/ai-sandbox-vercel

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-vercel@1583

      @tanstack/ai-skills

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-skills@1583

      @tanstack/ai-solid

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-solid@1583

      @tanstack/ai-solid-ui

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-solid-ui@1583

      @tanstack/ai-svelte

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-svelte@1583

      @tanstack/ai-typesafe

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-typesafe@1583

      @tanstack/ai-utils

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-utils@1583

      @tanstack/ai-vercel-gateway

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-vercel-gateway@1583

      @tanstack/ai-vertex

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-vertex@1583

      @tanstack/ai-vue

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-vue@1583

      @tanstack/ai-vue-ui

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-vue-ui@1583

      @tanstack/ai-worldlabs

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-worldlabs@1583

      @tanstack/openai-base

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/openai-base@1583

      @tanstack/preact-ai-devtools

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/preact-ai-devtools@1583

      @tanstack/react-ai-devtools

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/react-ai-devtools@1583

      @tanstack/solid-ai-devtools

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/solid-ai-devtools@1583

      @tanstack/svelte-ai-devtools

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/svelte-ai-devtools@1583

      commit: 8ff0110

    12. tombeckenham merged thisd07f0b6

    13. tombeckenham closed this

Sign in to join this conversation.