ai icon indicating copy to clipboard operation
ai copied to clipboard

StreamUI unhandledRejection: Error: .update(): UI stream is already closed.

Open ifreeman6 opened this issue 1 year ago • 6 comments

Description

The StreamUI calling tool reports an error, but the normal output components:

Error: .update(): UI stream is already closed. at assertStream (G:\ai-project\ai-chatbot.next\server\chunks\ssr_39f80c..js:12394:19) at Object.update (G:\ai-project\ai-chatbot.next\server\chunks\ssr_39f80c..js:12412:13) at handleRender (G:\ai-project\ai-chatbot.next\server\chunks\ssr_39f80c..js:12830:21) at G:\ai-project\ai-chatbot.next\server\chunks\ssr_39f80c..js:12870:29 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) ⨯ unhandledRejection: Error: .update(): UI stream is already closed. at assertStream (G:\ai-project\ai-chatbot.next\server\chunks\ssr_39f80c..js:12394:19) at Object.update (G:\ai-project\ai-chatbot.next\server\chunks\ssr_39f80c..js:12412:13) at handleRender (G:\ai-project\ai-chatbot.next\server\chunks\ssr_39f80c..js:12830:21) at G:\ai-project\ai-chatbot.next\server\chunks\ssr_39f80c..js:12870:29 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) ⨯ unhandledRejection: Error: .update(): UI stream is already closed. at assertStream (G:\ai-project\ai-chatbot.next\server\chunks\ssr_39f80c..js:12394:19) at Object.update (G:\ai-project\ai-chatbot.next\server\chunks\ssr_39f80c..js:12412:13) at handleRender (G:\ai-project\ai-chatbot.next\server\chunks\ssr_39f80c..js:12830:21) at G:\ai-project\ai-chatbot.next\server\chunks\ssr_39f80c..js:12870:29 at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Code example

No response

Additional context

No response

ifreeman6 avatar Sep 14 '24 09:09 ifreeman6

Hi @lgrammel Any solution for this issue? My app has been encountering this issue quite frequently lately which made many paid customers frustrated/churned. This issue started occurring several weeks ago already. I couldn't find any workaround fix on my own. This is very urgent. Please help.

sylviangth avatar Sep 17 '24 15:09 sylviangth

This happens when close() has been called on a streamableUI already, e.g. when you have parallel tool calls. streamUI uses this internally, so the cause can be parallel tool calls by the AI.

First try to turn off parallel tool calls (especially w/ openai - see OpenAI provider https://sdk.vercel.ai/providers/ai-sdk-providers/openai ). If that is not sufficient, you can use streamText with your own createStreamableUI instance to have more control, or switch to AI SDK UI.

lgrammel avatar Sep 17 '24 15:09 lgrammel

This happens when close() has been called on a streamableUI already, e.g. when you have parallel tool calls. streamUI uses this internally, so the cause can be parallel tool calls by the AI.

First try to turn off parallel tool calls (especially w/ openai - see OpenAI provider https://sdk.vercel.ai/providers/ai-sdk-providers/openai ). If that is not sufficient, you can use streamText with your own createStreamableUI instance to have more control, or switch to AI SDK UI.

@lgrammel How can I turn off parallelToolCalls with createOpenAI? I tried this but it says 'parallelToolCalls' does not exist in type 'OpenAICompletionSettings'

Is there anyway to disable it from the streamUI() function itself?

image

sylviangth avatar Sep 17 '24 17:09 sylviangth

It is available when you use chat models (completion models don't support tool calls to begin with): https://sdk.vercel.ai/providers/ai-sdk-providers/openai#chat-models

lgrammel avatar Sep 17 '24 18:09 lgrammel

This happens when close() has been called on a streamableUI already, e.g. when you have parallel tool calls. streamUI uses this internally, so the cause can be parallel tool calls by the AI.

First try to turn off parallel tool calls (especially w/ openai - see OpenAI provider https://sdk.vercel.ai/providers/ai-sdk-providers/openai ). If that is not sufficient, you can use streamText with your own createStreamableUI instance to have more control, or switch to AI SDK UI.

It's not okey when I turn off the "parallelToolCalls" config, the error occurs as before.

const openai = createOpenAI({
  baseURL: process.env.OPENAI_API_BASE,
  apiKey: process.env.OPENAI_API_KEY,
})
let openaiApiModel = process.env.OPENAI_API_MODEL || 'gpt-4o'
let model = openai.chat(openaiApiModel, {
  parallelToolCalls: false
})

ifreeman6 avatar Sep 18 '24 01:09 ifreeman6

please try my other suggestions in that case, and also check out this comment: https://github.com/vercel/ai/issues/2810#issuecomment-2320982906

You are seeing this error because you are trying to update an already closed rsc stream (streamableUI).

lgrammel avatar Sep 18 '24 07:09 lgrammel

I think this is a bug. Please fix it in the next version.

ifreeman6 avatar Oct 24 '24 07:10 ifreeman6

When i upgrade the latest version of ai, the bug still exists.

ifreeman6 avatar Oct 24 '24 07:10 ifreeman6

Same issue here.

aaron5670 avatar Dec 17 '24 09:12 aaron5670