From f63704ff508f73cee6aecde4743fc1cdb57e81de Mon Sep 17 00:00:00 2001 From: Ashish Karhade Date: Sun, 25 Jun 2023 08:04:19 +0530 Subject: [PATCH] Fix Stream parser expects additional space after colon "data:" #498 This commit updates the SSE parser to handle cases where there is an optional space after the "data:" prefix. Described in detail here - https://github.com/openai/openai-python/issues/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 #498 --- 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 504f7c4411..f942fb781d 100644 --- a/openai/api_requestor.py +++ b/openai/api_requestor.py @@ -103,8 +103,8 @@ def parse_stream_helper(line: bytes) -> Optional[str]: # return here will cause GeneratorExit exception in urllib3 # and it will close http connection with TCP Reset return None - if line.startswith(b"data: "): - line = line[len(b"data: "):] + if line.startswith(b"data:"): + line = line[len(b"data:"):] return line.decode("utf-8") else: return None