Commit e3e791c8
Changed files (1)
src
openai
lib
streaming
chat
src/openai/lib/streaming/chat/_completions.py
@@ -113,6 +113,8 @@ class ChatCompletionStream(Generic[ResponseFormatT]):
def __stream__(self) -> Iterator[ChatCompletionStreamEvent[ResponseFormatT]]:
for sse_event in self._raw_stream:
+ if not _is_valid_chat_completion_chunk_weak(sse_event):
+ continue
events_to_fire = self._state.handle_chunk(sse_event)
for event in events_to_fire:
yield event
@@ -234,6 +236,8 @@ class AsyncChatCompletionStream(Generic[ResponseFormatT]):
async def __stream__(self) -> AsyncIterator[ChatCompletionStreamEvent[ResponseFormatT]]:
async for sse_event in self._raw_stream:
+ if not _is_valid_chat_completion_chunk_weak(sse_event):
+ continue
events_to_fire = self._state.handle_chunk(sse_event)
for event in events_to_fire:
yield event
@@ -753,3 +757,12 @@ def _convert_initial_chunk_into_snapshot(chunk: ChatCompletionChunk) -> ParsedCh
},
),
)
+
+
+def _is_valid_chat_completion_chunk_weak(sse_event: ChatCompletionChunk) -> bool:
+ # Although the _raw_stream is always supposed to contain only objects adhering to ChatCompletionChunk schema,
+ # this is broken by the Azure OpenAI in case of Asynchronous Filter enabled.
+ # An easy filter is to check for the "object" property:
+ # - should be "chat.completion.chunk" for a ChatCompletionChunk;
+ # - is an empty string for Asynchronous Filter events.
+ return sse_event.object == "chat.completion.chunk" # type: ignore # pylance reports this as a useless check