Back to Bytes

Add CORS middleware configuration and streaming healthcheck endpoint — lab audio overview

2026-04-21

Configure CORSMiddleware on the FastAPI application for browser EventSource clients and implement a GET /api/v1/chat/stream/health endpoint that returns the streaming service status. You will also add startup and shutdown lifespan events for graceful resource management.

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

5:26
Configure CORSMiddleware on the FastAPI application for browser EventSource clients and implement a GET /api/v1/chat/stream/health endpoint that returns the streaming service status. You will also add startup and shutdown lifespan events for graceful resource management.
Share

More from this chapter

Transcript
Host: Welcome back. You're working through Full-Stack GenAI Applications, and you're inside the chapter on streaming chat — the one focused on delivering language model responses token by token, the way you see text appear word-by-word in modern chat interfaces. This whole chapter is about the plumbing that makes that experience possible, and today's skill is the one that decides whether your streaming service is actually usable by a real web browser, or just by a command-line tool on your laptop. Expert: Picture a small product team at a mid-sized software company. Maybe twenty engineers, a few designers, and they're building an internal assistant that employees will use through their browser. The backend team gets the streaming working beautifully — they can hit it from a terminal, words flow back, everything looks great. Then the frontend developer plugs it into the company's web page, opens the browser, and nothing works. The browser silently blocks the connection for security reasons. Then, a week later, the operations team asks, "How do we know if the streaming service is healthy? Our monitoring dashboard needs to ping something." And suddenly the backend team realizes they built a service that works in isolation but isn't actually ready for a real production environment. That's exactly the gap you're closing in this exercise. It's the difference between "it works on my machine" and "it works for our company." Host: In the previous exercise, you built the core streaming engine — the part that takes a chat request and pushes the language model's response back piece by piece as it's being generated. Now you're wrapping that engine in everything it needs to survive in a real production environment. So, what exactly are we building here? Expert: Three things, and they all work together. First, you'll configure something called Cross-Origin Resource Sharing — usually shortened to C-O-R-S, or "cors." This is a browser security rule. By default, if your web page is hosted at one web address and it tries to talk to a backend hosted at a different address, the browser refuses the connection. Cors is the permission slip that tells the browser, "yes, these two are allowed to talk to each other." You'll add this as what's called middleware — a piece of code that automatically runs on every incoming request without you having to wire it up each time. Second, you'll add a health check — a simple web address that anyone can ping, and it answers back, "yes, the streaming service is alive and ready." Monitoring tools use this. Load balancers use this. It's how the rest of your infrastructure knows your service is healthy. Third, you'll add startup and shutdown hooks — code that runs once when the server boots up, and once when it's shutting down. That's where you prepare resources and clean them up gracefully so nothing leaks. Host: Now, this sounds like three small things stitched together. Is there a concept underneath all of them — the key idea someone should hold in their head before they start typing? Expert: Yes, and it's important. The key idea is that a web service has a lifecycle, and a real production service needs to be honest about that lifecycle to the outside world. Think of it like a restaurant. The kitchen cooking food is your streaming endpoint from the last exercise. But a restaurant isn't just a kitchen. It's also the front door that decides who's allowed in — that's cors. It's the sign on the window that says "open" or "closed" — that's the health check. And it's the opening routine in the morning and the closing routine at night — that's your startup and shutdown hooks. In this exercise you're building the restaurant around the kitchen. The kitchen alone doesn't serve customers. Now, before you start, there's one thing that trips almost everyone up. Host: Okay, what's the gotcha? What should people watch for? Expert: The gotcha is the browser's streaming listener — the built-in tool browsers use to receive a stream of events from a server. It has very specific requirements that don't show up when you test with a terminal. Two things in particular. First, the cors permission must explicitly allow the kind of headers that streaming uses — if you just copy a default cors setup from a tutorial, streaming will often fail silently in the browser even though regular requests work fine. You need to allow credentials and expose the right response headers. Second — and this one surprises people — the health check endpoint should be extremely lightweight. Do not have it actually talk to the language model to test things. A health check that calls out to an expensive external service will get hammered by monitoring tools pinging every few seconds, and you'll rack up real costs or get rate-limited. Your health check should just confirm the service itself is running and report its status. It's a heartbeat, not a full physical exam. Keep it boring, keep it fast, and make sure it returns in milliseconds. Host: That's a great tip — the health check should be a heartbeat, not a physical exam. So after this exercise, what can the listener actually do? Expert: After this, you'll be able to take a streaming backend and make it genuinely browser-ready and production-ready — the kind of service a frontend developer on your team can plug into a real web page without anything silently breaking, and the kind of service your operations team can monitor and deploy with confidence. This is the final exercise in this objective, which means with this done, you now have the complete picture of a production streaming chat service — the data models, the streaming engine, and the production wrapper around it. That's a working implementation you can bring back to your team's architecture discussions the next time someone asks, "how should we expose our language model to the browser?" You'll have a concrete answer. Thanks for listening, and good luck with the build.

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