From a75ff6e7911768d940d06afff335e45a211220a3 Mon Sep 17 00:00:00 2001 From: Pon Pongwachirin Date: Sat, 11 Jan 2025 19:06:18 +0700 Subject: [PATCH 1/2] fix: add a check to handle empty or newline-only strings before calling `from_json` --- src/openai/lib/streaming/chat/_completions.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/openai/lib/streaming/chat/_completions.py b/src/openai/lib/streaming/chat/_completions.py index 2146091354..1ff7dea431 100644 --- a/src/openai/lib/streaming/chat/_completions.py +++ b/src/openai/lib/streaming/chat/_completions.py @@ -434,6 +434,7 @@ def _accumulate_chunk(self, chunk: ChatCompletionChunk) -> ParsedChatCompletionS choice_snapshot.message.content and not choice_snapshot.message.refusal and is_given(self._rich_response_format) + and choice_snapshot.message.content.strip() # Add filter to ensure no parsing errors ): choice_snapshot.message.parsed = from_json( bytes(choice_snapshot.message.content, "utf-8"), From ce2c36c304adce5d19b5d05ab7e04365e7a78296 Mon Sep 17 00:00:00 2001 From: SenorSpes <138108569+senorNox@users.noreply.github.com> Date: Wed, 15 Jan 2025 08:27:36 +0700 Subject: [PATCH 2/2] style: adjust comment format for better readability Co-authored-by: Robert Craigie --- src/openai/lib/streaming/chat/_completions.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/openai/lib/streaming/chat/_completions.py b/src/openai/lib/streaming/chat/_completions.py index 1ff7dea431..4623e16306 100644 --- a/src/openai/lib/streaming/chat/_completions.py +++ b/src/openai/lib/streaming/chat/_completions.py @@ -434,7 +434,8 @@ def _accumulate_chunk(self, chunk: ChatCompletionChunk) -> ParsedChatCompletionS choice_snapshot.message.content and not choice_snapshot.message.refusal and is_given(self._rich_response_format) - and choice_snapshot.message.content.strip() # Add filter to ensure no parsing errors + # partial parsing fails on white-space + and choice_snapshot.message.content.strip() ): choice_snapshot.message.parsed = from_json( bytes(choice_snapshot.message.content, "utf-8"),