◉GEMINI LABJP
●2.5 ROUTE — From today, 12/1, calls to gemini-2.5-pro and 2.5-flash are rerouted to newer models automatically●CLI 0.71.1 — Gemini CLI stable is still v0.71.1, with a 0.73 nightly published on 11/30●INTRO END — Introductory pricing for 3.8, 3.7 and 3.6 Flash ends on 12/31. 30 days left●VERTEX — Developers are asking how to run Gemini CLI with Vertex AI credentials●NEON — Gemini 4 Neon became generally available on 11/18 (paid tier only)●IMAGE TEXT — A new test shows text inside an image can sway Gemini's answer●2.5 ROUTE — From today, 12/1, calls to gemini-2.5-pro and 2.5-flash are rerouted to newer models automatically●CLI 0.71.1 — Gemini CLI stable is still v0.71.1, with a 0.73 nightly published on 11/30●INTRO END — Introductory pricing for 3.8, 3.7 and 3.6 Flash ends on 12/31. 30 days left●VERTEX — Developers are asking how to run Gemini CLI with Vertex AI credentials●NEON — Gemini 4 Neon became generally available on 11/18 (paid tier only)●IMAGE TEXT — A new test shows text inside an image can sway Gemini's answer
Articles/API / SDK
◈ API / SDK/2026-05-20Advanced

Evolving Gemini API Structured Output Schemas in Production — Design Notes from an Indie App

How I rebuilt the JSON contract layer for a Gemini-powered recommendation feature in a long-running indie app — Dual-Emit, Sunset protocol, and a Python compatibility checker.

gemini-api291structured-output29schema-evolutionproduction140indie-development5

✦ Premium Article

About a month ago, one of the wallpaper apps I run as a solo developer started throwing errors at 5am from a Gemini-powered recommendation endpoint. The Crashlytics alert woke me up. The error trail read Type 'undefined' is not assignable to type 'string' — a Zod validation failure on the client side, and the crash rate was already climbing.

The cause was almost embarrassing. The day before, I had added an optional field to the response_schema in Google AI Studio. Gemini happily generated the new field, but somewhere in the process, a different optional field changed its "unset" behavior from undefined to an empty string. An undocumented behavior shift. On every device that opened recommendations that morning, the theme list rendered as an empty strip. The path from one wallpaper to the next simply stopped, and the ads that lived along that path stopped with it. Before I had counted anything, my hands had already gone still.

What I really learned was this: the JSON that Gemini returns is not just data. It is a contract you make with every installed client — and it is a fragile contract, because a third-party model update can rewrite it without warning. This article is a record of the design I rebuilt after that incident: how I now treat schemas as contracts, the Dual-Emit pattern I've been running since April 2026, the Sunset protocol that follows it, and the Python compatibility checker that keeps the whole thing honest.

What broke, and what I couldn't see

I run wallpaper and healing-sound apps on my own, and Gemini API handles recommendation tagging and short intro-text generation in several of them.

What I actually knew that morning came down to three things:

  • The crashes appeared only in apps that used Gemini-driven recommendations
  • The failure was client-side Zod validation receiving undefined
  • The code fix would take about an hour, but old builds would keep running for as long as store review and staged rollout took

The painful part wasn't the incident itself — it was the root-cause investigation. All I had were client-side Zod failure logs. The actual raw Gemini responses were essentially gone. Cloudflare Workers retained logs for 7 days, but the structured output bodies were being stripped to save space. Half of the post-incident work was about fixing this observability gap, not the schema bug.

Doubt the model at the entrance; make promises only at the exit. Since that morning, I've applied this line to every endpoint that talks to Gemini.

Treat the schema as a contract — separate the internal IR from the public surface

The first thing I changed was the assumption that "Gemini's response equals our API response." I split the model into three layers: a permissive input schema that accepts whatever Gemini sends, an internal IR (Internal Representation) that the rest of my code is allowed to depend on, and an external schema that defines the actual public contract with the client.

// schemas/recommendation.ts
import { z } from "zod";
 
// Layer 1: Input schema — absorb Gemini-side variance here.
export const GeminiRawRecommendation = z.object({
  theme_tag: z.string().min(1).max(64),
  // accept null / undefined / empty string and normalize at the boundary
  subtitle: z.string().optional().nullable().transform((v) => v?.trim() || null),
  confidence: z.number().min(0).max(1).default(0.5),
}).passthrough(); // never reject if the model adds extra fields
 
// Layer 2: Internal IR — the invariants the rest of the app may rely on.
export const Recommendation = z.object({
  theme: z.string().min(1).max(64),
  subtitle: z.string().nullable(), // explicit null, never undefined
  confidence: z.number().min(0).max(1),
  source: z.enum(["primary", "fallback", "cached"]),
});
 
// Layer 3: Public contract — versioned, stable, what the client sees.
export const RecommendationV2 = z.object({
  schemaVersion: z.literal("v2"),
  theme: z.string(),
  subtitle: z.string().default(""), // clients are guaranteed never to see undefined
  confidence: z.number(),
});

Three things matter here. First, the input schema uses passthrough() deliberately — if Gemini sneaks in a new field, you don't want a 500 error. Second, the internal IR decides once and for all that "absent" means null, not undefined and not "". Letting both leak past the boundary is what hurt me. Third, the public contract gets an explicit schemaVersion literal so the client can pick a parser instead of guessing.

Normalizing at the boundary is the single change that paid off most for me. Once you settle the shape at the edge, everything inside becomes far easier to live with.

✦

Thank you for reading this far.

Continue Reading

What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.

WHAT YOU'LL LEARN
✦Two-layer Zod schema design (Gemini-facing input, internal IR, public contract) with full Cloudflare Workers source
✦Day-by-day log of a 2-week Dual-Emit migration, including the rollout cutoff threshold and how I picked it
✦Python compatibility checker that joins sampled response logs with Swift/Kotlin client code to flag breaking changes before they ship
Secure payment via Stripe · Cancel anytime
✦

Unlock This Article

Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.

or
Unlock all articles with Membership →
Share

Thank You for Reading

Gemini Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • ✦Copy-paste ready implementation code
  • ✦New advanced guides published daily
  • ✦$5/mo or $10 for lifetime access
View Membership →

Related Articles

◈ API / SDK2026-08-22
Once You Pass Twenty Mediation Groups, How Do You Find the Setting That Went Missing?
As ad mediation groups multiply, missing sources and type drift accumulate quietly. Here is the split I settled on: normalize the settings into one matrix, let code confirm the gaps, and send Gemini only the cells that need judgment.
◈ API / SDK2026-07-15
Sample what you already accepted — an audit budget that catches silent quality drift
A confidence gate only ever looks at output the model hesitated on. Silent drift sinks into the batch that sailed through. Working from a fixed 30-minutes-a-day review budget, this walks through deriving detection time from the binomial, reallocating the same budget across risk strata, and catching slow decay with a cumulative monitor.
◈ API / SDK2026-07-04
When Gemini API Leaks Japanese Into Your English Output Once in a While — Field Notes on Measuring the Contamination Rate and Tightening It in Stages
You told Gemini to answer in English, and 3 out of 100 runs slip a Japanese sentence into the tail. Here is why you cannot stop that 'once in a while', and a production pattern that measures the contamination rate as an SLO and tightens it with graded recovery, with working code.