Back to Bytes

Build async generator streaming endpoint with StreamingResponse — lab audio overview

2026-04-21

Implement the core streaming endpoint at POST /api/v1/chat/stream using an async generator that yields SSE-formatted token strings. You will build the stream_tokens() async generator, wire it into a FastAPI StreamingResponse with media_type text/event-stream, and create the FastAPI application with

GenAI Application Engineering › Full-Stack GenAI Applications › Chapter 1 · Chat Completion API with Streaming › Build a FastAPI SSE streaming response endpoint

5:26
Implement the core streaming endpoint at POST /api/v1/chat/stream using an async generator that yields SSE-formatted token strings. You will build the stream_tokens() async generator, wire it into a FastAPI StreamingResponse with media_type text/event-stream, and create the FastAPI application with
Share

More from this chapter

Transcript
Host: Welcome back to Full-Stack GenAI Applications. You're in the chapter on Chat Completion APIs with Streaming — and the word "streaming" here is the whole point. It means delivering a chatbot's answer word by word as it's being written, instead of making the user wait for the full response. This is the discipline of building the back-end plumbing that makes AI products feel alive. So why does this specific skill matter the moment you ship something real? Expert: Picture a mid-sized company — say a fifty-person SaaS team — that just launched their first AI assistant inside their product. They wired it up the simple way: user asks a question, the server calls the language model, waits for the entire answer, then sends it back. In testing it felt fine. In production, support tickets start rolling in. "The bot is broken." "It's frozen." What's actually happening is the model is taking eight, ten, sometimes fifteen seconds to generate a long answer, and during that whole window the user is staring at a spinner. They give up and refresh. Every chatbot you've ever enjoyed using — the ones where words appear as if someone's typing — solves this with streaming. The server sends each small chunk of text the instant the model produces it. The user sees motion immediately, which makes a fifteen-second answer feel fast. This is not a polish feature. It's the difference between a product people trust and a product people abandon. And the back-end pattern that makes it work is exactly what you're about to build. Host: Got it. And in the previous exercise you built the data shapes — the structured definitions for what a chat message looks like, what a chat request looks like, and a small helper that formats each chunk of text into the specific wire format the browser expects for streaming. Now you're extending that. So what exactly are we building today? Expert: Today you're building the actual streaming endpoint — the live pipe that the browser connects to. Three pieces fit together. First, you'll create the web application itself using FastAPI, which is a popular Python framework for building web back-ends. Second, you'll add a single URL on that application — a chat streaming address — that accepts incoming chat requests. Third, and this is the heart of it, you'll write a special kind of function that produces output piece by piece over time, instead of computing everything and returning once. In Python this pattern is called an async generator — "async" meaning it can pause and resume without blocking other users, and "generator" meaning it yields results one at a time. Here's the key idea. A normal web response is like mailing a letter — you write the whole thing, seal it, send it. A streaming response is like a phone call — the line stays open and you keep talking. Your function will produce each token — a token being a small piece of text, often a word or part of a word — wrap it in the streaming wire format you built last time, and hand it off. FastAPI keeps the connection open and forwards each piece to the browser the instant it's ready. You'll also tell FastAPI that this response is event-stream content, which is the official label browsers recognize as "expect a live feed, not a one-shot reply." Host: That makes sense. So what's the part that trips people up? What should I watch out for before I dive in? Expert: The single biggest trap is mixing up two similar-sounding Python features. There's a regular function that returns a value, there's a generator that yields values one at a time, and there's an async generator that yields values one at a time AND can pause for slow operations like waiting on the language model. For streaming to work, you need that third kind — async generator. If you accidentally write a regular function or a regular generator, FastAPI will either send everything at once at the end, or worse, block other users while your function runs. The tell-tale sign you've got it right is that your function is declared as asynchronous and uses the yield keyword to hand back each chunk, rather than the return keyword to hand back one final result. The second thing to watch for is the wire format. Each chunk you send out has to follow a very specific text shape that browsers understand — the formatter you built in the previous exercise handles that. Use it. Don't try to hand-write the format inline. And finally, send a clear end-of-stream marker when you're done, so the browser knows the conversation chunk is complete and can close the connection cleanly. Host: Perfect. Bring us home — what will I be able to do after this, and what's coming next? Expert: After this exercise, you'll be able to take any slow, long-running AI response and deliver it to a user as a live, word-by-word feed — the same experience users expect from every modern chat product. That's a foundational building block your team can drop into any AI feature you're shipping, whether it's a customer support bot, a documentation assistant, or an internal copilot. Next, you'll round this out by configuring two things on top of what you just built. First, the permissions that let a web browser running on one domain talk to your back-end running on another — without that, the browser blocks the connection for security reasons. Second, a small health-check address that monitoring tools can ping to confirm your streaming service is alive. Together, those three exercises give you a complete, production-shaped streaming chat back-end you can bring straight into your team's architecture conversations. Thanks for listening, and enjoy the build.

Want to go deeper? Explore disciplines with hands-on labs, quizzes, and chapter podcasts.