Skip to content
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

Fix OpenAI chat stream hanging #156

Conversation

stevehodgkiss
Copy link
Contributor

@stevehodgkiss stevehodgkiss commented Jul 18, 2024

We recently noticed that chat streams would appear to "hang" in the middle of generating a response. After a bit of debugging I found that it was due to the message being split in the middle of the "data: " part (i.e. "dat" in the last message, then "a: {" in the next). This results in the stream failing to parse any future messages and appearing to "hang" or freeze.

Haven't seen this issue before so I'm guessing something has changed in the way OpenAI streams their responses.

I couldn't see any tests for this particular case in the official node & python clients, but I modified one of the node clients tests to check and it is supported (still passes). The node client appears to split by line endings first, then by : to get the field name (i.e. data).

The change in this PR pattern matches on their being something in the buffer (previous/partial message data), combines the buffered & new data then recursively calls the decode_stream function, since the combined data may need splitting again on the "data: " key.

Copy link
Owner

@brainlid brainlid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome @stevehodgkiss! Thanks! I just noticed that hanging behavior the other day but hadn't diagnosed it yet.

❤️💛💙💜

@brainlid brainlid merged commit 38c9ff5 into brainlid:main Jul 18, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants