Skip to content

useChat assistant message.id changes mid-stream for Anthropic tool-first responses #477

@shaked-frame

Description

@shaked-frame

TanStack AI version

latest - 0.10.3

Framework/Library version

19.2.4

Describe the bug and the steps to reproduce it

When using useChat with fetchServerSentEvents and the Anthropic adapter, assistant message IDs can change during a single streamed response when tool calls arrive before text.
This makes UIMessage.id unstable for React list keys and can remount the message subtree mid-stream.
Setup
Client:

const { messages } = useChat({
  connection: fetchServerSentEvents('/api/reports/ask'),
})
return messages.map((msg) => (
  <ReportMessage key={msg.id} message={msg} />
))
Server:
const stream = chat({
  adapter: anthropicText('claude-sonnet-4-6'),
  messages,
  tools: [myServerTool],
})
return toServerSentEventsResponse(stream)

Your Minimal, Reproducible Example - (Sandbox Highly Recommended)

https://stackblitz.com/~/github.com/shaked-frame/ts-ai-reproduction

Screenshots or Videos (Optional)

example logs during streaming -

assistant|id=msg-1776683523350-hhod09           |key=1776683523350     during stream
assistant|id=anthropic-1776683527840-o09nzv     |key=1776683523350   after server confirms
Image

Do you intend to try to help solve this bug with your own PR?

Yes, I think I know how to fix it and will discuss it in the comments of this issue

Terms & Code of Conduct

  • I agree to follow this project's Code of Conduct
  • I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions