Skip to content

Python: fix(core): warn when a streamed run registers result gates without buffering - #9029

Merged
Eduard van Valkenburg (eavanvalkenburg) merged 2 commits into
microsoft:mainfrom
he-yufeng:fix/stream-result-gate-buffer-warning
Oct 7, 2026
Merged

Eduard van Valkenburg (eavanvalkenburg) merged 2 commits into
microsoft:mainfrom
he-yufeng:fix/stream-result-gate-buffer-warning

Conversation

@he-yufeng

Copy link
Copy Markdown
Contributor

Motivation & Context

#8954 shows that an output guard written the natural way fails open on streamed runs: a result gate runs at stream finalization, so with stream_buffer_updates left at its default False every update has already reached the consumer by the time the gate raises. Switching a guarded endpoint from run() to run(stream=True) silently removes the protection, and the exception raised afterwards makes logs look like the answer was blocked.

Description & Review Guide

  • What are the major changes? Both middleware pipelines (agent path and chat path) now log a warning when result gates are registered on a streamed run with buffering off, and the with_result_gate() docstring notes that buffer_updates() is what makes gates block delivery. Strictly additive: delivery semantics are unchanged.
  • What is the impact of these changes? The silent footgun becomes audible. Guarded streams behave exactly as before; authors now get a log line telling them the gate cannot hold updates back unless they opt into buffering. Making gates imply buffering, or adding a per-gate opt-in, changes latency semantics for existing streams and looks like vNext material, so it is left for a maintainer call (directions laid out in the issue).
  • What do you want reviewers to focus on? Whether the warning fires at the right choke point (post-chain stream configuration, where the final buffering state is known) and the wording.

Related Issue

Fixes #8954

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 title prefix in sync automatically.

…thout buffering

A result gate runs at stream finalization, so with stream_buffer_updates
left at its default the consumer has already received every update by the
time the gate raises: the guard fails open, silently, and the exception
afterwards documents a block that never happened. microsoft#8954 has the full
analysis and an offline repro.

Log a warning from both middleware pipelines when result gates are
registered on a streamed run with buffering off, and note on
with_result_gate() that buffer_updates() is what makes gates block
delivery. Strictly additive: delivery semantics are unchanged. Flipping
buffering on when a gate is registered, or adding a per-gate opt-in,
changes latency semantics and is a vNext call for maintainers.

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

🟡 Changes recommended

Warning detection does not reflect gates and buffering configured directly on the final ResponseStream.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Adds warnings for unbuffered streamed result gates in Python core middleware.

Changes:

  • Warns for unbuffered context-level result gates.
  • Documents buffering requirements.
  • Adds agent and chat middleware tests.
File Description
python/​packages/​core/​agent_framework/​_middleware.py Adds gate warning logic to both pipelines.
python/​packages/​core/​agent_framework/​_types.py Clarifies with_result_gate() buffering behavior.
python/​packages/​core/​tests/​core/​test_middleware_with_agent.py Tests agent-path warnings.
python/​packages/​core/​tests/​core/​test_middleware_with_chat.py Tests chat-path warnings.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment thread python/packages/core/agent_framework/_middleware.py Outdated
…al stream

The warning compared the middleware context's gate lists and buffer
option before they were mounted, so a middleware that called
with_result_gate() or buffer_updates() on the final ResponseStream
itself was invisible to it: direct gates went unwarned, direct
buffering still warned. Read the mounted stream state instead and run
the check after the mount block in both pipelines.
Merged via the queue into microsoft:main with commit 45a92e6 Oct 7, 2026
45 checks passed

This branch was successfully deployed

1 active deployment
github-app-auth — f7b31549 Deployed Oct 4, 2026 by he-yufeng via add_label #24386
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]: an output guard on a streamed run releases the full answer before it can raise, unless stream_buffer_updates=True is set

3 participants