Python: fix(openai): guard against null delta in streaming chunks from non-co…#5734
Open
Serjbory wants to merge 1 commit intomicrosoft:mainfrom
Open
Python: fix(openai): guard against null delta in streaming chunks from non-co…#5734Serjbory wants to merge 1 commit intomicrosoft:mainfrom
Serjbory wants to merge 1 commit intomicrosoft:mainfrom
Conversation
…mpliant providers (microsoft#5732)
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a streaming crash in the Python OpenAIChatCompletionClient when OpenAI-compatible providers emit non-spec finish chunks with "delta": null, by adding a single guard in the streaming chunk parser and covering the scenario with regression tests.
Changes:
- Add a
delta is Noneguard in_parse_response_update_from_openaito prevent downstream parsing from dereferencingNonewhile still capturingfinish_reason. - Update reasoning-details parsing to use the guarded local
deltareference. - Add regression tests for null-delta finish chunks (including finish reason preservation, usage coexistence, and tool-call/text parsing behavior).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| python/packages/openai/agent_framework_openai/_chat_completion_client.py | Adds a streaming-time delta=None guard and adjusts reasoning-details access to avoid AttributeError on non-compliant provider chunks. |
| python/packages/openai/tests/openai/test_openai_chat_completion_client.py | Adds regression tests reproducing and preventing the null-delta streaming crash, including finish-reason and usage behaviors. |
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.
Motivation and Context
Fixes #5732.
When using
OpenAIChatCompletionClientwith streaming, certain OpenAI-compatible providers (e.g. Azure OpenAI with specific configurations) send"delta": nullon finish chunks instead of the spec-compliant"delta": {}. This causes anAttributeError: 'NoneType' object has no attribute 'content'crash in_parse_text_from_openai, terminating the stream mid-response.Description
Added a runtime guard in
_parse_response_update_from_openaithat checks for aNonedelta before attempting to parse text, tool calls, or reasoning content from the streaming chunk.Key design decisions:
for choice in chunk.choicesloop) rather than in individual parsing methods, so all downstream content-parsing is protected by a single check.finish_reasonis extracted before the guard, ensuring terminal stream state is always captured even from non-compliant chunks.getattr(choice, "delta", None)instead ofchoice.deltato avoid a PyrightreportUnnecessaryComparisonerror, since the OpenAI SDK typesdeltaas non-optional despite it beingNoneat runtime from non-compliant providers.reasoning_detailsaccess was updated to use the localdeltavariable instead of re-accessingchoice.delta.Added 5 regression tests covering:
delta=Nonewithfinish_reason="stop")finish_reasonpreservation across different stop reasons ("length")ChoiceDelta()) is not skippedNoneContribution Checklist