fix: guard against None chunks in OpenAI streaming client#7903
Open
jahanzaib-iqbal-dev wants to merge 1 commit into
Open
fix: guard against None chunks in OpenAI streaming client#7903jahanzaib-iqbal-dev wants to merge 1 commit into
jahanzaib-iqbal-dev wants to merge 1 commit into
Conversation
Fixes AttributeError when the streaming response yields a None chunk before the None check is performed. Added early continue guard to skip None chunks in the streaming loop, placed before any attribute access on the chunk object. Also removes a redundant `maybe_model = chunk.model` assignment on line 949 (original) that duplicated the assignment already made earlier in the loop body. Fixes microsoft#7130
Author
|
@microsoft-github-policy-service agree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes
AttributeError: 'NoneType' object has no attribute 'model'that occurs when the OpenAI streaming response yields aNonechunk.Noneguard at the top of the streaming loop (before any attribute access) soNonechunks are silently skipped.maybe_model = chunk.modelassignment that appeared later in the same loop body (the assignment already happens earlier in the loop).Root Cause
In
_openai_client.py,chunk.modelwas accessed before checking ifchunkisNone. When the API yields aNonesentinel chunk (observed in compiled binaries and under heavy endpoint load), this caused an unhandledAttributeErrorthat crashed the streaming session:The type annotation on line 893 (
chunk: ChatCompletionChunk | None = None) already documents thatchunkcan beNone, but the guard was missing.Fix
Testing
test_openai_chat_completion_client_create_stream_none_chunkintest_openai_model_client.pywhich injectsNonechunks at both the start and in the middle of the stream and verifies the stream completes successfully without raisingAttributeError.Fixes #7130