From d820e93ad96233210c0dd2632497fc2167a0ecdb Mon Sep 17 00:00:00 2001 From: Michael Feil <63565275+michaelfeil@users.noreply.github.com> Date: Thu, 3 Aug 2023 17:50:07 +0200 Subject: [PATCH 1/2] Update api_requestor.py --- openai/api_requestor.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/openai/api_requestor.py b/openai/api_requestor.py index 504f7c4411..ba720c2423 100644 --- a/openai/api_requestor.py +++ b/openai/api_requestor.py @@ -98,16 +98,18 @@ def _make_session() -> requests.Session: def parse_stream_helper(line: bytes) -> Optional[str]: - if line: - if line.strip() == b"data: [DONE]": - # return here will cause GeneratorExit exception in urllib3 - # and it will close http connection with TCP Reset - return None + if line and line.startswith(b"data:"): if line.startswith(b"data: "): + # SSE event may be valid when it contains leading whitespace line = line[len(b"data: "):] - return line.decode("utf-8") else: + line = line[len(b"data:"):] + if line.strip() in b"[DONE]": + # return here will cause GeneratorExit exception in urllib3 + # and it will close http connection with TCP Reset return None + else: + return line.decode("utf-8") return None From dc9af50c18ff54632c5e6f605fe76080c9f2244f Mon Sep 17 00:00:00 2001 From: Michael Feil <63565275+michaelfeil@users.noreply.github.com> Date: Thu, 3 Aug 2023 17:55:03 +0200 Subject: [PATCH 2/2] fix: SSE event for api_requestor.py --- openai/api_requestor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/openai/api_requestor.py b/openai/api_requestor.py index ba720c2423..0b44949839 100644 --- a/openai/api_requestor.py +++ b/openai/api_requestor.py @@ -100,11 +100,11 @@ def _make_session() -> requests.Session: def parse_stream_helper(line: bytes) -> Optional[str]: if line and line.startswith(b"data:"): if line.startswith(b"data: "): - # SSE event may be valid when it contains leading whitespace + # SSE event may be valid when it contain whitespace line = line[len(b"data: "):] else: line = line[len(b"data:"):] - if line.strip() in b"[DONE]": + if line.strip() == b"[DONE]": # return here will cause GeneratorExit exception in urllib3 # and it will close http connection with TCP Reset return None