Skip to content

Python: Fix _add_text_reasoning_content to preserve id during coalescing#4862

Open
giles17 wants to merge 4 commits intomicrosoft:mainfrom
giles17:agent/fix-4852-1
Open

Python: Fix _add_text_reasoning_content to preserve id during coalescing#4862
giles17 wants to merge 4 commits intomicrosoft:mainfrom
giles17:agent/fix-4852-1

Conversation

@giles17
Copy link
Contributor

@giles17 giles17 commented Mar 23, 2026

Motivation and Context

When text_reasoning Content chunks are coalesced (e.g., during streaming), the id field was silently dropped. This broke encrypted reasoning round-trips, since the id (like rs_abc123) is required to associate the coalesced content back with its encrypted payload.

Fixes #4852

Description

The root cause was that _add_text_reasoning_content in Content constructed the merged result without passing through the id field. The fix adds id=self.id or other.id to the coalesced Content constructor call, preserving the identifier from whichever operand carries it. Tests cover the standard case, the fallback when only one operand has an id, and the combination of id with encrypted_content additional properties.

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 giles17's agent

Copilot and others added 2 commits March 23, 2026 17:18
…oft#4852)

Preserve the id field (rs_* identifier) when coalescing text_reasoning
Content objects by passing id=self.id or other.id to the Content
constructor. This fixes the encrypted reasoning round-trip where the
missing id prevented _prepare_content_for_openai from including it in
the serialized reasoning item.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 23, 2026 17:21
@giles17 giles17 self-assigned this Mar 23, 2026
Copy link
Contributor Author

@giles17 giles17 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: 96%

✓ Correctness

This diff removes one blank line between two test functions (going from two blank lines to one), which is a trivial whitespace change with no correctness impact. The test logic itself is unchanged and correct.

✓ Security Reliability

This diff removes a single blank line between two test functions (line 1528). There are no code logic changes, no security implications, and no reliability concerns. The underlying fix (preserving id during text_reasoning coalescing at line 1393 of _types.py) was already landed in a prior commit and is not part of this diff. The new tests adequately cover the id-preservation behavior.

✓ Test Coverage

The diff only removes a single blank line (cosmetic whitespace change). The actual substantive tests — test_text_reasoning_content_add_preserves_id, test_text_reasoning_content_add_id_fallback_to_other, and test_text_reasoning_content_add_preserves_id_with_encrypted_content — already exist in the file and properly cover the bug fix (id=self.id or other.id in _add_text_reasoning_content). The tests cover: (1) id preserved when both chunks carry it, (2) fallback to other's id when self has none, and (3) round-trip with encrypted_content. Assertions are meaningful (checking specific field values, not just no-exception). No test coverage gaps found.

✓ Design Approach

The diff is a trivial whitespace change — removal of a single blank line between two test functions (test_text_reasoning_content_iadd_coverage and test_text_reasoning_content_add_preserves_id). The surrounding test code itself is not changed by this diff. The new tests (test_text_reasoning_content_add_preserves_id, test_text_reasoning_content_add_id_fallback_to_other, test_text_reasoning_content_add_preserves_id_with_encrypted_content) look correct and well-targeted for the fix described in the chat context. No design-level concerns arise from removing a blank line.

Suggestions

  • Consider adding a test where neither self nor other has an id to verify the result's id is None — though this is a minor edge case and not blocking.

Automated review by giles17's agents

@markwallace-microsoft
Copy link
Member

markwallace-microsoft commented Mar 23, 2026

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/core/agent_framework
   _types.py10538691%58, 67–68, 122, 127, 146, 148, 152, 156, 158, 160, 162, 180, 184, 210, 232, 237, 242, 246, 276, 686–687, 846–847, 1232, 1304, 1339, 1359, 1369, 1421, 1553–1555, 1745, 1836–1841, 1866, 2034, 2046, 2297, 2321, 2416, 2647, 2853, 2922, 2933, 2935–2939, 2941, 2944–2952, 2962, 3167–3169, 3172–3174, 3178, 3183, 3187, 3271–3273, 3302, 3356, 3379–3383, 3389
TOTAL27298321888% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
5352 20 💤 0 ❌ 0 🔥 1m 27s ⏱️

Copy link
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 Content coalescing for text_reasoning chunks so the id (e.g., rs_*) is preserved, enabling encrypted reasoning round-trips to work correctly after streaming coalesces multiple chunks.

Changes:

  • Preserve Content.id when merging (__add__) text_reasoning content.
  • Add unit tests covering id preservation, id fallback, and interaction with encrypted_content additional properties.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
python/packages/core/agent_framework/_types.py Updates text_reasoning merge logic to carry forward id when coalescing chunks.
python/packages/core/tests/core/test_types.py Adds tests validating id preservation/fallback and encrypted reasoning round-trip expectations.

Copilot and others added 2 commits March 23, 2026 17:27
…ft#4852)

Detect when both operands have different non-empty ids during
text_reasoning Content coalescing and raise AdditionItemMismatch
instead of silently keeping one. This prevents mis-associating
encrypted_content during round-trips.

Also adds tests for conflicting ids and the neither-has-id edge case.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…dd_text_reasoning_content drops id during coalescing, breaking encrypted reasoning round-trip
Copy link
Contributor Author

@giles17 giles17 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: 96% | Result: All clear

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


Automated review by giles17's agents

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]: Content._add_text_reasoning_content drops id during coalescing, breaking encrypted reasoning round-trip

3 participants