File tree 1 file changed +13
-0
lines changed
src/openai/lib/streaming/chat
1 file changed +13
-0
lines changed Original file line number Diff line number Diff line change @@ -113,6 +113,8 @@ def current_completion_snapshot(self) -> ParsedChatCompletionSnapshot:
113
113
114
114
def __stream__ (self ) -> Iterator [ChatCompletionStreamEvent [ResponseFormatT ]]:
115
115
for sse_event in self ._raw_stream :
116
+ if not _is_valid_chat_completion_chunk_weak (sse_event ):
117
+ continue
116
118
events_to_fire = self ._state .handle_chunk (sse_event )
117
119
for event in events_to_fire :
118
120
yield event
@@ -234,6 +236,8 @@ def current_completion_snapshot(self) -> ParsedChatCompletionSnapshot:
234
236
235
237
async def __stream__ (self ) -> AsyncIterator [ChatCompletionStreamEvent [ResponseFormatT ]]:
236
238
async for sse_event in self ._raw_stream :
239
+ if not _is_valid_chat_completion_chunk_weak (sse_event ):
240
+ continue
237
241
events_to_fire = self ._state .handle_chunk (sse_event )
238
242
for event in events_to_fire :
239
243
yield event
@@ -753,3 +757,12 @@ def _convert_initial_chunk_into_snapshot(chunk: ChatCompletionChunk) -> ParsedCh
753
757
},
754
758
),
755
759
)
760
+
761
+
762
+ def _is_valid_chat_completion_chunk_weak (sse_event : ChatCompletionChunk ) -> bool :
763
+ # Although the _raw_stream is always supposed to contain only objects adhering to ChatCompletionChunk schema,
764
+ # this is broken by the Azure OpenAI in case of Asynchronous Filter enabled.
765
+ # An easy filter is to check for the "object" property:
766
+ # - should be "chat.completion.chunk" for a ChatCompletionChunk;
767
+ # - is an empty string for Asynchronous Filter events.
768
+ return sse_event .object == "chat.completion.chunk" # type: ignore # pylance reports this as a useless check
You can’t perform that action at this time.
0 commit comments