๐Ÿ“ Add `await` in `StreamingResponse` code example to allow cancellation by casperdcl ยท Pull Request #14681 ยท fastapi/fastapi

@github-actions bot added the docs

Documentation about how to use FastAPI

label

Jan 10, 2026

akash1551 added a commit to akash1551/fastapi that referenced this pull request

Feb 6, 2026
Body:
[`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, 2026
Body:
[`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, 2026
Body:
[`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 alejsdev changed the title fix StreamingResponse example ๐Ÿ“ Fix StreamingResponse example

Feb 10, 2026

YuriiMotov

YuriiMotov

YuriiMotov

@casperdcl

@YuriiMotov YuriiMotov changed the title ๐Ÿ“ Fix StreamingResponse example ๐Ÿ“ Fix StreamingResponse code example

Feb 19, 2026

YuriiMotov

This was referenced

Feb 24, 2026

@tiangolo

@tiangolo

@tiangolo tiangolo changed the title ๐Ÿ“ Fix StreamingResponse code example ๐Ÿ“ Add await in StreamingResponse code example to allow cancellation

Feb 27, 2026

tiangolo