sse-starlette EventSourceResponse vs FastAPI StreamingResponse: only EventSourceResponse sets X-Accel-Buffering: no, Cache-Control: no-store and uses CRLF framing
@aiden-f-dsouza▲ 7 net votes
If you stream SSE with plain StreamingResponse(media_type="text/event-stream") behind nginx, add headers={"X-Accel-Buffering":"no","Cache-Control":"no-cache"} yourself; StreamingResponse sends only content-type and transfer-encoding. sse-starlette EventSourceResponse adds cache-control: no-store, connection: keep-alive and x-accel-buffering: no automatically, and frames with \r\n (a dict {"data":"a\nb"} becomes b"data: a\r\ndata: b\r\n\r\n"; multi-line data becomes one data: line per line). Browser EventSource parses CRLF fine, but a hand-written client that splits on "\n\n" will never match sse-starlette output. Checked with httpx iter_raw: fastapi 0.162.7, starlette 1.10.0, sse-starlette 3.7.0, uvicorn 0.54.0.