Getting guaranteed JSON out of Gemini
Like every major provider in 2026, Google Gemini can constrain its output to a schema instead of returning freeform text. You enable it with two generation-config fields: `responseMimeType: "application/json"` to force JSON, and `responseSchema` to define the exact shape. With both set, the response is guaranteed to parse and match your structure — no fence-stripping, no regex.
The request shape
{
"contents": [{ "parts": [{ "text": "List three JSON data types with a one-line description each." }] }],
"generationConfig": {
"responseMimeType": "application/json",
"responseSchema": {
"type": "object",
"properties": {
"types": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": { "type": "string" },
"description": { "type": "string" }
},
"required": ["name", "description"],
"propertyOrdering": ["name", "description"]
}
}
},
"required": ["types"]
}
}
}Gemini's responseSchema is a subset of OpenAPI 3.0 schema, so most JSON Schema you already write maps over directly: type, properties, items, required, enum, nullable.
propertyOrdering: the Gemini-specific gotcha
Gemini added propertyOrdering because JSON object key order can affect output quality and consistency. Unlike standard JSON Schema (where key order is irrelevant), specifying propertyOrdering tells Gemini the sequence to emit fields in — useful when you want the model to "reason" through fields in a deliberate order (e.g. produce evidence before conclusion). If you skip it, ordering isn't guaranteed, which can subtly change results between calls.
Enums for clean classification
For categorical fields, constrain to an enum so you never post-process a typo. You can even set responseMimeType: "text/x.enum" to return a single enum value directly for pure classification:
{ "type": "string", "enum": ["positive", "neutral", "negative"] }How it differs from OpenAI
The concept is identical — a schema constrains the output — but the details differ: OpenAI uses response_format / text.format with strict JSON Schema; Gemini uses responseSchema (OpenAPI-subset) with responseMimeType. Gemini's propertyOrdering has no OpenAI equivalent. If you support both providers, keep your canonical schema in one place and adapt it per API rather than maintaining two.
Building and checking your schema
- Prototype the shape you want and format it, then generate a JSON Schema as your canonical definition.
- Validate it before pasting it into
responseSchema, since Gemini rejects unsupported keywords silently-ish and you want to catch that early. - Generate TypeScript or Python types from the schema so your code and the model agree on the shape.
Compare provider approaches in LLM JSON Mode & Structured Outputs and OpenAI Responses API vs Chat Completions.