Skip to content

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

Open
shaked-frame wants to merge 1 commit 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 1 commit 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

    • Fixed message tracking for tool calls to ensure they properly reference their parent message in chat streams.
    • Improved handling of tool calls that arrive before text content in streaming responses.
  • Tests

    • Added test coverage for tool-first message flows to verify correct message identification and parent-child relationships.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 20, 2026

📝 Walkthrough

Walkthrough

The Anthropic text adapter now includes a parentMessageId field in TOOL_CALL_START events, enabling tracking of the parent message across tool-first message flows. Two new tests validate this behavior for both adapter-level and stream processing scenarios.

Changes

Cohort / File(s) Summary
Anthropic Text Adapter
packages/typescript/ai-anthropic/src/adapters/text.ts
Added parentMessageId: messageId field to TOOL_CALL_START events in two code paths: when tool args begin streaming (input_json_delta) and when tool blocks complete before args arrive (content_block_stop).
Anthropic Adapter Tests
packages/typescript/ai-anthropic/tests/anthropic-adapter.test.ts
New test validates that TOOL_CALL_START events emitted during tool-first streams include the parentMessageId field matching the parent TEXT_MESSAGE_START message id.
Stream Processor Tests
packages/typescript/ai/tests/stream-processor.test.ts
New test confirms that tool-first message flows preserve the server-provided message id from parentMessageId in the resulting assistant UIMessage, with correct part ordering (tool-call followed by text content).

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A little field, so neat and bright,
Tracks parent IDs through the night,
When tools arrive before the text,
The message knows what flows comes next! 🎯

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning PR description covers objectives and verification steps but lacks completion of required checklist items and changeset declaration. Complete the checklist by confirming contribution guide compliance and test execution. Declare whether a changeset is needed for published code changes.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: adding preservation of Anthropic assistant IDs in tool-first streams.
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