Skip to content

Python: Preserve termination results in Agent and Chat middleware - #9681

Merged
Eduard van Valkenburg (eavanvalkenburg) merged 2 commits into
microsoft:mainfrom
ssccinng:openfix/067a12af-712e-47fe-95df-38ee7945a243
Nov 2, 2026
Merged

Eduard van Valkenburg (eavanvalkenburg) merged 2 commits into
microsoft:mainfrom
ssccinng:openfix/067a12af-712e-47fe-95df-38ee7945a243

Conversation

@ssccinng

@ssccinng ssccinng commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Motivation & Context

Agent and Chat middleware discarded MiddlewareTermination.result when context.result was unset, losing responses and causing chat middleware termination through Agent.run to fail.

Description & Review Guide

  • Major changes: Preserve the original termination-result fallback, integrate current main, remove the unused import, and correct streaming test handlers to return ResponseStream directly. Type the restored termination payload for each pipeline so strict source checks retain the stream generics.
  • Impact: Existing context results retain precedence. Terminated execution skips the underlying handler or client, and returned streams retain update transforms and final-result hooks.
  • Review focus: Retain all eight agent/chat, streaming/non-streaming, and pipeline/Agent.run scenarios. Streaming assertions accommodate metadata-only events while checking content, response types, and finalization from original updates.

Validation covers package checks, all five configured test checkers, applicable sample checks, and the four focused middleware modules on Python 3.11 and the default interpreter.

Validation: The affected checks fail on the previously published commit and pass after the update. The listed validation commands passed. The same configured checks also passed with Python 3.11.

Related Issue

Fixes #9471.

AI Assistance

  • No material AI assistance was used.
  • This is an AI-assisted contribution. I reviewed, understood, and verified all submitted content and accept responsibility for it.

AI assistance details: Implementation, regression coverage, and review were AI-assisted.

Contribution Checklist

  • The code builds clean without any errors or warnings.
  • All unit tests pass, and I have added new tests where possible.
  • The PR follows the Contribution Guidelines.
  • This PR links to an agreed issue with no competing open PR, or documents an applicable exception.
  • This is not a breaking change.

Copilot AI balanced review requested due to automatic review settings November 1, 2026 16:13

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.

🟢 Approval recommended

No active review issues remain for the changes preserving middleware termination results.

No open issues 🎉

What changed in this PR

Fixes #9471 by preserving MiddlewareTermination.result in Python agent and chat middleware when context.result is unset, preventing lost responses and chat termination failures through Agent.run.

Changes:

  • Adds typed termination-result fallbacks while preserving existing context-result precedence.
  • Keeps returned streams connected to update transforms and final-result hooks.
  • Adds pipeline and Agent.run regression tests for streaming and non-streaming termination.
File Description
python/​packages/​core/​tests/​core/​test_middleware_with_chat.py Tests chat termination responses and streams through Agent.run, without client calls.
python/​packages/​core/​tests/​core/​test_middleware_with_agent.py Tests agent termination responses and streams through Agent.run, without client calls.
python/​packages/​core/​tests/​core/​test_middleware_context_result.py Tests pipeline fallbacks, context-result precedence, and stream hooks.
python/​packages/​core/​agent_framework/​_middleware.py Restores termination payloads in both pipelines and removes the unused import.

Review effort: Balanced

Merged via the queue into microsoft:main with commit d2fb432 Nov 2, 2026
50 checks passed

This branch was previously deployed

1 inactive deployment
github-app-auth — 435f7160 Deployed Nov 1, 2026 by ssccinng via add_label #26808
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]: MiddlewareTermination(result=...) is dropped by agent and chat middleware; Agent.run crashes for chat middleware

3 participants