---
source: https://iinventedlight.com/library/notes/fastapi-streaming-responses.md
title: "Stream a FastAPI response with StreamingResponse and a generator"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT (FastAPI docs); BSD-3-Clause (Starlette docs); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: FastAPI docs, Custom Response - HTML, Stream, File, others; Starlette docs, Responses. MIT; BSD-3-Clause."
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: FastAPI StreamingResponse"
tags: [fastapi, starlette, streaming, streamingresponse, fileresponse, asgi]
client: Librarian
sources:
  - id: S1
    title: "Custom Response - HTML, Stream, File, others"
    publisher: FastAPI docs
    url: https://fastapi.tiangolo.com/advanced/custom-response/
    accessed: 2026-11-19
    license: "MIT"
  - id: S2
    title: "Responses"
    publisher: Starlette docs
    url: https://starlette.dev/responses/
    accessed: 2026-11-19
    license: "BSD-3-Clause"
---
# Stream a FastAPI response with StreamingResponse and a generator
**Answer:** `StreamingResponse` takes an async generator or a normal generator/iterator and streams the response body. `FileResponse` asynchronously streams a file as the response.

## StreamingResponse

Pass a function that uses `yield`, or any iterator, to `StreamingResponse`. Starlette also sets `media_type` on that response. File-like objects created by `open()` are normal iterators, so you can return them directly in a `StreamingResponse`.

Cancellation of an async task happens at an `await`. Without that point, the generator can not be cancelled properly and may keep running after cancellation is requested. The FastAPI example adds `await anyio.sleep(0)` so the event loop can handle cancellation, which matters more for large or infinite streams. A Stream Data style can handle cancellation behind the scenes.

## FileResponse

`FileResponse` takes a different set of arguments: `path` (the file path to the file to stream), `headers`, `media_type` (inferred from the filename or path if unset), and `filename` (included in `Content-Disposition` when set). File responses will include appropriate `Content-Length` and `Last-Modified` headers.

## See also

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

## Claims

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

- id: C2
  claim: A generator without await may keep running after cancellation.
  source: S1
  evidence: "can not be cancelled properly and may keep running even after cancellation is requested"

- id: C3
  claim: File-like objects from open() can be passed to StreamingResponse.
  source: S2
  evidence: "So, you can return them directly in a StreamingResponse"

## Sources

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

- [S2] Responses. Starlette docs. https://starlette.dev/responses/ . License: BSD-3-Clause. 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: FastAPI docs, Custom Response - HTML, Stream, File, others; Starlette docs, Responses. MIT; BSD-3-Clause. Text was condensed and changed from the originals.

---

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