Skip to content

fix(react-core): deduplicate tool calls in CopilotChatToolCallsView to prevent React key warning - #4058

Closed
octo-patch wants to merge 1 commit into
CopilotKit:mainfrom
octo-patch:fix/issue-3928-deduplicate-tool-calls-in-view
Closed

octo-patch wants to merge 1 commit into
CopilotKit:mainfrom
octo-patch:fix/issue-3928-deduplicate-tool-calls-in-view

Conversation

@octo-patch

Copy link
Copy Markdown
Contributor

Fixes #3928

Problem

When connectAgent() is called, InMemoryAgentRunner.connect() replays compacted historic events (including TOOL_CALL_START for completed tool calls) via @ag-ui/client's defaultApplyEvents. However, at that point the agent's messages state already contains those tool calls from the prior run. Because defaultApplyEvents pushes to message.toolCalls unconditionally — with no duplicate guard — the same toolCallId ends up in the array twice.

CopilotChatToolCallsView then maps this array using toolCall.id as a React key, producing the duplicate-key warning:

Warning: Encountered two children with the same key, `call_uUF5OZtugHE6tyoC9PPtpAs6`.

Solution

Deduplicate message.toolCalls by id before rendering in CopilotChatToolCallsView, keeping the last occurrence so the most up-to-date arguments are used:

const uniqueToolCalls = [
  ...new Map(message.toolCalls.map((tc) => [tc.id, tc])).values(),
];

This is a defensive fix at the view layer. The underlying idempotency issue in @ag-ui/client's defaultApplyEvents has been separately reported upstream.

Testing

  • The fix eliminates the React duplicate-key console warning when connectAgent() is called on an agent whose messages state already contains completed tool calls.
  • Existing tool call rendering behavior is preserved — only the deduplication step is added.

…o prevent React key warning

When connectAgent() replays historic TOOL_CALL_START events over a
pre-populated messages state, defaultApplyEvents pushes to toolCalls
unconditionally, causing the same toolCallId to appear twice in the
array. CopilotChatToolCallsView used toolCall.id as the React key,
triggering a duplicate-key warning.

Fix by deduplicating toolCalls by id before rendering, keeping the
last occurrence so the most up-to-date arguments are used.

Fixes CopilotKit#3928

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:49am

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

Thanks for the detailed writeup here — the root-cause analysis in the description is accurate, and it's the analysis that shaped how this got fixed.

Closing as superseded. Tool-call dedup now lives one layer up, in deduplicateMessages in packages/react-core/src/v2/components/chat/CopilotChatMessageView.tsx: a dedupeToolCalls helper is applied both when two occurrences of an assistant message merge and, separately, to any assistant message that carries toolCalls at all — that second branch covers exactly the connect()-replay case described here, where the message itself was never duplicated. CopilotChatMessageView renders from the deduplicated array, so CopilotChatToolCallsView receives already-deduped toolCalls on both the plain and virtualized paths.

One behavioral difference worth recording, since it's the reason the fix landed there rather than here: this PR keeps the last occurrence of a duplicated id, while the merged version prefers whichever occurrence actually carries function.arguments. In the replay scenario the blank copy tends to arrive second, so "keep last" would render an empty tool-call card — the same visible symptom in a different form.

Tracking for the wider tool-call dedup family is consolidated in #3644 (this branch's linked issue, #3928, was folded into it). If you still see a duplicate-key warning for tool calls on a current release, a reproduction on #3644 would be genuinely useful — the message-level and within-array facets were separate bugs and it's worth knowing if a third one exists.

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 — 8ae2c3f0 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

2 participants