GenAI Agent Engineering › Kubernetes Essentials for GenAI Engineers › Chapter 1 · Containerizing LLM Applications › Write a Python app that calls the Gemini API and returns structured responses
5:10
Build a Python app that renders text from style templates, enforces a word budget, and returns structured, reproducible report responses.
Host: Welcome back. You're in Kubernetes Essentials for GenAI Engineers — a course about running AI applications on the kind of infrastructure your company actually uses in production. This chapter is about containerizing large language model applications. That means taking a piece of AI software and packaging it so it can run reliably anywhere. And the skill you're picking up today — calling a hosted language model from your own code and getting back a clean, predictable answer — is the first brick in every production AI system your team will ever build.
Expert: Let me paint the scenario. Picture a mid-sized insurance company, maybe two hundred engineers, and a small AI team of five or six. Leadership wants a tool that summarizes customer claims. The team spins up a quick demo using a language model from Google called Gemini — that's Google's family of large language models, the kind that can read text and write text back. The demo is beautiful. Everyone's impressed. Then reality hits. The script only runs on one engineer's laptop. The model sometimes returns a paragraph, sometimes a bulleted list, sometimes JSON wrapped in an apology. The downstream system that's supposed to consume this output keeps breaking because the shape of the answer keeps changing. This is the exact problem every team hits the moment they move from a notebook demo to something real. Before you can deploy an AI application, before you can containerize it, before you can even think about Kubernetes — which is the system that manages running software across many machines — you need the application itself to behave predictably. That's what today is about. You're building the reliable core that everything else will wrap around.
Host: This is your starting point for the chapter. The chapter overview walked through the big picture — what containers are, why we package AI apps. Now we get specific. So what exactly are we building?
Expert: You're building a small Python program that does one job. It takes a prompt — a piece of text you send in, like "summarize this claim" — and it sends that prompt to Gemini, Google's hosted language model service. Gemini thinks, writes a response, and sends it back. Your program then hands that response to whoever asked, in a clean, predictable shape. And here's the key idea, the thing that makes this exercise more than just "hello world with an AI." Language models, by default, return free-form text. Beautiful prose, but unusable for a program that needs to extract specific fields. So you're going to ask the model not just for an answer, but for an answer that follows a specific structure — think of it like giving someone a fill-in-the-blank form instead of a blank sheet of paper. You'll define the shape you want: maybe a title, a summary, a confidence score. And Gemini will fill in those blanks. The result is that your program becomes a reliable building block. Another program can call yours and know exactly what fields it's getting back, every single time. That's the leap from toy to production.
Host: Okay, so before someone dives in — what's the part that trips people up here?
Expert: Two things. First, the authentication. To call Gemini, you need an API key — that's basically a password that proves your code is allowed to use the service. The trap is hardcoding that key directly into your program. Don't. You'll load it from what's called an environment variable — a setting that lives outside your code, in the surrounding system. That matters later when you containerize, because the container needs to receive the key from outside, not carry it baked in. Second trap — and this is the subtle one — when you ask the model for structured output, you have to be explicit about the structure. If you just say "give me JSON," you'll sometimes get JSON wrapped in explanations, or JSON with made-up extra fields. The fix is to describe the exact shape you want, using the library's built-in support for structured responses, and then validate what comes back before trusting it. Treat the model's output like input from a stranger. Always.
Host: Great. So let's land the plane. What will you walk away being able to do?
Expert: After this, you'll be able to take any hosted language model and wrap it in your own code so that it returns clean, structured, predictable results — every time. You'll know how to keep secrets out of your source code, how to define the shape of what you want back, and how to handle the cases where the model doesn't cooperate. This is the foundational building block your team will use for document processing, customer support automation, internal search — basically any place where an AI needs to produce output that another piece of software consumes. Bring this pattern back to your architecture discussions and you'll find it comes up constantly. And this is the final and only exercise in this chapter, so when you finish, you'll have the complete, working core — a reliable AI component that's ready to be packaged into a container in the next chapter, where things get really interesting. Thanks for listening, and good luck with the build.
Want to go deeper? Explore disciplines with hands-on labs, quizzes, and chapter podcasts.