Repository navigation
Python: Preserve termination results in Agent and Chat middleware - #9681
Merged
Eduard van Valkenburg (eavanvalkenburg) merged 2 commits intoNov 2, 2026
Conversation
ssccinng
temporarily deployed
to
github-app-auth
October 25, 2026 09:38 — with
GitHub Actions
Inactive
ssccinng
temporarily deployed
to
github-app-auth
October 25, 2026 09:39 — with
GitHub Actions
Inactive
Closed
6 of 7 tasks
ssccinng
temporarily deployed
to
github-app-auth
November 1, 2026 10:43 — with
GitHub Actions
Inactive
ssccinng
temporarily deployed
to
github-app-auth
November 1, 2026 16:13 — with
GitHub Actions
Inactive
Contributor
There was a problem hiding this comment.
🟢 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.runregression 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
Eduard van Valkenburg (eavanvalkenburg)
approved these changes
Nov 2, 2026
5 of 7 tasks
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation & Context
Agent and Chat middleware discarded
MiddlewareTermination.resultwhencontext.resultwas unset, losing responses and causing chat middleware termination throughAgent.runto fail.Description & Review Guide
ResponseStreamdirectly. Type the restored termination payload for each pipeline so strict source checks retain the stream generics.Agent.runscenarios. 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
AI assistance details: Implementation, regression coverage, and review were AI-assisted.
Contribution Checklist