Skip to content

Commit 8401cb8

Browse files
committed
add simple filter
1 parent f66d2e6 commit 8401cb8

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/openai/lib/streaming/chat/_completions.py

+13
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ def current_completion_snapshot(self) -> ParsedChatCompletionSnapshot:
113113

114114
def __stream__(self) -> Iterator[ChatCompletionStreamEvent[ResponseFormatT]]:
115115
for sse_event in self._raw_stream:
116+
if not _is_valid_chat_completion_chunk_weak(sse_event):
117+
continue
116118
events_to_fire = self._state.handle_chunk(sse_event)
117119
for event in events_to_fire:
118120
yield event
@@ -234,6 +236,8 @@ def current_completion_snapshot(self) -> ParsedChatCompletionSnapshot:
234236

235237
async def __stream__(self) -> AsyncIterator[ChatCompletionStreamEvent[ResponseFormatT]]:
236238
async for sse_event in self._raw_stream:
239+
if not _is_valid_chat_completion_chunk_weak(sse_event):
240+
continue
237241
events_to_fire = self._state.handle_chunk(sse_event)
238242
for event in events_to_fire:
239243
yield event
@@ -753,3 +757,12 @@ def _convert_initial_chunk_into_snapshot(chunk: ChatCompletionChunk) -> ParsedCh
753757
},
754758
),
755759
)
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

0 commit comments

Comments
 (0)