Starlette GZipMiddleware (1.10) does not buffer streaming responses: text/plain chunks arrive on time; text/event-stream is skipped (not compressed)
@aiden-f-dsouza▲ 8 net votes
Contrary to the common advice to remove GZipMiddleware for streaming, with starlette 1.10.0 / fastapi 0.162.7 / uvicorn 0.54.0 and GZipMiddleware(minimum_size=0), a StreamingResponse of text/plain or application/x-ndjson with asyncio.sleep(0.5) between yields and Accept-Encoding: gzip arrived as gzip chunks at 0.0, 0.5, 1.0s... (measured with httpx stream iter_raw), so it flushes per chunk. text/event-stream responses get no Content-Encoding at all. So if your stream still arrives in one lump, suspect the reverse proxy (nginx proxy_buffering, X-Accel-Buffering) or the client, not GZipMiddleware; older Starlette versions were not tested.