Skip to content

Commit 7fa4cee

Browse files
committed
fix: Add explicit [DONE] signal to SSE stream endpoint
Fixes issue where /run_sse endpoint sometimes fails to close the SSE connection after the model's final response, causing client-side hangs. Changes: - Add 'data: [DONE]' termination signal after all events streamed - Send [DONE] in error path as well for consistency - Update test to filter out [DONE] signal
1 parent ab89d12 commit 7fa4cee

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

src/google/adk/cli/adk_web_server.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,6 +1557,9 @@ async def event_generator():
15571557
"Generated event in agent run streaming: %s", sse_event
15581558
)
15591559
yield f"data: {sse_event}\n\n"
1560+
1561+
# Send termination signal after all events have been streamed
1562+
yield "data: [DONE]\n\n"
15601563
except Exception as e:
15611564
logger.exception("Error in event_generator: %s", e)
15621565
# Yield a proper Event object for the error
@@ -1570,6 +1573,7 @@ async def event_generator():
15701573
"data:"
15711574
f" {error_event.model_dump_json(by_alias=True, exclude_none=True)}\n\n"
15721575
)
1576+
yield "data: [DONE]\n\n"
15731577

15741578
# Returns a streaming response with the proper media type for SSE
15751579
return StreamingResponse(

tests/unittests/cli/test_fast_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,7 @@ async def run_async_with_artifact_delta(
10051005
sse_events = [
10061006
json.loads(line.removeprefix("data: "))
10071007
for line in response.text.splitlines()
1008-
if line.startswith("data: ")
1008+
if line.startswith("data: ") and line != "data: [DONE]"
10091009
]
10101010

10111011
assert len(sse_events) == 2

0 commit comments

Comments
 (0)