Chat response stream hangup #4799
Replies: 3 comments 2 replies
-
Hi @thingersoft I moved this to discussions because I can't reproduce it, and I'm using the app daily. Would need more information like endpoint/provider/model and other relevant chat settings used to help reproduce. The SyntaxError is to be expected when there's a malformed stream chunk from the API. The SSE code is by Maxime Petazzoni and used in place of EventSource for these reasons:
I have not updated it since it was first implemented (at the very start of the project). At the time, there was no other way to initiate server-sent events via POST request that I knew how via the native EventSource method. Maybe we can do that if some of these issues are then resolved, and if not, they can be addressed here: https://github.com/mpetazzoni/sse.js/ What I have in https://github.com/danny-avila/LibreChat/blob/main/packages/data-provider/src/sse.js I forget why I chose to do it this way instead of including it as a package. |
Beta Was this translation helpful? Give feedback.
-
Hello @danny-avila I think EventSource API doesn's support POST on purpose since clients shouldn't expect a stream of events in response to server mutations. Using the right http verbs has security implications and the reason behind choosing POST, PUT PATCH, etc. rather than GET shouldn't be "my query string is too long". If for some reason one absolutely needs to use POST for a query than there's probably something wrong with the API design in first place. Having said that, in the case of LibreChat I guess that sending chat messages actually causes server mutations, so using POST is the right choice indeed. Imho the sending message request should be decoupled from the progress/stream request, the first one being a POST and the second one a GET. Anyway looking at the sse.js code currently in use by LibreChat I see at least two points of failure.
By a quick look at the mpetazzoni repository these aspects haven't been fixed yet so I'll open an issue there. Anyway I'm still trying to figure out how to reproduce the scenario of two chunks collapsed into one, I'll get back as soon as I can. |
Beta Was this translation helpful? Give feedback.
-
@danny-avila It's a pretty edge case, anyway it sounds strange that you you never experienced it, may it's due to different network characteristics? The cleanest solution would be to replace the embedded version of sse.js with the external up-to-date dependency. I'll try to come up with a PR asap, in the meantime I think the discussion can be converted back into an issue. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
What happened?
Chat response stream sometimes gets stuck logging multiple "Unexpected non-whitespace character after JSON ..." messages in console.
Most often it happens with lenghty responses and structured content.
The problem resides in
/packages/data-provider/src/sse.js
The code listens to the
progress
event ofXMLHttpRequest
to extract the chunks from the response and produce a custom "message" event for each json chunk./client/src/hooks/SSE/useSSE.ts
defines a listener for the "message" event that crashes as it tries to parse the (expected) json from the "message" event data.When the first error occurs console log shows that the "message" event data field contains two concatenated json chunks rather than a single one.
After that the data field keeps growing, potentially leading to browser crashes (probably related to #4417 also).
Don't know why the client is listening on the
progress
event ofXMLHttpRequest
rather than using the nativeEventSource
API, doing asubstring
on the whole response body at each step isn't great from a performance point of view.Also the whole event dispatching logic looks pretty brittle to me, concatenating strings to get the "registered" listener to invoke.
@danny-avila
I would like to produce a PR but rather than simply fixing the bug I would replace the client side streaming logic to leverage the
EventSource
API.Steps to Reproduce
What browsers are you seeing the problem on?
No response
Relevant log output
No response
Screenshots
Code of Conduct
Beta Was this translation helpful? Give feedback.
All reactions