Skip to content

fix(python): stop aggregate_chunked_results mutating caller arguments - #14850

Open
DZH166 wants to merge 1 commit into
microsoft:mainfrom
DZH166:fix/stop-aggregate-chunked-results-mutating
Open

DZH166 wants to merge 1 commit into
microsoft:mainfrom
DZH166:fix/stop-aggregate-chunked-results-mutating

Conversation

@DZH166

@DZH166 DZH166 commented Nov 21, 2026

Copy link
Copy Markdown

Problem

aggregate_chunked_results writes every chunk into the borrowed arguments as input and never
restores it, so the caller gets its arguments back mutated:

  • an existing input is replaced by the last chunk, so the original value is lost;
  • an argument-less call gains a spurious input key that was never there.
arguments = KernelArguments(input="the whole original transcript")
await aggregate_chunked_results(func, ["chunk one", "chunk two"], kernel, arguments)
arguments["input"]   # "chunk two"   <- expected "the whole original transcript"

ConversationSummaryPlugin.summarize_conversation passes its own KernelArguments straight into this
helper and returns it (documented as "KernelArguments with the summarized conversation result in key
self.return_key"), so in practice the summarization plugin silently replaces the caller's $input
with the last chunk of the transcript while the caller only asked for arguments["summary"].

Fix

Record whether input was present, and restore the previous value (or drop the key) in a finally
block, so the caller's arguments are exactly as passed in even when a chunk invocation raises.
Only the aggregated text is returned.

Tests

New test test_aggregate_results_does_not_mutate_caller_arguments in
python/tests/unit/text/test_function_extension.py asserts both directions: an existing input
is restored and a bare KernelArguments() stays empty. The existing test_aggregate_results still
covers the aggregation output itself.

Measured counts (Python 3.13, offline):

  • pytest tests/unit/text/test_function_extension.py -> 2 passed
  • pytest tests/unit/text tests/unit/core_plugins tests/unit/prompt_template -> 736 passed, 3 skipped

@DZH166
DZH166 requested a review from a team as a code owner November 21, 2026 13:50
Copilot AI balanced review requested due to automatic review settings November 21, 2026 13:50
@DZH166
DZH166 deployed to github-app-auth November 21, 2026 13:50 — with GitHub Actions Active

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.

🟡 Changes recommended

The tests do not cover a later chunk invocation raising, leaving exception propagation and restoration or removal of input unverified.

1 open issue
What changed in this PR

This PR prevents chunk aggregation from leaving the caller’s input argument overwritten, preserving arguments used by callers such as ConversationSummaryPlugin.

Changes:

  • Restore the original input, or remove the temporary key, in a finally block.
  • Document the restoration behavior and add regression tests for successful calls with and without an existing input.
File Description
python/​tests/​unit/​text/​test_function_extension.py Tests restoration of existing input and removal of temporary input.
python/​semantic_kernel/​text/​function_extension.py Preserves the caller’s input after aggregation, including when an invocation raises.

Review effort: Balanced


results.append(str(result))
results.append(str(result))
finally:

This branch was successfully deployed

1 active deployment
github-app-auth — e7b5f8ec Deployed Nov 21, 2026 by DZH166 via add_label #29782
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants