Sub topic 15
Why proxies and platforms break streaming
This chapter assumes you can read Python and JavaScript, know how Server-Sent Events (SSE) frame a stream, and have a server that relays an LLM stream to a browser. Your relay works on your laptop. After deployment, the text arrives in one lump at the end. This chapter explains why and how to fix it.
Why buffering happens
A proxy is a server that sits between the client and your app. Examples are nginx, a CDN, a load balancer, or a corporate gateway. Many proxies buffer responses by default. They collect the body from your app, then send it to the client in larger pieces. That is efficient for web pages. It defeats streaming, because the client sees nothing until the buffer fills or the response ends.
Compression causes the same problem. A gzip layer waits to fill a block before it emits bytes.
Platforms add their own limits. Some serverless runtimes return a response only after your function finishes. Others cap how long a connection can stay open.
Symptoms
- Text appears all at once after a delay equal to the full generation time.
- It streams locally and in a direct
curlcall, but not through the public URL. - Streaming works for short answers and fails after a fixed timeout, such as 30 or 60 seconds.
Fixes
- Turn off buffering at the proxy. The FastAPI example from earlier sends
X-Accel-Buffering: no, which tells nginx not to buffer that response. In nginx config you can setproxy_buffering off;for the route. - Disable compression for the stream. Do not gzip
text/event-streamresponses. - Set the right headers. Use
Content-Type: text/event-streamandCache-Control: no-cache. Do not set aContent-Length. - Send keep-alive comments. Idle connections get closed by load balancers. A comment line such as
: pingevery 15 seconds keeps the connection active, and clients ignore it. - Raise idle and read timeouts. Make the proxy timeout longer than your slowest generation.
- Choose a streaming-capable runtime. Check your platform's documentation for response streaming support.
import asyncio
from fastapi import FastAPI
from fastapi.responses import StreamingResponse
app = FastAPI()
async def tokens_with_keepalive():
for token in ["Hello", " from", " a", " slow", " model"]:
await asyncio.sleep(1)
yield f"data: {token}\n\n"
yield ": ping\n\n"
yield "data: [DONE]\n\n"
@app.get("/stream")
async def stream():
return StreamingResponse(
tokens_with_keepalive(),
media_type="text/event-stream",
headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
)Testing the deployed path
Test the public URL, not only localhost. Run curl -N https://your-host/stream. The -N flag turns off curl's own buffering. If the lines arrive one at a time, the whole path streams. If they arrive together, something in between is buffering. Test each hop until you find it.
Exercise
A streamed chat works at http://localhost:8000 but shows the full answer at once on the production domain. The app code is the same. What is the most likely cause, and how would you confirm it?
Show answer
A proxy, CDN, or compression layer between the client and the app is buffering the response. Confirm by running curl -N against both URLs and comparing: direct output arrives line by line, while the production output arrives in one lump. Then disable buffering (for example proxy_buffering off or the X-Accel-Buffering: no header) and turn off compression for the stream.