●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
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.
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.tsimport { 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.
Dual-Emit — return v1 and v2 from the same response
You cannot wait for 100% of installed clients to update before flipping the schema, especially as an indie developer. Plenty of people don't open an app for weeks, so a meaningful fraction of users will still be on the old build a month later. So during migration, the server returns both v1 and v2 in the same response body.
// workers/recommend.tsimport { RecommendationV2 } from "../schemas/recommendation";export async function handleRecommend(req: Request, env: Env) { const raw = await callGeminiAPI(env, prompt); const ir = await normalizeToIR(raw); // input -> internal IR const v1Payload = toV1Shape(ir); const v2Payload = RecommendationV2.parse({ schemaVersion: "v2", theme: ir.theme, subtitle: ir.subtitle ?? "", confidence: ir.confidence, }); return Response.json({ // old clients keep reading the top-level fields ...v1Payload, // new clients read the v2 key v2: v2Payload, _meta: { generated_at: new Date().toISOString(), ir_source: ir.source, }, });}
New clients prefer the v2 key when it exists; old clients don't know about it and ignore the extra payload. They run side by side for a defined window.
I chose two weeks, after checking in my own version distribution that traffic from old builds shrinks to a small remainder by Day 14 once an update prompt is shown. As a rule of thumb I use "Day 7 is observation, Day 14 is the safe cutoff."
Workers implementation and the observability fix
The trap in implementing Dual-Emit is forgetting to log all three layers — raw, IR, and public output. That was the original incident's root cause for me. The fix was sampled logging to Cloudflare R2: 1% of requests get their raw response, IR, v1 output, and v2 output written as JSON Lines.
async function logSample(env: Env, payload: SamplePayload) { // 1% sampling — enough to debug, cheap enough to keep if (Math.random() > 0.01) return; const date = new Date().toISOString().slice(0, 10); const key = `gemini-samples/${date}/${crypto.randomUUID()}.jsonl`; const line = JSON.stringify({ ts: Date.now(), schema_version: "v2", gemini_raw: payload.raw, ir: payload.ir, v1_output: payload.v1, v2_output: payload.v2, // critical: always include app version and locale app_version: payload.appVersion, locale: payload.locale, }); await env.SAMPLES.put(key, line, { httpMetadata: { contentType: "application/x-ndjson" }, });}
Even with 30-day retention, the stored volume is tiny and the cost is something I rarely think about. Compared with living through another incident blind, it is an easy trade.
Sampling rate, in practice
Day 1 at 1%, then drop to 0.1% once stable. For my apps (tens of thousands of requests per day per app), 0.1% still produces dozens of samples a day — plenty for production debugging.
Always log app_version and locale
I strongly recommend this. Without it, you cannot distinguish "the Japanese-locale build of app X" from "everything globally." I lost half a day during the original incident because my samples didn't carry these fields, and I couldn't tell where the breakage was concentrated.
Compatibility checker — find breaking changes before you ship
Before shipping a new schema, I want to know what fields my client code actually depends on, and what values are currently flowing through. This Python script joins the two:
# scripts/schema_compat.pyimport jsonimport refrom pathlib import Pathfrom collections import Counter# 1. Extract field references from client code (Swift + Kotlin)FIELD_RE = re.compile(r"\.recommendation\.(\w+)|response\[['\"](\w+)['\"]\]")def extract_field_usage(app_dirs: list[Path]) -> Counter: usage = Counter() for d in app_dirs: for ext in ("*.swift", "*.kt"): for f in d.rglob(ext): text = f.read_text(encoding="utf-8", errors="ignore") for m in FIELD_RE.finditer(text): field = m.group(1) or m.group(2) if field: usage[field] += 1 return usage# 2. Compute the actual distribution of values from sampled R2 logs.def sample_field_distribution(log_dir: Path) -> dict[str, dict]: dist = {} for f in log_dir.rglob("*.jsonl"): for line in f.read_text().splitlines(): row = json.loads(line) for k, v in (row.get("v1_output") or {}).items(): d = dist.setdefault(k, {"null": 0, "empty": 0, "value": 0}) if v is None: d["null"] += 1 elif v == "": d["empty"] += 1 else: d["value"] += 1 return dist# 3. Classify each proposed change.def classify_change(field, removed, became_required, usage, dist): used = usage.get(field, 0) nulls = dist.get(field, {}).get("null", 0) total = sum(dist.get(field, {}).values()) or 1 null_ratio = nulls / total if removed and used > 0: return f"BREAKING: field '{field}' removed but referenced {used} times" if became_required and null_ratio > 0.05: return f"BREAKING: '{field}' required but {null_ratio:.1%} of past responses were null" return "SAFE"
It's rough, but for my scale it's been enough. The first time I ran it, the subtitle field turned out to be null in a share of production responses far above the 5% threshold I had picked. Knowing "I cannot make this required without breaking a real slice of responses" before I write the migration is much cheaper than learning it the way I did the first time.
Sunset protocol — how to retire v1 without scaring anyone
Once Dual-Emit has been live for two weeks, you have to make a call. My rule:
Day 0 — Ship Dual-Emit. Submit the client update that reads v2 to the stores.
Day 7 — Add _meta.deprecated = true and _meta.sunset_at = "YYYY-MM-DD" to v1 responses.
Day 10 — Check the version distribution of clients still reading v1. Project whether 90% will be on the new version by Day 14.
Day 14 — If the projection holds, stop emitting v1. The response now contains only v2 and the meta block.
If Day 14 falls short of 90%, extend the sunset to Day 21 and trigger an in-app update banner.
I chose 90% because, when review goes smoothly, my own update-completion curves have reliably reached that point. Calibrate this number to your own update curves before you trust it.
I also recommend reading _meta.sunset_at on the client and rendering a soft "please update" dialog when the sunset date is within seven days. Five lines of code. It is not a forced upgrade, so it does not hurt the UX, but it noticeably accelerates migration in my own measurements.
Why an indie developer ever starts thinking about contracts
Honestly, I used to think schema versioning was a problem for enterprise-scale services, not solo developers. While an app is something you write and use yourself, the JSON Gemini returns is just "something that parses," and "contract" feels like an unnecessary word.
What changed my mind was running the same feature long enough that other devices and older builds started depending on its responses. Change the schema once, and a client you shipped weeks ago crashes quietly. The radius of "I'll just fix it myself" had grown past what one indie developer can hold in their head.
Treating a schema as a contract is not about getting one response right. It is about keeping a promise — to users you will never meet, and to the code you yourself shipped in the past — that you will not break it.
Why schemas always change
So far we have looked at surviving a single breaking change. Let me switch angles: why do changes keep happening at all, and how do you make the act of changing safe and repeatable?
It is essentially impossible to ship a schema that survives a year unchanged. The reasons are predictable:
User expectations shift — "category was enough; now we need a sub-category"
Model capability shifts — a new Gemini version can return a richer shape
Downstream needs shift — DB indexes, frontend display
You spot the original mistake too late — tags: string should have been tags: string[]
These are all things you only notice once 10,000+ records have flowed through production. The lesson: do not aim for "never change." Aim for "change without breaking anything."
Three layers — Gemini schema, stored schema, domain model
Earlier I described the "contract" as two layers — the input IR and the public surface. For something you run for a long time, adding a stored layer in between gives you three, and it keeps the blast radius of any change confined to a single layer. Each layer changes on its own cadence, and a change in one does not ripple into the others.
[Gemini schema] → JSON Schema sent to the API
↓ parse
[Stored schema (versioned)] → type that lands in Postgres / Firestore / KV
↓ map
[Domain model] → type the application code reads
Splitting them this way keeps each kind of change from cascading:
Change only the Gemini schema → existing stored data stays as-is; new records use the new schema
Change only the stored schema → migration is needed, but Gemini and the app code stay still
Change only the domain model → application-level concern, storage untouched
The single most important rule: every stored record carries schemaVersion. Without it, code cannot tell which shape it is reading. Records I stored without version tags have cost me, in retrospect, far more time to read back than every other migration cost combined.
Four kinds of change, four levels of difficulty
Schema changes fall into four buckets that demand very different procedures.
1. Add a field — easy
Add a non-required field. Mark it optional in both the Gemini schema and the stored schema. Old data simply has the field as null. Risk is low; running V1 and V2 in parallel is unnecessary.
2. Change a type — moderate
Going from tags: string to tags: string[] is a real version bump.
Existing data is converted by a background job in stages. The non-negotiable: keep both V1 and V2 readable during the transition. A "convert everything in one go" plan loses every record on the day of failure.
3. Rename a field — handle with care
Renaming title to headline is a four-phase procedure: add the new field, write both, switch reads to the new field, retire the old. This is the pattern usually called Expand-Contract, or parallel change.
// Phase 1: write bothfunction write(record: { headline?: string; title?: string }) { const value = record.headline ?? record.title; return { headline: value, title: value }; // keep for old readers}// Phase 2: stop reading the old field// Phase 3: stop writing the old field// Phase 4: drop the old field from the schema
Each phase needs at least a week of soak time. Production exposes the readers you forgot about; the soak window is your only chance to discover them. New Gemini requests can return headline from Phase 1, but the storage layer still carries both fields for the duration.
4. Retire a field — hardest
Removing a field demands the most caution. Are you really sure no downstream consumer reads it? My standing rule is at least three months of deprecation announcement before deletion. I layer three signals: a JSDoc deprecation note in code, _meta.deprecated in the response, and an in-app notice.
How to communicate version to Gemini
Gemini accepts a fresh schema per request, so versioning is a client-side responsibility. I instrument every call with the schema version so the migration can be observed in production.
async function classify(text: string, version: 1 | 2 = 2): Promise<Classification> { const schema = version === 2 ? GeminiSchemaV2 : GeminiSchemaV1; const response = await ai.models.generateContent({ model: "gemini-3.8-flash", // as of Nov 2026; the 2.5 family is shut down contents: text, config: { responseMimeType: "application/json", responseSchema: schema, }, }); const parsed = JSON.parse(response.text!); const stored = saveWithVersion(parsed, version); await logUsage({ schemaVersion: version, inputTokens: response.usageMetadata?.promptTokenCount, outputTokens: response.usageMetadata?.candidatesTokenCount, }); return toDomain(stored);}
Knowing which version is producing what share of traffic lets you visualize migration progress. Once a week I aggregate the R2 samples and look at the V1 / V2 ratio. When the ratio plateaus unexpectedly, that is the first signal of a code path still creating V1 records.
Phased upgrade of legacy data
Past data should be converted in stages, never in one go. The five-step pattern I use:
Filter records where schemaVersion = 1
Run a small batch (100 records), measure error rate
If errors are below 1%, ramp the batch size up
Failed records go to a separate queue; fix the converter and replay
24 hours after rewriting to V2, re-verify the old field's consistency
Start at 100, watch error rate and runtime, then ramp 1,000 / 5,000 / 10,000. Never discard failed records — push them to a side queue so you can diagnose later why they did not convert. Some of the most useful schema observations come out of this queue.
A pitfall: Gemini "remembers" old enums
A subtler issue I noticed in long-running services: when you narrow an enum in V2 (say, splitting tech into tech-news and tech-tutorial), the model occasionally returns a mix of old and new values, especially, in my setup, when prompt examples or history still contain the old names. Old categories quietly slip back in.
The fix that worked: never narrow an enum in place. Always widen — keep all old values, add new ones — and migrate downstream consumers to ignore the old ones explicitly. Widening is safe; narrowing fights the model's prior. Treating enums as append-only datasets removed an entire class of mysterious validation failures.
Choosing how the schema reaches Gemini — responseSchema, responseJsonSchema, or the OpenAI compatibility layer
No amount of Dual-Emit or Sunset discipline helps if you are vague about how the schema actually reaches Gemini. My own detour came during a stretch when I had existing OpenAI SDK code pointed at Gemini with only the endpoint changed. JSON came back, but the fields I had marked optional did not behave the way they did through the Gemini SDK, and I spent a whole night unsure which side to treat as correct.
As of November 2026 there are three main routes, all documented on the official Structured outputs and OpenAI compatibility pages.
Route
How the schema is sent
Good fit
Watch out for
responseSchema (Gemini SDK)
An OpenAPI-style subset in config.responseSchema
Existing code already written this way
A narrower slice of JSON Schema; no $ref
responseJsonSchema (Gemini SDK)
JSON Schema in config.responseJsonSchema
New code, or schemas generated from Zod
Check supported keywords against the official list
OpenAI compatibility layer
json_schema inside response_format
Trying Gemini without rewriting OpenAI SDK code
Differences in optional fields, field order, and nesting have been reported on the Google AI Developers Forum
New production code goes through responseJsonSchema in the Gemini SDK; the compatibility layer is scaffolding for the migration window. That is where I draw the line today.
When the schema you send and the schema you validate against are written separately, sooner or later you fix one and forget the other. The code below builds both from a single Zod definition.
// schemas/to-gemini.ts// Zod 4's z.toJSONSchema() produces the outgoing schema from the same definition we validate withimport { z } from "zod";import { GoogleGenAI } from "@google/genai";export const RecommendationOut = z.object({ theme_tag: z.string().min(1).max(64), subtitle: z.string().nullable(), // nullable, not optional: "no value" has exactly one shape confidence: z.number().min(0).max(1),});const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });export async function recommend(prompt: string) { const res = await ai.models.generateContent({ model: "gemini-3.8-flash", contents: prompt, config: { responseMimeType: "application/json", responseJsonSchema: z.toJSONSchema(RecommendationOut), }, }); // Validate with the same definition we sent. Failures go to the sample log, not into the IR. return RecommendationOut.safeParse(JSON.parse(res.text ?? "{}"));}
subtitle is .nullable() rather than .optional() so the opening incident cannot repeat: the model is never allowed to omit the key, and "no value" always means null. Note that z.toJSONSchema() also emits keys such as $schema. What gets accepted can vary by model and version, so send it once for real and confirm you don't get a 400 before shipping.
If you want to keep existing OpenAI SDK code while you test, swap only the base URL and the model name.
// Keep the OpenAI SDK code; point it at Geminiimport OpenAI from "openai";import { z } from "zod";import { RecommendationOut } from "./schemas/to-gemini";const client = new OpenAI({ apiKey: process.env.GEMINI_API_KEY, baseURL: "https://generativelanguage.googleapis.com/v1beta/openai/",});export async function recommendViaCompat(prompt: string) { const completion = await client.chat.completions.create({ model: "gemini-3.8-flash", messages: [{ role: "user", content: prompt }], response_format: { type: "json_schema", json_schema: { name: "recommendation", schema: z.toJSONSchema(RecommendationOut) }, }, }); const text = completion.choices[0]?.message?.content ?? "{}"; return RecommendationOut.safeParse(JSON.parse(text));}
JSON that comes back through the compatibility layer is validated with exactly the same Zod definition. Whatever the route, the yardstick stays fixed — that turned out to be the whole answer to my night of not knowing which side was right.
Triage table: symptoms after a schema change
When something breaks the morning after a schema change, I map the symptom onto this table first. The point is not to bet on a single cause, but to work down the "how to confirm" column one row at a time.
Symptom
Likely causes
How to confirm
Fix
Client receives undefined and fails validation
Optional field returned as missing key, empty string, or null, mixed
Count presence and value types for that key in gemini_raw samples
Normalize to null at the entrance; always fill a default at the public layer
A string outside the enum comes back
Enum was narrowed, but old values remain in prompt examples
Search the system instruction and prompt examples for the old values
Widen enums instead of narrowing; remap old values in the domain layer
400 error: schema rejected
Keywords responseSchema cannot express, or tools specified alongside it
Match the field named in the error against the schema you sent
Move to responseJsonSchema, or flatten the schema
Shape differs only via the OpenAI compatibility layer
Differences in how the layer handles optional fields or ordering
Send the same prompt through the Gemini SDK and compare outputs side by side
Move production traffic to the Gemini SDK; keep the compat route as validated scaffolding
Only old app builds keep crashing
Dual-Emit stopped too early, or an old key was removed first
Split samples by app_version and measure who still reads v1
Extend the sunset; restore the old key under Dual-Emit
The row that cost me the most time was the fourth. I rewrote my own schema three times before suspecting the compatibility layer, and only put the two outputs side by side after midnight.
Common misconceptions
"If I specify a schema, the values are guaranteed too." Structured output gives you the shape of the JSON, not the meaning of the values. The official Structured outputs page also recommends validating what you receive in application code. That is why this design puts Zod at three points: entrance, internal IR, and exit.
"Marking a field optional keeps it backward compatible." As the opening incident shows, optional does not decide whether you get a missing key, null, or an empty string. Compatibility comes from normalizing at the entrance, not from the optional flag.
"The OpenAI compatibility layer behaves exactly like the Gemini SDK." The layer exists to make migration easier. Several threads on the Google AI Developers Forum report differences with optional fields, field order, and nested schemas. If I use it as a production path at all, it is only after comparing its output with the Gemini SDK's.
The next wall — the day you change the model ID
Once schema versioning is running smoothly, the next thing you hit is changing the model itself. The 2.5 family has already been shut down, and the day you rewrite a model ID will come again. When it does, running shadow traffic for a model migration — sending the same input to old and new models and comparing outputs — fits neatly alongside Dual-Emit.
If you have no schema change in mind right now, the single highest-leverage habit to adopt is adding schemaVersion to every stored record. That alone makes future-you ten times more efficient when the inevitable change request arrives.
interface Stored { schemaVersion: number; // ... other fields}
Designing schemas to grow, not to be permanent, is the quiet investment that turns a Gemini-powered feature from "throwaway prototype" into "system that survives." It is not glamorous. It is the lever that separates code I will be glad I wrote in two years from code I will be cleaning up in two years.
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.