Replies: 2 comments
-
|
The from fastapi import FastAPI
from fastapi.responses import StreamingResponse
import json
@app.get("/large")
async def large_endpoint():
data = ["a"] * 10_000_000
async def generate():
yield json.dumps(data)
return StreamingResponse(generate(), media_type="application/json")When you return a On your second question — downsides of
For large payloads, returning |
Beta Was this translation helpful? Give feedback.
-
|
I think this is expected behavior in FastAPI.
return ["a"] * 10000000FastAPI treats that as application data, runs the normal response serialization flow, and only then passes the result to the configured response class. That is why If you want to completely bypass @app.get("/")
async def get_data():
return JSONStreamingResponse(["a"] * 10000000)or preferably stream/generate the data without building the entire list first: @app.get("/")
async def get_data():
async def generate():
yield b"["
first = True
for item in some_large_iterator():
if not first:
yield b","
first = False
yield orjson.dumps(item)
yield b"]"
return StreamingResponse(generate(), media_type="application/json")Using a streaming response as the default response class can work, but there are trade-offs:
So the practical answer is: use |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
When FastAPI serves a large payload, it blocks the app from serving other requests.
I want to use the above
JSONStreamingResponseas the default response class to get around this problem.However, it seems like
jsonable_encoderis still called on the response payload unless I explicitly callreturn JSONStreamingResponse(json_stream_generator(content))in the endpoint.Is there a way I can return JSON as per normal in the endpoint but skip
jsonable_encoderand use theJSONStreamingResponse?Lastly, is there a downside to using
JSONStreamingResponseas the default response class, other than losing Pydantic validation on the response payload?Operating System
Linux
Operating System Details
No response
FastAPI Version
0.129.0
Pydantic Version
2.12.5
Python Version
3.12.11
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions