Python: Fix response_format crash on background polling with empty text#5146
Open
eavanvalkenburg wants to merge 2 commits intomicrosoft:mainfrom
Open
Python: Fix response_format crash on background polling with empty text#5146eavanvalkenburg wants to merge 2 commits intomicrosoft:mainfrom
response_format crash on background polling with empty text#5146eavanvalkenburg wants to merge 2 commits intomicrosoft:mainfrom
Conversation
…ft#5145) When using response_format with background=True (Responses API), polling an in-progress response produces empty text. _parse_structured_response_value unconditionally passed this to model_validate_json/json.loads, causing ValidationError or JSONDecodeError. Add an early return of None when text is empty, matching the existing guard for response_format=None. This allows .value to safely return None for in-progress background responses. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
eavanvalkenburg
commented
Apr 7, 2026
Member
Author
eavanvalkenburg
left a comment
There was a problem hiding this comment.
Automated Code Review
Reviewers: 4 | Confidence: 98% | Result: All clear
Reviewed: Correctness, Security Reliability, Test Coverage, Design Approach
Automated review by eavanvalkenburg's agents
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a crash in the Python core SDK when polling background Responses API runs with response_format enabled, where structured parsing was attempted against an empty in-progress .text.
Changes:
- Add an early-return guard in
_parse_structured_response_valuewhentextis empty. - Add regression tests covering empty-text behavior for both Pydantic and mapping schema formats, including
ChatResponseandAgentResponsewrappers.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| python/packages/core/agent_framework/_types.py | Prevents structured parsing from running on empty in-progress text during polling by returning None early. |
| python/packages/core/tests/core/test_types.py | Adds regression coverage ensuring empty text with response_format returns None (and does not raise) across parsing paths and response wrappers. |
Contributor
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
chetantoshniwal
approved these changes
Apr 7, 2026
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
When using
response_formatwith a Pydantic model or dict schema together withbackground=True, the SDK crashes during polling because_parse_structured_response_valueis called with an empty string while the response is still in-progress, causingmodel_validate_json("")to raiseValidationErrorandjson.loads("")to raiseJSONDecodeError.Fixes #5145
Description
The root cause is that
_parse_structured_response_valueunconditionally attempts to parse the text even when it is empty, which happens on every poll iteration before the background response completes. The fix adds an early return ofNonewhentextis falsy, before reaching either the Pydantic or dict-schema parsing branches. This allows.valueto safely returnNoneon in-progress polls and the fully parsed result once the response is complete. Regression tests cover both the Pydantic and mapping schema paths as well as theChatResponseandAgentResponsewrappers.Contribution Checklist