๐ Add `await` in `StreamingResponse` code example to allow cancellation by casperdcl ยท Pull Request #14681 ยท fastapi/fastapi
akash1551 added a commit to akash1551/fastapi that referenced this pull request
Feb 6, 2026Body: [`StreamingResponse` docs](https://fastapi.tiangolo.com/advanced/custom-response/?h=responses#streamingresponse) state: > Takes an async generator or a normal generator/iterator However based on Kludex/starlette#1776 (comment) `async` generetors need something `await`ed to work: ```py import asyncio from time import sleep from fastapi import FastAPI from fastapi.responses import StreamingResponse app = FastAPI() async def fake_video_streamer(): for i in range(10): sleep(0.1) yield b"some fake video bytes" # await asyncio.sleep(0) # <-- uncomment to fix example @app.get("/") async def main(): """ this is as per https://fastapi.tiangolo.com/advanced/custom-response/?h=responses#streamingresponse but does not to work (blocks for 1 sec, then returns all chunks at once): curl -sNo- localhost:8000 wget -qO- localhost:8000 """ return StreamingResponse(fake_video_streamer()) ``` - `uvicorn==0.40.0` - `fastapi==0.128.0` --- Comments --- User: casperdcl It seems based on Kludex/starlette#1776 (comment) that `await asyncio.sleep(0)` needs to be added to the documented example to work. User: mirza-mohibul-hasan Hi @casperdcl, thanks for raising this. I understand the issue and the root cause with async generators blocking without an await. If the fix isnโt already finalized, Iโd be happy to work on this or help refine the docs/validate the example. Please let me know if that works for you. User: casperdcl fixed in fastapi#14681
akash1551 added a commit to akash1551/fastapi that referenced this pull request
Feb 6, 2026Body: [`StreamingResponse` docs](https://fastapi.tiangolo.com/advanced/custom-response/?h=responses#streamingresponse) state: > Takes an async generator or a normal generator/iterator However based on Kludex/starlette#1776 (comment) `async` generetors need something `await`ed to work: ```py import asyncio from time import sleep from fastapi import FastAPI from fastapi.responses import StreamingResponse app = FastAPI() async def fake_video_streamer(): for i in range(10): sleep(0.1) yield b"some fake video bytes" # await asyncio.sleep(0) # <-- uncomment to fix example @app.get("/") async def main(): """ this is as per https://fastapi.tiangolo.com/advanced/custom-response/?h=responses#streamingresponse but does not to work (blocks for 1 sec, then returns all chunks at once): curl -sNo- localhost:8000 wget -qO- localhost:8000 """ return StreamingResponse(fake_video_streamer()) ``` - `uvicorn==0.40.0` - `fastapi==0.128.0` --- Comments --- User: casperdcl It seems based on Kludex/starlette#1776 (comment) that `await asyncio.sleep(0)` needs to be added to the documented example to work. User: mirza-mohibul-hasan Hi @casperdcl, thanks for raising this. I understand the issue and the root cause with async generators blocking without an await. If the fix isnโt already finalized, Iโd be happy to work on this or help refine the docs/validate the example. Please let me know if that works for you. User: casperdcl fixed in fastapi#14681
akash1551 added a commit to akash1551/fastapi that referenced this pull request
Feb 6, 2026Body: [`StreamingResponse` docs](https://fastapi.tiangolo.com/advanced/custom-response/?h=responses#streamingresponse) state: > Takes an async generator or a normal generator/iterator However based on Kludex/starlette#1776 (comment) `async` generetors need something `await`ed to work: ```py import asyncio from time import sleep from fastapi import FastAPI from fastapi.responses import StreamingResponse app = FastAPI() async def fake_video_streamer(): for i in range(10): sleep(0.1) yield b"some fake video bytes" # await asyncio.sleep(0) # <-- uncomment to fix example @app.get("/") async def main(): """ this is as per https://fastapi.tiangolo.com/advanced/custom-response/?h=responses#streamingresponse but does not to work (blocks for 1 sec, then returns all chunks at once): curl -sNo- localhost:8000 wget -qO- localhost:8000 """ return StreamingResponse(fake_video_streamer()) ``` - `uvicorn==0.40.0` - `fastapi==0.128.0` --- Comments --- User: casperdcl It seems based on Kludex/starlette#1776 (comment) that `await asyncio.sleep(0)` needs to be added to the documented example to work. User: mirza-mohibul-hasan Hi @casperdcl, thanks for raising this. I understand the issue and the root cause with async generators blocking without an await. If the fix isnโt already finalized, Iโd be happy to work on this or help refine the docs/validate the example. Please let me know if that works for you. User: casperdcl fixed in fastapi#14681
alejsdev
changed the title
fix StreamingResponse example
๐ Fix StreamingResponse example
YuriiMotov
changed the title
๐ Fix StreamingResponse example
๐ Fix StreamingResponse code example
This was referenced
Feb 24, 2026
tiangolo
changed the title
๐ Fix
๐ Add StreamingResponse code exampleawait in StreamingResponse code example to allow cancellation
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