Repository navigation
Python: Reduce ResponseStream hot-path latency - #8971
Eduard van Valkenburg (eavanvalkenburg) merged 5 commits into
Conversation
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The optimized branches preserve existing behavior and are covered by the surrounding stream tests.
Review effort: Balanced
Findings: None
What changed in this PR
Optimizes Python ResponseStream hot paths while preserving stream processing semantics.
Changes:
- Bypasses empty gates/transforms and specializes single-hook/context paths.
- Avoids loading tool-approval code when middleware is absent.
| File | Description |
|---|---|
python/packages/core/agent_framework/_types.py |
Optimizes update pulling, transformation, gating, and finalization. |
python/packages/core/agent_framework/_agents.py |
Skips unnecessary tool-approval imports. |
💡 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)): 26fff6772461
Model: gpt-5.6-sol
Overview
The PR reduces ResponseStream overhead by bypassing empty gate/transform work, specializing single-transform and zero/one pull-context paths, and avoiding the tool-approval import when middleware is absent. Existing tests and lifecycle guards preserve ordinary sync/async transforms, gate ordering, cleanup, buffering, finalization, and pull-context unwinding. The single-transform specialization does, however, change the established live-list behavior when another transform is registered while the sole async transform is suspended, allowing one update to bypass that newly registered transform.
Reviewed the supplied pull-request change set across correctness, security/reliability, architecture, and failure behavior.
1 verified finding remained after source verification (1 medium) across 1 file. Details are attached to the affected lines below.
Affected areas: python/packages/core/agent_framework/_types.py
Jose Alvarez (jpalvarezl)
left a comment
There was a problem hiding this comment.
The correctness regression from the initial review is fixed with focused coverage. The remaining inline notes are non-blocking nits.
Motivation & Context
Recent
ResponseStreamgates and buffering support added measurable per-update overhead even when a stream has no configured gates, transforms, or pull contexts. The cost compounds across the nested streams used by chat clients and agents. No-middleware agent runs also paid a one-time lazy import cost for tool-approval machinery.Same-host deterministic benchmarks showed a 100-update agent-like stream increasing from 0.255 ms to 0.674 ms, chat streaming from 0.176 ms to 0.256 ms, and agent streaming from 0.269 ms to 0.417 ms. This contributes to the latency investigation and optimization goals tracked in #3686.
Description & Review Guide
ResponseStream.ToolApprovalMiddlewarewhen an agent has no middleware.ResponseStreambenchmark improves from 0.674 ms to 0.182 ms.ExitStackpath.Related Issue
Related to #3686
Contribution Checklist
breaking changelabel (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and the title prefix in sync automatically.