Skip to content

Commit 8944bd1

Browse files
authored
Fix: SSE Stream parser expects additional space after colon "data:" (#559)
* Update api_requestor.py * fix: SSE event for api_requestor.py
1 parent 7fba4dc commit 8944bd1

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

Diff for: openai/api_requestor.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,18 @@ def _make_session() -> requests.Session:
9898

9999

100100
def parse_stream_helper(line: bytes) -> Optional[str]:
101-
if line:
102-
if line.strip() == b"data: [DONE]":
103-
# return here will cause GeneratorExit exception in urllib3
104-
# and it will close http connection with TCP Reset
105-
return None
101+
if line and line.startswith(b"data:"):
106102
if line.startswith(b"data: "):
103+
# SSE event may be valid when it contain whitespace
107104
line = line[len(b"data: "):]
108-
return line.decode("utf-8")
109105
else:
106+
line = line[len(b"data:"):]
107+
if line.strip() == b"[DONE]":
108+
# return here will cause GeneratorExit exception in urllib3
109+
# and it will close http connection with TCP Reset
110110
return None
111+
else:
112+
return line.decode("utf-8")
111113
return None
112114

113115

0 commit comments

Comments
 (0)