Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/openai/lib/streaming/chat/_completions.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ def current_completion_snapshot(self) -> ParsedChatCompletionSnapshot:

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
Expand Down Expand Up @@ -234,6 +236,8 @@ def current_completion_snapshot(self) -> ParsedChatCompletionSnapshot:

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
Expand Down Expand Up @@ -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
Loading