ai
ai copied to clipboard
Duplicate streamText Assistant Messages with Client Tool Continuations
Description
Bug Report: Duplicate Assistant Messages with Client Tool Continuations
Summary
When using client-side tools with sendAutomaticallyWhen, the assistant message gets duplicated in the client state instead of being updated.
Steps to Reproduce
- Send a user message that triggers server-side tool calls requiring client execution
- Client tools execute and
sendAutomaticallyWhenreturnstrue - Second request is automatically sent back to server with tool results
- streamText adds a new Assistant message then finishes it.
Expected Behavior
The existing assistant message should be updated, not duplicated.
Actual Behavior
The client useChat receives a duplicate assistant message that is added to the client state.
Root Cause
In packages/ai/src/generate-text/stream-text.ts at line 1127:
let stepResponse: { id: string; timestamp: Date; modelId: string } = {
id: generateId(), // ← Always generates new ID
timestamp: currentDate(),
modelId: model.modelId,
};
When streamText handles client tool continuations, it unconditionally generates a new message ID instead of reusing the existing assistant message ID from the conversation history. This causes the client's replaceLastMessage logic in chat.ts to fail, resulting in pushMessage being called instead.
Even with a hack of generateId to return the existing id of the assistant message it still adds a second message to the stream state messages creating an issue with saving the messages onFinish
Additional Context
- This appears to be a regression from pre-AI SDK 5 behavior
- Workaround attempted: overriding
generateIdto return existing message ID, but this still creates duplicates - The issue occurs because
streamTextlacks awareness that it's continuing an existing assistant message rather than starting a new one
Suggested Fix
For client tool continuations, streamText should detect when the last message in history is an assistant message with pending client tools and reuse that message's ID instead of generating a new one.
Environment
- AI SDK version: 5.x
- Framework: [Your framework here]
- Client transport: Custom DefaultChatTranspor
AI SDK Version
ai: 5.0.0 ai-sdk/react: 2.0.0 next: 15.2.3
Please ensure you pass the original messages into the ui message stream generation to have stable ids.
https://ai-sdk.dev/docs/ai-sdk-ui/chatbot-message-persistence#message-ids
Thank you, I am still getting irregular behavior, in my on finish still duplicate messages passing the orignalMessages param this time the whole thread gets duplicated.
I'm also facing the same issue, temporarily handled this by custom logic. Expecting a fix from the upcoming updates.
This will help
https://ai-sdk.dev/docs/troubleshooting/repeated-assistant-messages
We’re using Vercel AI SDK on the backend with streamText(...) and running into the same issue: assistant messages are duplicated instead of continued.
I also have this annoying error, I think this is urgent. I already pass the original messages array to toUIMessageStreamResponse but it had no effect.
+1 client side tools are unusable without patching
I ran into this issue. It seems the bug is at client side. I got three different ids in duplicated messages and network request
The last message in the second request:
id: "b8dmxricdy56ghhvqxn4a1uq", role: "assistant"
and server response as expected
But finally received tow duplicated messages with id mqgp89zz9yxnk7ejj2jrcp2e and jcmb71yvt2crrfa31o67mjgx
I ran into this issue. It seems the bug is at client side. I got three different ids in duplicated messages and network request
The last message in the second request:
id: "b8dmxricdy56ghhvqxn4a1uq", role: "assistant"and server response as expected
But finally received tow duplicated messages with id
mqgp89zz9yxnk7ejj2jrcp2eandjcmb71yvt2crrfa31o67mjgx
Cheers. I resolved my bug. Not caused by ai sdk.
It is caused by some legacy code modifying message.id inside onFinish callback
I can confirm this works fine https://ai-sdk.dev/docs/troubleshooting/repeated-assistant-messages
Will try again, this fix didnt work at the time of posting
The fix of passing original messages to stream generator is still not working in ai 5.0.86, ai sdk 2.0.86
can someone reproduce a minimal reproducible example? It will help us prioritize working on this issue
