Skip to content

fix: preserve anthropic assistant ids in tool-first streams#480

Open
shaked-frame wants to merge 3 commits intoTanStack:mainfrom
shaked-frame:fix/anthropic-tool-parent-message-id
Open

fix: preserve anthropic assistant ids in tool-first streams#480
shaked-frame wants to merge 3 commits intoTanStack:mainfrom
shaked-frame:fix/anthropic-tool-parent-message-id

Conversation

@shaked-frame
Copy link
Copy Markdown

@shaked-frame shaked-frame commented Apr 20, 2026

Summary

  • add parentMessageId to Anthropic TOOL_CALL_START chunks so tool-first streams bind to the eventual assistant message id immediately
  • add an Anthropic adapter regression covering tool-first streams and asserting the tool call chunk references the same assistant message id as TEXT_MESSAGE_START
  • add a stream processor regression showing tool-first flows with parentMessageId preserve a single assistant message instead of creating a temporary local id

fixes #477

Verification

  • pnpm exec vitest run packages/typescript/ai/tests/stream-processor.test.ts
  • pnpm test:lib -- --runInBand in packages/typescript/ai
    • the new stream-processor regression passed
    • the package command still hits existing workspace resolution issues (@tanstack/ai-event-client, examples/ts-svelte-chat/.svelte-kit/tsconfig.json)
  • pnpm test:lib -- --runInBand in packages/typescript/ai-anthropic
    • blocked by an existing @tanstack/ai package-entry resolution issue before the adapter test file runs

Summary by CodeRabbit

  • Bug Fixes

    • Tool call events in streamed chats now include the parent message ID so tool calls correctly link to their originating message, improving tool-first and mixed streaming flows.
  • Tests

    • Added tests covering tool-first flows and verifying message identity, ordering, and parent/child relationships.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 20, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3fc3f0e4-ccfd-4d81-a5b8-d00c637e6003

📥 Commits

Reviewing files that changed from the base of the PR and between 4884561 and f07aad7.

📒 Files selected for processing (1)
  • packages/typescript/ai-anthropic/src/adapters/text.ts

📝 Walkthrough

Walkthrough

Anthropic text adapter now includes parentMessageId (the originating messageId) on emitted TOOL_CALL_START events in tool-first streaming flows; two new tests validate emission and downstream preservation of that server-provided message id.

Changes

Cohort / File(s) Summary
Anthropic Text Adapter
packages/typescript/ai-anthropic/src/adapters/text.ts
Adds parentMessageId: messageId to TOOL_CALL_START payload in both places that emit that event (first input_json_delta during streaming tool_use, and content_block_stop when tool_use ends without streaming args).
Anthropic Adapter Tests
packages/typescript/ai-anthropic/tests/anthropic-adapter.test.ts
New stream-processing test that mocks a tool-first Anthropic response and asserts the emitted TOOL_CALL_START chunk contains parentMessageId matching the TEXT_MESSAGE_START messageId.
Stream Processor Tests
packages/typescript/ai/tests/stream-processor.test.ts
New test ensuring when TOOL_CALL_START includes parentMessageId, the resulting assistant UIMessage preserves that server-provided id and that message parts (tool-call then text) are ordered and populated correctly.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐇 I hop through streams with careful cheer,
I carry IDs so messages stay near.
Tools leap first, then text follows through,
One stable id keeps the parts true. 🥕

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed Title clearly summarizes the main change: adding parentMessageId preservation in Anthropic tool-first streams to stabilize assistant message IDs.
Description check ✅ Passed Description covers key changes with summary, verification steps, and issue reference, but lacks detailed checklist completion and changeset confirmation.
Linked Issues check ✅ Passed Code changes fully address issue #477: parentMessageId is added to TOOL_CALL_START events, with tests verifying tool-first streams preserve a single assistant message ID.
Out of Scope Changes check ✅ Passed All changes are directly scoped to the linked issue: adapter changes emit parentMessageId, and test regressions validate the fix. No unrelated modifications present.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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 and usage tips.

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.

useChat assistant message.id changes mid-stream for Anthropic tool-first responses

1 participant