Skip to content

Python: parse structured response value from final message - #6383

Merged
Evan Mattson (moonbox3) merged 1 commit into
microsoft:mainfrom
liuzemei:fix-agent-response-value-final-message-6366
Jul 9, 2026
Merged

Python: parse structured response value from final message#6383
Evan Mattson (moonbox3) merged 1 commit into
microsoft:mainfrom
liuzemei:fix-agent-response-value-final-message-6366

Conversation

@liuzemei

Copy link
Copy Markdown
Contributor

Motivation and Context

Fixes #6366.

When an agent response contains intermediate messages and a final structured JSON payload, AgentResponse.value currently parses self.text, which concatenates all message text. That can join intermediate tool/context JSON with the final response JSON and cause ValidationError / JSONDecodeError from trailing characters.

Description

  • Parse structured ChatResponse.value and AgentResponse.value from the last non-empty message text instead of the public concatenated .text value.
  • Keep .text behavior unchanged for callers that want the full concatenated response text.
  • Add regression coverage for Pydantic response formats and JSON schema mapping response formats.

Testing

uv run --group dev pytest packages/core/tests/core/test_types.py -q -k 'final_message or empty_text or invalid_schema or chat_response_with_format or mapping_response_format'\nuv run pytest packages/core/tests/core/test_types.py -q\nuv run pytest packages/core/tests/core/test_middleware.py -q\nuv run pytest packages/core/tests/workflow/test_workflow.py -q -k 'AgentResponse or response or output'\nuv run ruff format --check packages/core/agent_framework/_types.py packages/core/tests/core/test_types.py\nuv run ruff check packages/core/agent_framework/_types.py packages/core/tests/core/test_types.py\ngit diff --check\n```\n\n### Contribution Checklist\n\n- [x] The code builds clean without any errors or warnings\n- [x] The PR follows the Contribution Guidelines\n- [x] All unit tests pass, and I have added new tests where possible\n- [ ] **Is this a breaking change?** If yes, add "[BREAKING]" prefix to the title of the PR.

Copilot AI review requested due to automatic review settings June 8, 2026 05:43
@moonbox3 Evan Mattson (moonbox3) added the python Usage: [Issues, PRs], Target: Python label Jun 8, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR updates structured-output parsing for ChatResponse.value and AgentResponse.value to parse only the final non-empty message (ignoring earlier messages), and adds regression tests for multi-message responses.

Changes:

  • Add _last_non_empty_message_text() helper to select the final non-empty message text.
  • Update ChatResponse.value / AgentResponse.value to parse structured output from the final message only.
  • Add tests asserting intermediate messages are ignored when parsing structured output.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
python/packages/core/agent_framework/_types.py Parse structured output from the last non-empty message instead of concatenated response text.
python/packages/core/tests/core/test_types.py Add tests validating structured parsing ignores intermediate messages for both response types.

Comment thread python/packages/core/agent_framework/_types.py Outdated
Comment thread python/packages/core/agent_framework/_types.py
Comment thread python/packages/core/tests/core/test_types.py
@liuzemei

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

@github-actions

github-actions Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/core/agent_framework
   _types.py12269792%61, 70–71, 125, 130, 149, 151, 155, 159, 161, 163, 165, 183, 187, 213, 235, 240, 245, 249, 279, 705–706, 887–888, 1346, 1421, 1456, 1476, 1486, 1538, 1672–1674, 1959–1964, 1989, 2044, 2049, 2059, 2067, 2074–2078, 2096, 2169, 2182, 2187, 2300, 2323, 2584, 2608, 2707, 2888–2889, 2991, 3220, 3273, 3292, 3331, 3342, 3344–3348, 3350, 3353–3361, 3371, 3460, 3597, 3602, 3607, 3612, 3616, 3702–3704, 3733, 3821–3825
TOTAL44044527088% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
8801 33 💤 0 ❌ 0 🔥 2m 17s ⏱️

Comment thread python/packages/core/tests/core/test_types.py
@eavanvalkenburg

Copy link
Copy Markdown
Member

and some tests are failing, please have a look at those as well Sheldon (@liuzemei)

Signed-off-by: liuzemei <35027683+liuzemei@users.noreply.github.com>
@moonbox3
Evan Mattson (moonbox3) force-pushed the fix-agent-response-value-final-message-6366 branch from cdbb45b to bb81170 Compare July 9, 2026 08:21
@moonbox3
Evan Mattson (moonbox3) added this pull request to the merge queue Jul 9, 2026
Merged via the queue into microsoft:main with commit 9f4526a Jul 9, 2026
37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: AgentResponse.value throws pydantic ValidationError parse errors in multi-message scenarios when using with_request_info()

4 participants