Skip to content

SSE Stream parser expects additional space after colon "data:" #498

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

Closed
rick-li opened this issue Jun 22, 2023 · 0 comments · Fixed by #559
Closed

SSE Stream parser expects additional space after colon "data:" #498

rick-li opened this issue Jun 22, 2023 · 0 comments · Fixed by #559
Labels
bug Something isn't working

Comments

@rick-li
Copy link

rick-li commented Jun 22, 2023

Describe the bug

In openai-python the SSE parser always check the additional space after "data:"

if line.startswith(b"data: "):

But according to the SSE spec "data: " is identical to "data:"
https://html.spec.whatwg.org/multipage/server-sent-events.html#server-sent-events
`The following stream fires two identical events:
data:test

data: test
This is because the space after the colon is ignored if present.`

The use case is some organizations might proxy the response for additional control but not all library put a whitespace after the colon ex. springframework https://github.com/spring-projects/spring-framework/blob/f06cf21341a35f863b327e0bfe2305111f69c468/spring-web/src/main/java/org/springframework/http/codec/ServerSentEventHttpMessageWriter.java#L146
if (data != null) { sb.append("data:"); }

To Reproduce

Proxy OpenAI API with spring framework

Code snippets

No response

OS

All

Python version

All

Library version

All

@rick-li rick-li added the bug Something isn't working label Jun 22, 2023
AshiishKarhade added a commit to AshiishKarhade/openai-python that referenced this issue Jun 25, 2023
…#498

This commit updates the SSE parser to handle cases where there is an optional space after the "data:" prefix. Described in detail here - openai#498

To address this, I modified the parser code to use the `startswith` method and then strip any leading or trailing whitespaces from the line. This ensures that the parser can handle both cases where there is a space after "data:" and where there isn't. The modified code now correctly decodes the line as UTF-8 and returns the parsed data. 

This change improves compatibility with different SSE implementations and ensures that the parser functions correctly in various scenarios. It provides a more flexible solution that aligns with the SSE specification and accommodates libraries like springframework that omit the space after "data:".

Fixes openai#498
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
1 participant