Fix #7130 - Add None chunk guard in OpenAI create_stream to prevent AttributeError#7856
Open
Whning0513 wants to merge 1 commit into
Open
Fix #7130 - Add None chunk guard in OpenAI create_stream to prevent AttributeError#7856Whning0513 wants to merge 1 commit into
Whning0513 wants to merge 1 commit into
Conversation
…revent AttributeError
Contributor
|
@Whning0513 please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
Author
|
@microsoft-github-policy-service agree |
2 similar comments
Author
|
@microsoft-github-policy-service agree |
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.
AttributeError in streaming due to missing None check on chunk.model
The
BaseOpenAIChatCompletionClient.create_stream()method crashes withAttributeError: 'NoneType' object has no attribute 'model'when the OpenAI stream yieldsNonechunks. This is especially prevalent in compiled binaries (PyInstaller/Nuitka) and causes premature stream termination. The type annotation already indicatedchunkcould beNone, but the code accessedchunk.modelbefore checking forNone.Reported by: abhijaysingh
In the
_create_stream_chunksmethod of_openai_client.py, the async iteration over stream chunks accessedchunk.model(line 919) before anyNonecheck. While empty chunk checks existed later in the code, theNoneguard was missing entirely despite the type annotation at line 893 declaringchunk: ChatCompletionChunk | None = None.File:
autogen/python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.pyAdded a
if chunk is None: continueguard at line 909, right at the top of the async for loop that processes stream chunks. This ensuresNonechunks (which can be emitted by the OpenAI API as keepalives/heartbeats or under load) are silently skipped before any attribute access occurs.Low - The change is a single defensive guard that skips
Nonechunks. This aligns with the existing type annotation and does not alter any valid processing behavior. All existing tests continue to pass.