---
source: https://iinventedlight.com/library/notes/starlette-streaming-responses.md
title: "Stream a response body from Starlette or FastAPI"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "BSD-3-Clause (Starlette); MIT (FastAPI); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Starlette, Responses; FastAPI, Custom Response - HTML, Stream, File, others. BSD-3-Clause; MIT."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-19
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-19
as_of: 2026-11-19
confidence: medium
untested: "Not reproduced (2026-11-19). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: Starlette and FastAPI StreamingResponse"
tags: [starlette, fastapi, streaming, http, asgi]
client: Librarian
sources:
  - id: S1
    title: "Responses"
    publisher: Starlette
    url: https://starlette.dev/responses/
    accessed: 2026-11-19
    license: "BSD-3-Clause"
  - id: S2
    title: "Custom Response - HTML, Stream, File, others"
    publisher: FastAPI
    url: https://fastapi.tiangolo.com/advanced/custom-response/
    accessed: 2026-11-19
    license: "MIT"
---
# Stream a response body from Starlette or FastAPI
**Answer:** `StreamingResponse` takes an async generator or a normal generator or iterator and streams the response body. Starlette and FastAPI both ship that class.

## Starlette

Pass the generator and a `media_type` such as `text/html`. The `slow_numbers` example yields HTML incrementally and uses `asyncio.sleep`. File-like objects (like those created by `open()`) are normal iterators. Those objects can be returned directly in a `StreamingResponse`. `FileResponse` streams a file asynchronously. It also supports HTTP range requests.

## FastAPI

The FastAPI example is a function with `yield`. An async task can only be cancelled when it reaches an `await`. Without that, the generator may keep running after cancellation is requested. The example adds `await anyio.sleep(0)` so the event loop has a chance to halt the generator.

| StreamingResponse | FileResponse |
|---|---|
| StreamingResponse | Streams a generator or iterator as the body |
| media_type | Argument such as text/html |
| FileResponse | Asynchronously streams a file as the response |
| await | Needed so an async generator can be cancelled |

## See also

- [fastapi-response-model](https://iinventedlight.com/library/notes/fastapi-response-model/)
- [fastapi-async-await](https://iinventedlight.com/library/notes/fastapi-async-await/)
- [django-request-response](https://iinventedlight.com/library/notes/django-request-response/)

## Claims

- id: C1
  claim: Starlette's StreamingResponse streams the body from an async generator or iterator.
  source: S1
  evidence: "Takes an async generator or a normal generator/iterator and streams the response body"

- id: C2
  claim: File-like objects created by open() can be returned directly in a StreamingResponse.
  source: S1
  evidence: "you can return them directly in a StreamingResponse"

- id: C3
  claim: FastAPI notes that an async task can only be cancelled when it reaches an await.
  source: S2
  evidence: "An async task can only be cancelled when it reaches an await"

## Sources

- [S1] Responses. Starlette. https://starlette.dev/responses/ . License: BSD-3-Clause. Accessed 2026-11-19.

- [S2] Custom Response - HTML, Stream, File, others. FastAPI. https://fastapi.tiangolo.com/advanced/custom-response/ . License: MIT. Accessed 2026-11-19.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Starlette, Responses; FastAPI, Custom Response - HTML, Stream, File, others. BSD-3-Clause; MIT. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
