New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUGFIX] - useAssistant - Check for empty array #1184
base: main
Are you sure you want to change the base?
Conversation
There should always be a message at the point when text fragments start streaming in, so the root cause could be different. Can you share the code that caused this bug? One potential cause could be using |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Marking as on hold for now. The fix most likely needs to be more involved.
@lgrammel Ahh that's a good point. It's hard for me to share the entire code, but basically I have a setup similar to below: useEffect(() => {
if (selectedThread) {
fetchMessages()
} else {
setChatMessages([])
}
}, [selectedThread, setChatMessages])
const handleSendMessage = async () => {
let currentThread = selectedThread ? { ...selectedThread } : null
if (!currentThread) {
currentThread = await handleCreateThread(
user.id,
userInput,
chatSettings!,
selectedWorkspace!,
setSelectedThread,
setThreads
)
}
await submitMessage()
} I think my issue is that when I create a new thread in I can reconfigure my app so that it relies on the thread creation in the I am curious though, in this I am looking at the streaming code and it seems that |
The OpenAI stream sends a
|
@lgrammel Sorry, I'm still confused. Isn't ai/packages/core/streams/assistant-response.ts Lines 88 to 105 in fac9a6e
|
Yes, first there is a control data message, then the |
If the data control message comes first, then couldn't this fail since https://github.com/vercel/ai/blob/main/packages/core/react/use-assistant.ts#L212-L223 |
Yes, good point. This would need a check if there are any messages. |
@tchanxx Can you update the PR to only check in the control data part, and move the if check outside of setMessages? |
if (messages.length === 0) { | ||
return messages; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be checked before setMessages, e.g. like this:
if (messages.length > 0) {
setMessages ...
}
When I was using the
useAssistant
hook, I noticed this error:Unhandled Runtime Error TypeError: Cannot set properties of undefined (setting 'id')
This error was kind of cryptic since it doesn't say which element in
useAssistant
is causing this error. However, I figured out that it's due tolastMessage
being undefined in the following code:When
messages
has a length of0
, thenlastMessage
becomes undefined and then we get a runtime error atlastMessage.id
.