Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

To stream LLM output through your own backend, have FastAPI return either a Server-Sent Events (SSE) response or a raw StreamingResponse, then make the browser consume that format incrementally. For browser-facing chat, SSE is useful when you want named events and reconnection; raw streaming is simpler when you control a text-only protocol. The key caveat is that a Python yield does not prove the browser received or displayed the chunk immediately: middleware, proxies, compression and client parsing can all affect delivery.

What does it mean to stream LLM output through your own backend?

Your backend sits between the browser and the model provider. It receives the browser’s request, obtains output from the model, and forwards pieces of that output as they become available instead of waiting for the complete answer. This lets the application deliver incremental updates while keeping provider-specific calls behind its own server boundary.

There are two distinct streams to keep working: the model provider must produce output incrementally, and your application must pass that output through to the browser incrementally. If the provider returns a completed answer before your generator yields, the browser cannot recover the time spent waiting. Likewise, yielding chunks in Python does not by itself guarantee that the network path or browser will expose them as they are produced.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

FastAPI documents two relevant response paths: EventSourceResponse for SSE and StreamingResponse for raw strings or bytes. The framework added its documented SSE API in FastAPI 0.135.0 and its documented stream-data path in 0.134.0. Check the installed version before adopting either API; older versions may not provide these documented interfaces.

#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter | 4Pack Type C Female to A Male Cable Converter for Apple Duo 17 16 15 14 Plus,iWatch Watch 12 11 10,iPad 9 8 Air,Airpods 5 4
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.

Should I use SSE or a raw StreamingResponse?

Choose SSE when the browser should receive framed events, such as named token or completion events, and you want the browser’s native EventSource reconnection behavior. Choose StreamingResponse when you deliberately want to send raw text or bytes and will define how the client interprets them.

Choice What FastAPI sends Useful when
EventSourceResponse UTF-8 SSE events using the text/event-stream format You need event names, IDs, retry values, comments or SSE framing
StreamingResponse Yielded strings or bytes passed through without JSON conversion Your client and server intentionally use a raw text or byte stream

FastAPI’s SSE guide documents yielding events with EventSourceResponse. Its stream-data guide describes StreamingResponse as passing each yielded chunk through without converting it to JSON, including for AI LLM output. Raw streaming is not automatically SSE: if you send plain text, an SSE parser should not be expected to interpret it as events.

How do I return SSE events from FastAPI?

Declare an endpoint with response_class=EventSourceResponse and yield event objects. FastAPI can serialize ordinary yielded objects into the SSE data: field; use ServerSentEvent when you need an explicit event name, resume ID, retry value or comment.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
from collections.abc import AsyncIterable
from fastapi import FastAPI
from fastapi.sse import EventSourceResponse, ServerSentEvent

app = FastAPI()

@app.post("/chat/stream", response_class=EventSourceResponse)
async def chat_stream() -> AsyncIterable[ServerSentEvent]:
    # Replace with an async iterator from the model provider.
    for token in ["Hello", " there"]:
        yield ServerSentEvent(data=token, event="token")
    yield ServerSentEvent(raw_data="[DONE]", event="done")

This example shows response framing, not a complete chat service. Replace the illustrative tokens with the asynchronous iterator supplied by your model integration, and implement the application’s own request validation, authentication, cancellation and error behavior. FastAPI also documents synchronous def generators and Iterable return types where appropriate. The SSE reference describes the response and event types.

Rank #2
Sale
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter | USB C Dongle with 90W Max Power Delivery, 3×5Gbps USB A Data Ports, for MacBook, Dell, and More (Charger Not Included)
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.

The example uses POST, which FastAPI’s SSE support allows. That does not mean a browser’s native EventSource can submit a prompt body: its constructor takes a URL and an optional credentials setting, not an arbitrary request body. For a prompt-bearing POST, a common choice is fetch() and a client-side SSE parser that reads the response body incrementally.

How does the browser know where an SSE event ends?

SSE is a UTF-8 text protocol with the text/event-stream media type. A blank line terminates an event and triggers dispatch; the browser does not dispatch a final event that ends without that terminating blank line. Valid line endings include CRLF, LF and CR.

Each event can carry fields such as data, event, id and retry. Multiple data: lines in one event are combined with newline characters. A line beginning with a colon is a comment, not a dispatched event; comments can serve as keep-alive traffic. These are protocol rules in the WHATWG Server-sent events standard.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Do not assume that one network read corresponds to one event. A read can end in the middle of a UTF-8 character, a line or an event, or it can contain several complete events. For a fetch()-based client, use a parser that retains incomplete data between reads, decodes UTF-8 safely across read boundaries, and dispatches only after it has recognized the event-terminating blank line. Splitting each read on a newline and immediately treating each part as a complete event is not sufficient.

Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter | 85W Max PD, 2 USB 3.0 & 1 USBC Data Ports, SD/TF Card Reader, for Laptops/Mac and Other Type C Devices (Charger Not Included)
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.

When should I use fetch instead of EventSource?

Use native EventSource when a URL-based event-stream request fits the endpoint and the browser’s built-in reconnection behavior is useful. Use fetch() with an incremental parser when the request needs a body, such as a prompt sent with POST, or when the client needs control that does not fit the native interface.

FastAPI’s ability to send SSE over POST is a server capability; it does not add body support to the browser’s EventSource constructor. If you choose fetch(), the application becomes responsible for reading and parsing the stream. It should distinguish protocol framing from the arbitrary boundaries of network reads and should define what the UI does when the stream closes or reports an error.

Native EventSource reconnects after a connection loss unless the client closes it. When SSE events include IDs, a reconnect request communicates the last received ID in the Last-Event-ID header. This transport behavior does not store prior model output or decide what should be replayed: the application must define whether IDs map to retained events, token positions or some other resumable state. Without that application behavior, reconnection alone does not reconstruct a lost answer.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Why does the browser display chunks in a burst?

A burst means data became visible to the client in groups rather than at the cadence expected from the application. It does not identify a single cause. Buffering can occur in middleware, compression, a reverse proxy, a CDN or other network layer; the browser may also appear to wait if the client parser does not dispatch until it sees a complete event.

Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray | Data transfer & Fast charging for MacBook, iPhone 17/16/15, iPad, Samsung Galaxy, Apple Watch
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft

FastAPI’s SSE guidance includes a 15-second ping comment, Cache-Control: no-cache and X-Accel-Buffering: no. The latter is intended to prevent buffering in some proxies, including Nginx, and the headers and ping help with particular intermediary behaviors. They are useful signals, not guarantees that every layer will stream immediately. The WHATWG standard also warns that block buffering, line buffering that expects different line endings, and HTTP chunking by a layer unaware of timing requirements can affect delivery. Comments about every 15 seconds can help protect against some legacy proxies that drop idle connections.

Diagnose the path in stages rather than assuming that adding a header fixes it:

  1. Record yield times. Log a timestamp immediately before each application yield. This establishes when the application attempted to hand off each piece.
  2. Test the app server directly. If possible, request the server without going through the public proxy and observe whether response bytes arrive incrementally.
  3. Compare the proxied route. Repeat against the public route and compare arrival cadence with the direct request. A difference narrows the investigation to components present only on the proxied path.
  4. Inspect response and configuration. Check the content type, SSE framing, response headers, compression settings and middleware that might collect or transform response data.
  5. Test parsing separately. Feed the browser parser complete known events and events split across multiple reads. Confirm that it waits for the blank-line terminator and handles more than one event in a read.

This sequence helps locate where the observed delay enters the path; it cannot identify the cause in an unknown deployment without those measurements. Verify behavior with the actual ASGI server, proxy and hosting setup in use. Neither X-Accel-Buffering: no nor a successful Python yield proves that every intermediary and the browser are delivering promptly.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What should I decide before shipping a stream?

Streaming changes delivery, not the application’s responsibilities around the request or the answer. Before shipping, make these choices explicit:

Best Value
Sale
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display | 85W Max PD, 1 Powered USB-C 5Gbps & 2 Powered USB-A 3.0 5Gbps Data Ports, for MacBook Pro, MacBook Air, Dell and More (Black)
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
  • Protocol: decide whether the browser consumes SSE events or raw text/bytes. Do not label a raw stream as SSE unless it follows SSE framing.
  • Event shape: for SSE, decide which events carry content and whether a distinct completion event is useful. Use IDs and retry values only when the application has defined their meaning.
  • Request interface: check whether the browser request needs a prompt body. Native EventSource does not submit one; a body-bearing request needs another client pattern, such as fetch() and an SSE parser.
  • Resume behavior: decide whether reconnecting should resume, restart or fail. The browser’s last-event ID header is transport information, not a replay store.
  • Infrastructure path: test the direct and public routes, and inspect middleware, compression and proxy behavior rather than relying on a single response header.
  • Version: verify the FastAPI version installed in the application against the documented API’s introduction version.

Provider-specific iterator behavior, authentication patterns, ASGI server configuration, cancellation semantics and deployment-platform buffering depend on the application and infrastructure. The framework and protocol documentation do not establish one universal setting that resolves those deployment-specific questions.

Frequently Asked Questions

Can I send JSON objects over a FastAPI SSE stream?

Yes. FastAPI’s SSE response can serialize ordinary yielded objects into the event’s data field. If the event must contain raw text instead, use the documented raw-data option rather than assuming the object will be sent unchanged.

What happens if an SSE connection closes before the model finishes?

The browser’s reconnection behavior does not determine whether model generation continues or how a partial answer is recovered. Those outcomes depend on how the server handles disconnects and whether the application has implemented resumable event storage.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Does SSE guarantee lower latency than a raw response?

No. SSE defines event framing and browser processing, not a latency guarantee. Actual delivery timing depends on the application and the intervening infrastructure.

Can I use FastAPI’s SSE response with a synchronous generator?

FastAPI’s SSE guidance supports synchronous def generators and Iterable return types where appropriate, as well as asynchronous iterables. Choose the form that matches how the application obtains its chunks.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.