Skip to content

fix(react-core): merge toolCalls by id when deduplicating duplicate assistant messages - #4059

Closed
octo-patch wants to merge 1 commit into
CopilotKit:mainfrom
octo-patch:fix/issue-3644-merge-tool-calls-on-duplicate-message-id
Closed

octo-patch wants to merge 1 commit into
CopilotKit:mainfrom
octo-patch:fix/issue-3644-merge-tool-calls-on-duplicate-message-id

Conversation

@octo-patch

Copy link
Copy Markdown
Contributor

Fixes #3644

Problem

When a TOOL_CALL_RESULT is interleaved between TOOL_CALL_START events for the same parentMessageId, @ag-ui/client creates a second assistant message with the same id instead of appending to the existing one (because the parent is no longer the last message in the array).

This produces a sequence like:

  1. msg_x created with toolCalls: [tool_a, tool_b]
  2. A TOOL_CALL_RESULT for tool_a is inserted as a separate "tool" message
  3. TOOL_CALL_START(tool_c) — parent msg_x is no longer last, so a new msg_x entry is created with toolCalls: [tool_c]

The previous deduplicateMessages logic:

const toolCalls = message.toolCalls ?? existing.toolCalls;

picks the later occurrence's toolCalls when it's defined — silently dropping tool_a and tool_b. The dev-mode warning fires but the data is already wrong.

Solution

When both occurrences have non-empty toolCalls arrays, merge them by id (later occurrence wins for any given id):

const merged = new Map(existing.toolCalls.map((tc) => [tc.id, tc]));
for (const tc of message.toolCalls) {
  merged.set(tc.id, tc);
}
toolCalls = [...merged.values()];

Semantics preserved:

  • undefined → fall back to existing (no tool call activity in this chunk)
  • [] → intentional reset ("all tool calls finished"), resets the set
  • Two non-empty arrays → union by id, later wins

Testing

Two new unit tests added to the existing deduplicateMessages suite:

  1. Verifies that toolCalls from both occurrences are merged correctly (reproduces the 🐛 Bug: Duplicate assistant message IDs when multiple tool calls are interleaved with tool results #3644 scenario)
  2. Verifies that when the same tool call id appears in both, the later entry's arguments win

…ssistant messages

When a TOOL_CALL_RESULT is interleaved between TOOL_CALL_START events
for the same parentMessageId, @ag-ui/client creates a second assistant
message with the same id rather than appending to the existing one
(because the parent is no longer the last message in the array).

The previous deduplicateMessages logic used:
  toolCalls = message.toolCalls ?? existing.toolCalls
This discards any toolCalls from earlier occurrences when the later
duplicate has its own non-undefined toolCalls array.

Fix: when both occurrences have non-empty toolCalls arrays, merge them
by id (later occurrence wins), so no tool call is silently dropped.
Explicit [] (all tool calls completed) still resets the set.

Adds two new tests covering the merge scenario and the tie-break rule.

Fixes CopilotKit#3644

Signed-off-by: octo-patch <[email protected]>

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@vercel

vercel Bot commented Apr 18, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
docs Ready Ready Preview, Comment Apr 18, 2026 1:52am

Request Review

@vercel

vercel Bot commented Apr 18, 2026

Copy link
Copy Markdown
Contributor

Someone is attempting to deploy a commit to the CopilotKit Team on Vercel.

A member of the Team first needs to authorize it.

@BenTaylorDev

Copy link
Copy Markdown
Contributor

Closing as already-landed — the deduplicateMessages() merge-by-id fix (with toolCalls ?? existing.toolCalls recovery) is on main at packages/react-core/src/v2/components/chat/CopilotChatMessageView.tsx:324-339. Note: #3644 stays open as its own tracker, since the interleaved-tool-calls case may not be fully covered by the dedup.

tylerslaton added a commit that referenced this pull request Nov 16, 2026
…8408)

<!--
Thank you for sending the PR! We appreciate you spending the time to
work on these changes.

Help us understand your motivation by explaining why you decided to make
this change.


**Please PLEASE reach out to us first before starting any significant
work on new or existing features.**

By the time you've gotten here, you're looking at creating a pull
request so hopefully we're not too late.

We love community contributions! That said, we want to make sure we're
all on the same page before you start.
Investing a lot of time and effort just to find out it doesn't align
with the upstream project feels awful, and we don't want that to happen.
It also helps to make sure the work you're planning isn't already in
progress.

As described in our contributing guide, please file an issue first:
https://github.com/ag-ui-protocol/ag-ui/issues
Or, reach out to us on Discord: https://discord.com/invite/6dffbvGU3D


You can learn more about contributing to copilotkit here:
https://github.com/copilotkit/copilotkit/blob/master/CONTRIBUTING.md

Happy contributing!

-->

## What does this PR do?

Fixes the remaining half of #3644: when two copies of an assistant
message share an id, the chat view dropped the tool calls held only by
the earlier copy. The original cause was in `@ag-ui/client`: up to
0.0.51, `TOOL_CALL_START` attached a tool call to its parent only if the
parent was the *last* message. A `TOOL_CALL_RESULT` between two tool
calls for the same parent therefore created a second assistant message
with the same id. Upstream fixed this in 0.0.52 (2026-04-08), and
CopilotKit has used a fixed version since `a5c1137465`. I reproduced the
reporter's event sequence against an old and a current client to confirm
this.

`deduplicateMessages` in `CopilotChatMessageView.tsx` already merges
duplicate assistant messages (it recovers wiped content and `undefined`
toolCalls), but when both copies had tool calls it kept only the later
copy's list. For the reporter's sequence — `[tool_a, tool_b]` on the
first copy and `[tool_c]` on the second — only `tool_c` rendered.
Duplicates can still reach the view from older clients, custom agents,
or stored history.

- When both copies carry tool calls, `deduplicateMessages` now keeps the
calls from both. The existing rules are unchanged: `undefined` recovers
the earlier list, and an explicit `[]` is kept.
- A new mergeToolCalls helper combines the lists: for a tool call in
both copies, a later copy with arguments wins, and a blank copy never
replaces a populated one. dedupeToolCalls is unchanged, so within a
single message the first populated copy still wins (TOOL_CALL_ARGS
deltas go to the first copy).
- Moved the `deduplicateMessages` doc comment back above its function;
an earlier change had placed `dedupeToolCalls` between them.
- Four new deduplicateMessages tests: the reporter's scenario (all three
tool calls survive and the content is kept), a blank later copy does not
replace a populated one, a later populated copy wins when both are
populated, and within one message a complete copy is kept over a stale
later copy.

This builds on #4059, which proposed combining the lists but merged
shared ids with "later wins". As noted when #4058 was closed, the blank
copy of a duplicated tool call usually arrives second, so this PR keeps
the existing "prefer the populated copy" protection.


## Related PRs and Issues
- Fixes #3644
- #4059
- #4058 
- #3928

## Checklist

- [x] I have read the [Contribution
Guide](https://github.com/copilotkit/copilotkit/blob/master/CONTRIBUTING.md)
- [x] If the PR changes or adds functionality, I have updated the
relevant documentation
- [x] If the PR contains a breaking change, the title has `!` (for
example `feat(react-core)!: ...`) and the description has a `BREAKING
CHANGE:` line with migration steps. See
[VERSIONING.md](https://github.com/CopilotKit/CopilotKit/blob/main/VERSIONING.md)
- [x] "Allow edits by maintainers" is checked (lets us help iterate on
your PR directly — faster turnaround for everyone)

This branch was successfully deployed

1 active deployment
Preview – docs — dc2c16d5 Deployed Apr 18, 2026 by vercel[bot]
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.

🐛 Bug: Duplicate assistant message IDs when multiple tool calls are interleaved with tool results

2 participants