geminigooglestructured-outputjson-schemaai

Google Gemini Structured Output: responseSchema and JSON Mode

·8 min read·AI & JSON

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

json
{
  "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:

json
{ "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

  1. Prototype the shape you want and format it, then generate a JSON Schema as your canonical definition.
  2. Validate it before pasting it into responseSchema, since Gemini rejects unsupported keywords silently-ish and you want to catch that early.
  3. 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.

Frequently asked questions

They're Gemini generationConfig fields. responseMimeType: "application/json" forces JSON output; responseSchema defines the shape it must match. Together they guarantee parseable, structured results.

A Gemini-specific hint that fixes the order the model emits object fields. Standard JSON Schema ignores key order, but Gemini can vary results based on it, so propertyOrdering improves consistency and lets you order fields for better reasoning.

It's a subset of the OpenAPI 3.0 schema object, which overlaps heavily with JSON Schema. Common keywords (type, properties, items, required, enum, nullable) work; some advanced JSON Schema features aren't supported.

Constrain the field to an enum, or use responseMimeType: "text/x.enum" to return a single enum value directly — ideal for sentiment, intent, or category labels with no post-processing.

Try JSON Schema Generator

Generate the responseSchema to constrain Gemini's output.