Python: Emit SSE keepalive comments during idle AG-UI event streams#6965
Closed
kartikmadan11 wants to merge 2 commits into
Closed
Python: Emit SSE keepalive comments during idle AG-UI event streams#6965kartikmadan11 wants to merge 2 commits into
kartikmadan11 wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves robustness of the Python AG-UI FastAPI SSE endpoint by emitting periodic SSE keepalive comments during idle gaps, preventing reverse proxies and EventSource clients from closing otherwise-healthy long-running streams (notably during “silent” reasoning/model phases). It adds a configurable keepalive_seconds parameter and validates the behavior with new endpoint tests.
Changes:
- Add
keepalive_seconds(default15) toadd_agent_framework_fastapi_endpointand emit: keepaliveSSE comments during idle gaps. - Refactor streaming to use a dedicated producer task feeding an
asyncio.Queue, so keepalive timeouts only affectqueue.get()rather than the protocol generator. - Add tests covering keepalive emission, disabled keepalive, and ensuring fast streams don’t interleave keepalives.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| python/packages/ag-ui/agent_framework_ag_ui/_endpoint.py | Adds configurable SSE keepalive support and introduces a producer/queue pattern to avoid canceling the underlying event generator on idle timeouts. |
| python/packages/ag-ui/tests/ag_ui/test_endpoint.py | Adds tests verifying keepalive behavior during idle gaps and ensuring disabling/fast-stream behavior works as intended. |
Reverse proxies and browser EventSource runtimes close connections that go silent for too long. Reasoning models can think for 30-60s without emitting events, triggering these timeouts. Adds a keepalive_seconds parameter (default 15) to add_agent_framework_fastapi_endpoint. The event iterator runs in a dedicated producer task feeding an asyncio.Queue so wait_for timeouts only cancel queue.get() rather than the generator itself, which would corrupt ContextVar state used by OTel hooks. Fixes microsoft#6941
reject negative keepalive_seconds early with ValueError bound event queue with maxsize=1 to preserve backpressure and avoid unbounded memory growth
e1963ba to
3cfae42
Compare
5 tasks
Contributor
|
Thanks for your contribution, @kartikmadan11. I had this inflight and it's now published as #6980. |
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.
Reverse proxies and browser EventSource runtimes close connections that go silent for too long. Reasoning models can think for 30-60s without emitting events, triggering these timeouts.
Adds a
keepalive_secondsparameter (default 15) toadd_agent_framework_fastapi_endpoint. The event iterator runs in a dedicated producer task feeding anasyncio.Queuesowait_fortimeouts only cancelqueue.get()rather than the generator itself, which would corrupt ContextVar state used by OTel hooks.Fixes #6941