vercel / ai

Build AI-powered applications with React, Svelte, Vue, and Solid
https://sdk.vercel.ai/docs
Other
10.19k stars 1.54k forks source link

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

Open ifreeman6 opened 2 months ago

ifreeman6 commented 2 months ago

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/taskqueues: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/taskqueues: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

sylviangth commented 2 months ago

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.

lgrammel commented 2 months ago

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.

sylviangth commented 2 months ago

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
lgrammel commented 2 months ago

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

ifreeman6 commented 2 months ago

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
})
lgrammel commented 2 months ago

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).

ifreeman6 commented 1 month ago

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

ifreeman6 commented 1 month ago

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