Week 20 • Day 2Week 20

Day 2:Streaming API Endpoints & Server-Sent Events Routes

Battle-tested daily lesson note • 100% runnable code, interview Q&A, test suite & interactive quiz

Day 2: Streaming API Endpoints & Server-Sent Events Routes

1. First-Principles: Zero-Buffering Streaming Endpoints

In FastAPI, streaming endpoints return a StreamingResponse wrapping an asynchronous generator:

python
@app.post("/v1/copilot/query") async def copilot_query_endpoint(payload: CopilotQueryPayload): generator = app_service.stream_query(payload) return StreamingResponse( generator, media_type="text/event-stream", headers={ "Cache-Control": "no-cache", "Connection": "keep-alive", "X-Accel-Buffering": "no", }, )

The endpoint multiplexes:

  • event: thought: SRE diagnostic reasoning steps
  • event: citation: Runbook sources and section references
  • event: component: Interactive Generative UI components (HITL Action Cards)
  • event: token: Response tokens
  • event: done: Stream termination sentinel [DONE]

In Simple English: Zero-Buffering SSE Routes (Month 5 Capstone Step 2)

By default, reverse proxies (like Nginx) buffer HTTP responses until finished — secretly destroying token streaming! Production streaming routes emit proper text/event-stream headers (Cache-Control: no-cache, X-Accel-Buffering: no) and format every chunk cleanly as event: <type>\ndata: <json>\n\n with incremental backpressure safety.

Interactive Sandbox: Open Tab 2 (Codes) & Tab 4 (Test & Troubleshooting) to test the pure-Python SSE Formatter & Split-Chunk Stream Parser live!