Skip to content

Python: Reduce ResponseStream hot-path latency - #8971

Merged
Eduard van Valkenburg (eavanvalkenburg) merged 5 commits into
microsoft:mainfrom
eavanvalkenburg:investigate-framework-latency
Oct 2, 2026
Merged

Eduard van Valkenburg (eavanvalkenburg) merged 5 commits into
microsoft:mainfrom
eavanvalkenburg:investigate-framework-latency

Conversation

@eavanvalkenburg

Copy link
Copy Markdown
Member

Motivation & Context

Recent ResponseStream gates 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

  • What are the major changes?
    • Skip empty update and result gate/transform processing in ResponseStream.
    • Specialize the common single-transform and zero/one pull-context paths while preserving the existing multi-hook and multi-context behavior.
    • Avoid importing ToolApprovalMiddleware when an agent has no middleware.
  • What is the impact of these changes?
    • The 100-update agent-like ResponseStream benchmark improves from 0.674 ms to 0.182 ms.
    • Chat streaming improves from 0.256 ms to 0.200 ms, and agent streaming improves from 0.417 ms to 0.360 ms.
    • Existing late-hook registration, cleanup, mapping, finalization, buffered/gated fail-closed behavior, and public APIs are unchanged.
  • What do you want reviewers to focus on?
    • The equivalence of the zero/one-context fast paths with the existing ExitStack path.
    • Preservation of late transform hooks on ordinary streams and result finalization semantics.

Related Issue

Related to #3686

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 is linked to an issue and there is no other open PR for this issue (see Related Issue above).
  • This is not a breaking change. If it is a breaking change, add the breaking change label (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.

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.

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.

@github-actions github-actions Bot 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.

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

Comment thread python/packages/core/agent_framework/_types.py Outdated
@agent-framework-automation agent-framework-automation Bot added the documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs label Oct 2, 2026

@jpalvarezl Jose Alvarez (jpalvarezl) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The correctness regression from the initial review is fixed with focused coverage. The remaining inline notes are non-blocking nits.

Comment thread python/packages/core/agent_framework/_types.py
Merged via the queue into microsoft:main with commit ffd85a2 Oct 2, 2026
46 checks passed
@eavanvalkenburg
Eduard van Valkenburg (eavanvalkenburg) deleted the investigate-framework-latency branch October 2, 2026 12:00

This branch was previously deployed

1 inactive deployment
github-app-auth — 32717f06 Deployed Oct 2, 2026 by eavanvalkenburg via add_label #24283
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants