fix(ai-anthropic): send {} when a replayed tool call has invalid JSON arguments #1583
main fromrefs/pull/1583/head·merged by tombeckenham View on GitHubA 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: thecatchinformatMessagessetsparsedInput = {}. Before, it set the raw string. The same value istool_use.inputandserver_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.tswith route/api/anthropic-truncated-tool-input-wire. Turn 1 stops insideinput_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_resultwithout itstool_usewhen 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, samecatchas507ecd674): fail.tool_use.inputwas"{\"orderId\": \"44". - Same repro on this branch after the merge (
8ff011087): pass.tool_use.inputis{}. pnpm test:prwas not run after the merge.
Manual test
- On
main, replay an assistant tool call whose arguments are'{"orderId": "44'. - Make sure that
tool_use.inputis that string. - On this branch, replay the same call.
- Make sure that
tool_use.inputis{}.
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
github-actions[bot](GitHub profile, opens in a new tab) assignedtombeckenham
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
github-actions[bot](GitHub profile, opens in a new tab) added labelswaiting-on: maintainer
github-actions[bot](GitHub profile, opens in a new tab) added labelsmerge-conflicts
github-actions[bot](GitHub profile, opens in a new tab) added labelswaiting-on: author
github-actions[bot](GitHub profile, opens in a new tab) removed labelswaiting-on: maintainer
🦋 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
Name Type @tanstack/ai-anthropic Patch 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
tombeckenham approved these changes
View your CI Pipeline Execution ↗ for commit 8ff011087a749abe4af460c27f2ed44a04d731b2
Command Status Duration Result nx run @tanstack/ai-grok-build:build✅ Succeeded 1s View ↗
☁️ Nx Cloud last updated this comment at
2026-10-06 04:55:31UTCView your CI Pipeline Execution ↗ for commit 8ff011087a749abe4af460c27f2ed44a04d731b2
Command Status Duration Result nx run-many --targets=build --exclude=examples/...✅ Succeeded 8s View ↗
☁️ Nx Cloud last updated this comment at
2026-10-06 04:55:44UTC@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@1583commit:
8ff0110tombeckenham merged thisd07f0b6
tombeckenham closed this
Sign in to join this conversation.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
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.tsIncluded 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.inputas an empty object. Unit and end-to-end tests check valid and truncated arguments, including the replayed request body.Changes#
Anthropic tool-call replay
packages/ai-anthropic/src/adapters/text.ts,packages/ai-anthropic/tests/anthropic-adapter.test.ts,.changeset/fix-ai-anthropic-truncated-tool-input.mdtesting/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.tsPriority: ➖ Normal
Estimated code review effort: 2 (Simple) | ~10 minutes
Change: Bug fix · Severity of issue fixed: Medium
Suggested reviewers:
alemtuzlakMerge Risk: ⚪ Minimal · up to
8ff01Malformed 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
8ff01The 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
Trust Boundaries and Controls
Resilience and Maintainability Implications
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)#
✅ Passed checks (4 passed)
#1582] requires replayed truncated Anthropic tool-call arguments to produce an object intool_use.input.packages/ai-anthropic/src/adapters/text.tsnow uses{}when JSON parsing fails and…#1582]. The available change summary shows no unrelated changes.✨ Finishing Touches
🧪 Generate unit tests (beta)
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