ai icon indicating copy to clipboard operation
ai copied to clipboard

Duplicate streamText Assistant Messages with Client Tool Continuations

Open cgilly2fast opened this issue 4 months ago • 12 comments

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

  1. Send a user message that triggers server-side tool calls requiring client execution
  2. Client tools execute and sendAutomaticallyWhen returns true
  3. Second request is automatically sent back to server with tool results
  4. 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 generateId to return existing message ID, but this still creates duplicates
  • The issue occurs because streamText lacks 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

cgilly2fast avatar Aug 02 '25 17:08 cgilly2fast

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

lgrammel avatar Aug 04 '25 08:08 lgrammel

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.

cgilly2fast avatar Aug 04 '25 17:08 cgilly2fast

I'm also facing the same issue, temporarily handled this by custom logic. Expecting a fix from the upcoming updates.

thamizhanban666 avatar Sep 01 '25 04:09 thamizhanban666

This will help

https://ai-sdk.dev/docs/troubleshooting/repeated-assistant-messages

buzo1234 avatar Sep 10 '25 17:09 buzo1234

We’re using Vercel AI SDK on the backend with streamText(...) and running into the same issue: assistant messages are duplicated instead of continued.

nareshtank001 avatar Sep 15 '25 06:09 nareshtank001

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.

trfi avatar Sep 20 '25 12:09 trfi

+1 client side tools are unusable without patching

brenbitY2K avatar Oct 19 '25 14:10 brenbitY2K

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 Image

But finally received tow duplicated messages with id mqgp89zz9yxnk7ejj2jrcp2e and jcmb71yvt2crrfa31o67mjgx

intellild avatar Oct 23 '25 05:10 intellild

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 Image

But finally received tow duplicated messages with id mqgp89zz9yxnk7ejj2jrcp2e and jcmb71yvt2crrfa31o67mjgx

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

intellild avatar Oct 23 '25 08:10 intellild

Will try again, this fix didnt work at the time of posting

cgilly2fast avatar Oct 25 '25 20:10 cgilly2fast

The fix of passing original messages to stream generator is still not working in ai 5.0.86, ai sdk 2.0.86

ChunxuYang avatar Nov 08 '25 20:11 ChunxuYang

can someone reproduce a minimal reproducible example? It will help us prioritize working on this issue

gr2m avatar Nov 11 '25 03:11 gr2m