Skip to content

Python: Fix response_format crash on background polling with empty text#5146

Open
eavanvalkenburg wants to merge 2 commits intomicrosoft:mainfrom
eavanvalkenburg:agent/fix-5145-1
Open

Python: Fix response_format crash on background polling with empty text#5146
eavanvalkenburg wants to merge 2 commits intomicrosoft:mainfrom
eavanvalkenburg:agent/fix-5145-1

Conversation

@eavanvalkenburg
Copy link
Copy Markdown
Member

Motivation and Context

When using response_format with a Pydantic model or dict schema together with background=True, the SDK crashes during polling because _parse_structured_response_value is called with an empty string while the response is still in-progress, causing model_validate_json("") to raise ValidationError and json.loads("") to raise JSONDecodeError.

Fixes #5145

Description

The root cause is that _parse_structured_response_value unconditionally 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 of None when text is falsy, before reaching either the Pydantic or dict-schema parsing branches. This allows .value to safely return None on 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 the ChatResponse and AgentResponse wrappers.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

Note: PR autogenerated by eavanvalkenburg's agent

Copilot and others added 2 commits April 7, 2026 12:49
…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>
Copilot AI review requested due to automatic review settings April 7, 2026 12:52
@eavanvalkenburg eavanvalkenburg self-assigned this Apr 7, 2026
@moonbox3 moonbox3 added the python label Apr 7, 2026
Copy link
Copy Markdown
Member Author

@eavanvalkenburg eavanvalkenburg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated Code Review

Reviewers: 4 | Confidence: 98% | Result: All clear

Reviewed: Correctness, Security Reliability, Test Coverage, Design Approach


Automated review by eavanvalkenburg's agents

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_value when text is empty.
  • Add regression tests covering empty-text behavior for both Pydantic and mapping schema formats, including ChatResponse and AgentResponse wrappers.

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.

@moonbox3
Copy link
Copy Markdown
Contributor

moonbox3 commented Apr 7, 2026

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/core/agent_framework
   _types.py10829091%58, 67–68, 122, 127, 146, 148, 152, 156, 158, 160, 162, 180, 184, 210, 232, 237, 242, 246, 276, 687–688, 847–848, 1233, 1305, 1340, 1360, 1370, 1422, 1554–1556, 1738, 1841–1846, 1871, 1959, 1967–1969, 1974, 2065, 2077, 2100, 2355, 2379, 2474, 2723, 2929, 2998, 3009, 3011–3015, 3017, 3020–3028, 3038, 3242–3244, 3247–3249, 3253, 3258, 3262, 3346–3348, 3377, 3454–3458
TOTAL27059318888% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
5377 20 💤 0 ❌ 0 🔥 1m 24s ⏱️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: response_format with Pydantic model crashes on background (Responses API) polling

4 participants