Implement the stream_chat async generator method with ThinkingConfig toggle that controls Gemini 2.5 Flash extended reasoning budget and streams GenerateContentResponse chunks as unified StreamChunk objects.
GenAI Application Engineering › Full-Stack GenAI Applications › Chapter 1 · Chat Completion API with Streaming › Implement a Gemini 2.5 Flash streaming adapter with thinking budget
6:11
Implement the stream_chat async generator method with ThinkingConfig toggle that controls Gemini 2.5 Flash extended reasoning budget and streams GenerateContentResponse chunks as unified StreamChunk objects.
Host: Welcome back to the Full-Stack GenAI Applications course. You're in the chapter on streaming chat — the technique where text from an AI model arrives on your screen word by word, instead of all at once after a long wait. This is the plumbing behind every modern AI chat product. And in this exercise, you're working with something specific — giving a model the ability to think harder when the question is hard, and think less when it's simple. Let's get into why that matters in production.
Expert: Picture a mid-sized company — maybe two hundred engineers — that just shipped an internal AI assistant. It answers policy questions, writes code, summarizes long documents. At first, everyone's happy. Then the finance team looks at the monthly bill, and it's four times what was forecast. Why? Because every single request — even "what's the Wi-Fi password" — is running the model at full reasoning power. Full reasoning means the model spends extra compute walking through its logic internally before answering. That's great for a tricky math problem. It's wildly wasteful for a one-line answer. So the team lead asks: can we make the model think hard only when we need it to, and answer quickly otherwise? That's the exact capability you're building today. Google's newest model in the Gemini family — called Gemini 2.5 Flash — has a dial you can turn. You can tell it: for this request, spend extra thinking time. For that request, skip it. The exercise is about wiring up that dial inside your streaming chat system, so your team can decide, per request, how much reasoning budget to spend.
Host: So in the previous exercise you set up the connection to Google's Gemini model — you built the wrapper that talks to it and defined the shape of the messages flowing through. Now you're adding the actual conversation logic. What exactly are we building in this one?
Expert: You're building the part that carries on a streaming conversation with the model and hands the pieces back to the rest of your application as they arrive. Think of it as a relay runner. The model starts producing its answer in small chunks — a few words at a time. Your code catches each chunk the moment it appears and passes it onward, so the user sees text flowing in real time rather than waiting for a complete paragraph. On top of that relay, you're adding the reasoning dial I mentioned — a simple switch that says "for this request, let the model use extended thinking" or "for this request, skip it and just answer fast." Here's the key idea, the aha moment. Every major AI provider — Google, OpenAI, Anthropic, and others — returns its streaming output in a slightly different shape. Different field names, different structures, different ways of signaling "I'm done." If your application has to know all those shapes, your code becomes a tangled mess. So the pattern you're implementing is called a unified chunk — a single, consistent shape that every provider's output gets translated into before it leaves your adapter. The rest of your application only ever sees this one clean shape. That translation layer is what makes a multi-provider system maintainable. It's the difference between plumbing that scales and plumbing that collapses the third time you add a new model.
Host: Before I let people start — what's the part of this that trips people up? What should they watch for?
Expert: Two things. First, the reasoning dial is not a simple on-off switch in the way you might expect. It takes a number — a budget — that tells the model roughly how much internal thinking to do. If you just pass "true" or "false," it won't work. You need to translate your toggle into the right numeric value. Zero means no extended thinking. A higher number means more. So before you code, decide what your "off" state looks like and what your "on" state looks like, in numbers. Second thing — streaming code uses a pattern called asynchronous generation. That's a fancy way of saying your function doesn't return one answer at the end. Instead, it hands back pieces one at a time, pausing between each one, while other work in your application continues. If you've mostly written traditional functions that compute something and return it, this pattern can feel backwards. The mental shift is: you're not producing an answer, you're producing a stream of small results, and each one gets passed along the moment it's ready. Take a minute to sit with that before you start typing — it'll save you debugging time later.
Host: Alright — so what walks out of this exercise in the student's toolkit? And what comes next?
Expert: After this, you'll be able to take any streaming AI model, wrap it in a consistent interface, and give your team fine-grained control over how much the model thinks per request. That's a real production capability — it's the foundation for cost control, latency tuning, and the kind of per-customer configuration enterprise products need. Your team can take this pattern and apply it to any new model that ships next quarter. In the next exercise, you'll extend this further — you'll add safety handling, so when the model refuses to answer something for policy reasons, your application knows how to detect that and respond gracefully. You'll also clean up a small inconsistency in how Google labels the AI's messages compared to how the rest of the industry does. Small details, but they're what separate a demo from something you'd actually put in front of real users.
Host: Perfect. Open up the exercise, take your time with the reasoning dial, and keep the unified chunk idea in the back of your mind — that's the design pattern doing the real work here. Thanks for listening, and good luck.
Want to go deeper? Explore disciplines with hands-on labs, quizzes, and chapter podcasts.