FastAPI's StreamingResponse: Send Data in Chunks
StreamingResponse sends data piece by piece, like a live broadcast, instead of sending a complete file all at once. This keeps your server's memory low for huge responses like file downloads, video streams, or live data from AI models.
Why it exists
Standard HTTP responses require the server to know the full content and its size before sending anything. For very large files or dynamically generated content, this means holding the entire response in memory, which is slow, resource-intensive, and can lead to server crashes.
The mental model
Think of it like a garden hose versus a bucket. A normal response is like filling a huge bucket (the server's memory) with data and then dumping it all at once on the client. A StreamingResponse is like turning on the hose; data flows immediately and continuously, without needing a bucket at all. The server just passes chunks along as they become available.
How it works
StreamingResponse takes a generator function or an iterator. In a FastAPI path operation, you write a function that uses yield to produce chunks of data (as bytes or strings). FastAPI iterates over your generator, sending each yielded chunk to the client as part of the HTTP response body. The connection stays open until the generator is exhausted, typically using HTTP's chunked transfer encoding.
When to use it
Use StreamingResponse when the response body is too large to fit comfortably in memory or is generated over time. This is perfect for serving large file downloads (CSVs, logs, videos), streaming audio, or piping the live output from a service like an AI Large Language Model. It dramatically improves the time-to-first-byte for the client.
When not to use it
Avoid it for small, self-contained data, especially structured data like a typical JSON object. A standard JSONResponse is simpler and more efficient for these cases. If you need to stream structured JSON, consider using a format like JSON Lines, where each line is a valid JSON object.
One canonical example
A common use is serving a large video file from disk. Instead of reading the entire multi-gigabyte file into memory, you can write a generator that reads the file in small chunks (e.g., 1MB at a time) and yields each chunk. The user's browser can start playing the video almost instantly while the rest of the file continues to stream in the background, all while your server's memory usage remains minimal.
Interview question
For which scenario is FastAPI's StreamingResponse most appropriate when the response is generated incrementally rather than returned as a static file?
- a.Improving the overall performance of all API endpoints by reducing latency for every request.
- b.Establishing a persistent, bidirectional communication channel for real-time updates.
- c.Sending a large or dynamically generated video stream incrementally to a client.Correct
- d.Returning a small, structured JSON object containing user profile information.
Why? this is the answer
StreamingResponse sends large or dynamically generated content incrementally, avoiding the need to construct the entire response in memory. A static file is usually better served with FileResponse, while a small structured object can use a normal JSON response.
Just read this? Test yourself on what you have been reading.
Read the original → fastapi.tiangolo.com
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on fastapi — each one lists the topics its interview covers.
See open roles