Skip to content

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

Closed

Description

@caibirdme

♻️ Reproduction Steps

When using CopilotKit with an AG-UI event stream that contains multiple TOOL_CALL_START events for the same assistant message, interleaved with TOOL_CALL_RESULT events, the
frontend can end up with duplicate assistant message IDs in messages[].

In development mode, this triggers:

CopilotChatMessageView: Deduplicated 1 message(s) with duplicate IDs.

This seems to happen with a sequence like this:

  1. An assistant text message is created with messageId = msg_x
  2. TOOL_CALL_START(tool_a, parentMessageId = msg_x)
  3. TOOL_CALL_START(tool_b, parentMessageId = msg_x)
  4. TOOL_CALL_RESULT(messageId = tool-result:msg_x:tool_a, toolCallId = tool_a)
  5. TOOL_CALL_START(tool_c, parentMessageId = msg_x)

✅ Expected Behavior

tool_c should be appended to the existing assistant message msg_x

❌ Actual Behavior

  • a new assistant message is created with the same id msg_x
  • now there are two assistant messages with the same id

CopilotChatMessageView deduplicates messages by message.id:
const deduplicatedMessages = [...new Map(messages.map((m) => [m.id, m])).values()];

So once duplicate IDs appear, one of the messages is dropped from rendering. In my case, the later duplicate assistant message had empty content and only the last tool call, so deduplication effectively hid the earlier assistant content/tool calls.

Screenshots

No response

𝌚 CopilotKit Version

- CopilotKit React: 1.54.1
  - @ag-ui/client: 0.0.48

📄 Logs (Optional)

The issue appears to come from the event-application logic for TOOL_CALL_START.
The current logic seems to only attach the tool call to the parent assistant message if the last message in the array has `id === parentMessageId`.
That works until a TOOL_CALL_RESULT is appended as a separate role: "tool" message. After that, the last message is no longer the parent assistant message, so a later TOOL_CALL_START for the same parent creates a new assistant message with the same ID instead of merging into the existing one.

  In other words, the logic is too dependent on "last message" position and does not search for the existing assistant message by parentMessageId.

Activity

  1. added
    bug
    on Apr 7, 2026
  2. added a commit that references this issue on Apr 18, 2026
    dc2c16d
  3. BenTaylorDev commented on Jul 15, 2026

    @BenTaylorDev
    Contributor

    Cross-linking #3928 — same root-cause family. Both stem from missing idempotency/dedup when tool-call events are applied/rendered (upstream @ag-ui/client defaultApplyEvents has no duplicate guard). This issue is the cross-message facet (a second assistant message with the same id); #3928 is the within-toolCalls-array facet (same toolCallId twice → React duplicate-key warning). A proper fix likely needs both: union tool calls when merging duplicate-id messages, and dedup toolCalls in CopilotChatToolCallsView.

  4. linear commented on Jul 21, 2026

    @linear
  5. added
    reserved for students
    on Sep 3, 2026
  6. self-assigned this
    on Sep 30, 2026
  7. tylerslaton commented on Sep 30, 2026

    @tylerslaton
    Contributor

    This issue is already claimed externally. Please don’t start new work on it. If you need this fixed immediately, please reach out to us in our Discord or via [email protected].

  8. miah7 commented on Nov 12, 2026

    @miah7
    Contributor

    Hi Im a CS146S student commenting to work on issue!

  9. added a commit that references this issue on Nov 16, 2026
    44b265b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugreserved for students

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions