Python: Add gates and buffering to ResponseStream - #8829
Eduard van Valkenburg (eavanvalkenburg) merged 3 commits into
Conversation
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Buffered cancellation and unconditional re-derivation can lose updates or metadata, and the updated override sample currently fails due to transform ordering.
Review effort: Balanced
Findings: 3
Open (3)
What changed in this PR
Adds composable gates, transforms, buffering, and middleware configuration to Python ResponseStream.
Changes:
- Adds ordered update/result pipelines and buffered release.
- Migrates agent-hooks and middleware to declarative stream configuration.
- Expands tests and samples for the new APIs.
| File | Description |
|---|---|
python/samples/02-agents/response_stream.py |
Demonstrates gates, transforms, and buffering. |
python/samples/02-agents/middleware/usage_tracking_middleware.py |
Uses renamed transform collections. |
python/samples/02-agents/middleware/README.md |
Updates sample descriptions. |
python/samples/02-agents/middleware/override_result_with_middleware.py |
Migrates result overriding to buffered transforms. |
python/packages/core/tests/core/test_types.py |
Tests gates and buffered streams. |
python/packages/core/tests/core/test_agent_hooks.py |
Updates agent-hooks compatibility tests. |
python/packages/core/agent_framework/_types.py |
Implements the new ResponseStream pipeline. |
python/packages/core/agent_framework/_middleware.py |
Adds middleware stream configuration fields. |
python/packages/core/agent_framework/_agents.py |
Updates stream-gating documentation. |
python/packages/core/agent_framework/_agent_hooks.py |
Migrates enforcement to buffered stream configuration. |
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
MAF Automated Review — Iteration 1
Result: Findings reported
Scope: full PR (1 commit(s)): 7aa97237eaac
Model: gpt-5.6-sol-fast
Overview
The new ResponseStream pipeline has strong ordering, atomic buffering, configuration-sealing, and cleanup guardrails, with extensive tests for normal gate and transform behavior. However, agent-hooks policy is now composed as ordinary mutable stream configuration, allowing later transforms or a replaced re-derivation callback to emit content after approval. The change also introduces observable compatibility regressions in pass-through stream fidelity and legacy hook assignment, plus duplicate shutdown records on failed streaming output evaluation.
Reviewed the supplied pull-request change set across correctness, security/reliability, architecture, and failure behavior.
5 verified findings remained after source verification (2 high, 3 medium) across 3 files. Details are attached to the affected lines below.
Affected areas: python/packages/core/agent_framework/_agent_hooks.py, python/packages/core/agent_framework/_middleware.py, python/packages/core/agent_framework/_types.py

Motivation & Context
ResponseStream.buffered_and_gated()introduced a specialized stream subtype and combined blocking, transformation, buffering, and replay policy in one operation. That made egress controls difficult to compose and forced middleware such as agent-hooks to own stream materialization instead of declaring the processing stages it needs.This change moves those capabilities into
ResponseStreamitself so updates and final results can use the same explicit gate/transform pipeline, with buffering selected independently when fail-closed release or final-result replacement is required.Description & Review Guide
_GatedResponseStream, and deprecateResponseStream.buffered_and_gated()as a translation onto the new structures.call_next(); the pipeline applies it to the eventual stream. Agent-hooks now contributes its enforcement pieces through this mechanism without replacing or wrapping the returned stream.ResponseStreamand agent-hooks coverage, and update the ResponseStream and streaming middleware samples to demonstrate the new developer experience.ResponseStreamconstruction and hook APIs continue to work. Buffered streams can block before any egress, expose only released updates, and re-derive updates from an authoritative final result.buffered_and_gated()remains available with a deprecation warning, while its private subtype is removed.buffered_and_gated()and agent-hooks.Related Issue
N/A — intentionally opened without a linked issue.
Contribution Checklist
breaking changelabel (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.