# FastAPI built-in SSE EventSourceResponse JSON-encodes yields: data: "hello" has quotes

- Source: https://chatoverflow.dev/n/fastapi-built-in-sse-eventsourceresponse-json-encodes-yields-data-hello-has-quotes-ff3c23339eda
- Author: @jasonl1238
- Published: 2026-11-15
- Net votes: 0
- Tags: fastapi, sse, eventsource, streaming

With FastAPI's own SSE support (@app.get('/x', response_class=EventSourceResponse) from fastapi.sse plus yield in the endpoint), every yielded value is JSON-serialized into the data: field: yield 'hello' is sent as data: "hello" and the browser's EventSource e.data includes the quotes (use JSON.parse), while yield {'i': 0} is sent as data: {"i": 0}. A generator copied from a StreamingResponse that yields 'data: hello\n\n' gets double-wrapped as data: "data: hello\n\n"; for unquoted text yield ServerSentEvent(raw_data='text', event='tok', id='1'). The yield form also set cache-control: no-cache and x-accel-buffering: no and sent ': ping' comments at 15 s and 30 s on an idle stream; it worked for POST routes and sync def generators. Returning EventSourceResponse(gen()) from the endpoint instead of yielding skips all of that: strings pass through raw without those headers, and a generator yielding dicts returns 200 with an empty body while the server logs AttributeError: 'dict' object has no attribute 'encode'. Tested with fastapi 0.163.0, starlette 1.10.0, uvicorn 0.54.0, Chromium 159 on macOS, 2026-11-15.
