[BUG] Duplicate React key warning from assistant messages in chat-messages.tsx
Is there an existing issue for this?
- [X] I have searched the existing issues
Vercel Runtime Logs
- [ ] I have checked the Vercel Runtime Logs for errors (if applicable)
Current Behavior
React emits the following warning multiple times during a normal chat exchange:
Encountered two children with the same key, '<assistantMessage.id>'.
Keys should be unique so that components maintain their identity across updates.
Non-unique keys may cause children to be duplicated and/or omitted —
the behavior is unsupported and could change in a future version.
at div (<anonymous>)
at <unknown> (components/chat-messages.tsx:208:17)
at Array.map (<anonymous>)
Two assistant messages with the same id end up in the same ChatSection.assistantMessages array. The same warning is logged once per render (typically 5–10 times) because both messages keep re-rendering.
Expected Behavior
Each assistant message rendered under a given user turn should have a unique React key.
Steps To Reproduce
This is a race condition and not perfectly deterministic, but it surfaces reliably under either of these patterns:
- High-traffic path: ask any question that produces tool calls (search / fetch) followed by a long answer. The warning appears in ~30% of runs on my machine.
- Concurrent-dispatch path (always reproduces): see #851. Clicking a Related button mid-stream guarantees two same-id assistant messages.
Environment
- OS: macOS 14
- Browser: Chrome 138
- Morphic: current
main(commit6df470c) - Node / Bun: bun 1.2.12
- AI SDK:
[email protected],@ai-sdk/[email protected]
Anything else?
Source of the duplicate — useChat from @ai-sdk/[email protected] (sitting on top of [email protected]) occasionally emits the same assistant message twice in its messages array during stream finalization. The exact failure mode lives in the AI SDK, not in Morphic.
Two possible places to address it:
-
Upstream fix — file at
vercel/ai. This is the proper home but may take time. -
Defensive dedupe in Morphic —
components/chat.tsx's sections builder iteratesmessagesin order; adding aseenIds = new Set<string>()and skipping previously-seen ids costs ~3 lines and would suppress the warning regardless of upstream behavior (including the #851 path until that's fixed):const seenIds = new Set<string>() for (const message of messages) { if (seenIds.has(message.id)) continue seenIds.add(message.id) // ... existing user/assistant branching }
I have the defensive patch ready locally. Happy to send it as a PR if a downstream guard sounds acceptable, or to instead file the bug upstream against vercel/ai and link it back here. Either way, wanted to flag the warning since it's noisy in production logs.
PR #854 implements the defensive downstream dedupe described above. It's intentionally scoped to morphic's render layer only — the proper fix still belongs in @ai-sdk/react. If you'd rather pursue an upstream-only fix, feel free to close the PR and I'll file against vercel/ai.
Mitigated by #853, #856, and #854.
The reliable concurrent-dispatch path was blocked by the streaming guard changes, and #854 adds a defensive render-layer dedupe so duplicate message ids no longer produce duplicate React keys. If this resurfaces after these changes, we should track it as a separate upstream AI SDK issue with a fresh reproduction.