This repository was archived by the owner on Mar 29, 2026. It is now read-only.
fix: add asyncio.sleep(0) to SSE generator for real-time streaming#1041
Open
lorry01 wants to merge 1 commit into
Open
fix: add asyncio.sleep(0) to SSE generator for real-time streaming#1041lorry01 wants to merge 1 commit into
lorry01 wants to merge 1 commit into
Conversation
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Problem
The SSE endpoint
/api/vanna/v2/chat_ssebuffers responses instead of delivering them in real-time. When testing with browser DevTools, all chunks arrive in 2-3 batches rather than streaming continuously.Expected behavior: Chunks should arrive one-by-one as they are generated.
Actual behavior: Chunks are batched together and delivered in 2-3 large groups, losing the real-time streaming effect.
Root Cause
In
src/vanna/servers/fastapi/routes.py, thegenerate()async generator yields data but doesn't give the event loop a chance to process network I/O between yields. This causes data to accumulate in the buffer until the generator pauses (waiting for LLM response) or completes.Solution
Add
await asyncio.sleep(0)after eachyieldstatement. This yields control back to the event loop, allowing it to process pending network I/O and send data to the client immediately.async def generate() -> AsyncGenerator[str, None]:
"""Generate SSE stream."""
try:
async for chunk in chat_handler.handle_stream(chat_request):
chunk_json = chunk.model_dump_json()
yield f"data: {chunk_json}\n\n"
await asyncio.sleep(0) # Yield control to event loop for immediate transmission
yield "data: [DONE]\n\n"
except Exception as e:
# ...## Why
await asyncio.sleep(0)worksIn Python's asyncio,
await asyncio.sleep(0)is a common idiom to:Without this, the generator runs synchronously between
yieldstatements, and the event loop doesn't get a chance to flush the network buffer until the generator blocks on an actual I/O operation.Testing
Environment:
Before fix:
After fix:
Related
This is a common issue with FastAPI
StreamingResponse. Similar solutions are documented in: