Implement ChatRequest and ChatMessage Pydantic models with SSE frame formatter — lab audio overview
2026-04-21
Build the Pydantic data models that define the chat request schema and SSE frame formatting. You will create ChatMessage, ChatRequest, and StreamChunk models with field validators for provider names and temperature ranges, plus an SSE frame formatter that outputs W3C-compliant event-stream strings.
GenAI Application Engineering › Full-Stack GenAI Applications › Chapter 1 · Chat Completion API with Streaming › Build a FastAPI SSE streaming response endpoint
5:26
Build the Pydantic data models that define the chat request schema and SSE frame formatting. You will create ChatMessage, ChatRequest, and StreamChunk models with field validators for provider names and temperature ranges, plus an SSE frame formatter that outputs W3C-compliant event-stream strings.
Host: Welcome back. You're in Full-Stack GenAI Applications — a course about building real, end-to-end AI products that run in production, not just notebook demos. The chapter you're starting now is called Chat Completion API with Streaming. In plain language, that means building the part of a chat app that sends words back to the user one piece at a time, the same way you see ChatGPT typing letters as it thinks. Why does this matter so much in your day job?
Expert: Picture this. You're on a small platform team at a mid-sized company, maybe forty engineers, and the product team has just decided to add an AI assistant to your customer support tool. The very first complaint from your beta testers is going to be the same one every team hears: "It feels slow." And here's the thing — the AI itself isn't slow. It's actually generating words within a second. The problem is your backend is waiting for the entire answer to finish before sending anything to the browser. So the user stares at a blank screen for eight, ten, fifteen seconds. The fix is a technique called streaming. Instead of waiting for the whole response, your server pushes each little piece of text — each token, which is roughly a word or part of a word — to the browser the moment the AI produces it. That single change is the difference between an assistant that feels alive and one that feels broken. Every team building an AI chat feature hits this within the first week. So this chapter is about getting it right from the start, with a clean foundation that works across four different AI providers — OpenAI, Anthropic, Google, and a local model.
Host: Good. So this is the first exercise in the chapter — your starting point. The chapter overview gave you the big picture; now we get specific. What exactly are we building in this first exercise?
Expert: In this exercise you're building the foundation — the shapes of the data that flow in and out of your chat system, and the formatting rule for how you'll send streamed text back to the browser. So three things. First, a definition of what a single chat message looks like — basically, who said it and what they said. Second, a definition of what a full chat request looks like — the list of messages so far, which AI provider to use, and how creative the AI should be, which is a number called temperature. Third, a definition of what one streamed chunk looks like as it's flying back to the browser. And then the fourth piece is a small helper that takes any chunk of text and wraps it in the exact format that browsers expect for streaming. That format is called Server-Sent Events, or SSE — it's a web standard, defined by the group that runs the web, for sending a continuous stream of small messages from a server to a browser over a normal web connection. The key idea here is separation of concerns. Before you write any AI logic, before you touch any provider, you define the contracts — the shapes — that everything else in the system will agree on. The library you'll use to define these shapes is called Pydantic. Think of it as a tool that lets you describe what valid data looks like, and it automatically rejects bad data at the door. So if someone sends a request with a temperature of fifty, or a provider name you don't support, your system says no immediately, with a clear error, before any AI gets called.
Host: That makes sense — get the contracts right first, build on top of them later. So before someone hits play on this exercise and dives in, what's the part that tends to trip people up?
Expert: Two things. The first is the streaming format itself. Server-Sent Events looks deceptively simple — it's just text — but it has very strict rules about how each message is structured. Each message has a label, then the actual content, and then a specific blank-line separator at the end. If you get that separator wrong, the browser will either show nothing, or it'll buffer everything and then dump it all at once, which defeats the whole point. So when you write your formatter, follow the spec exactly, character for character, including the line breaks. The second thing is validation. Pydantic gives you the ability to add custom rules — for example, "the provider name must be one of these four strings" or "the temperature must be between zero and two." It's tempting to skip these and just trust the input. Don't. Add the validators now. Every minute you spend on validation in this exercise saves you an hour of debugging weird production errors later, when bad data sneaks through and crashes your AI calls deep in the system.
Host: Solid advice. So bring it home — what will the listener walk away able to do, and where does this lead?
Expert: After this exercise, you'll be able to design the data contracts for any streaming AI endpoint, and you'll be able to format a stream of tokens in the exact way browsers expect. That's a foundational building block your team can reuse in every AI product you ship — chat, copilots, document assistants, all of them. In the next exercise, you'll take these shapes and this formatter and plug them into a live streaming endpoint, where an actual AI provider sends back tokens and your code pushes them out to the browser in real time. That's where it starts to feel magical. Thanks for listening, and enjoy the build.
Want to go deeper? Explore disciplines with hands-on labs, quizzes, and chapter podcasts.