GrepBoard/Manuals
605 manuals ·

Lock Gemini's response to a JSON schema

·Tested on Gemini API official docs · 2026-10 · responses checked with jsonschema 4.10.3·Test scope ↓·6 steps

Send a mime_type and a JSON schema with the Gemini API request to get a fixed JSON shape back. Covers supported keywords, limits, and checking the result.

Instead of writing "answer in JSON" in the prompt, put a schema in the request so the API fixes the response shape. To shape output with the prompt alone, see Fix Gemini's answer format with system instructions and examples first.

[1] Add two values to the request: the MIME type application/json and a JSON schema. This is the Recipe Extractor example from the official docs with fewer fields; the current docs use response_format in the Interactions API (docs).

request.json
{  "model": "gemini-3.8-flash",  "input": "Please extract the recipe from the following text. ...",  "response_format": {    "type": "text",    "mime_type": "application/json",    "schema": {      "type": "object",      "properties": {        "recipe_name": { "type": "string", "description": "The name of the recipe." },        "ingredients": {          "type": "array",          "items": {            "type": "object",            "properties": { "name": { "type": "string" }, "quantity": { "type": "string" } },            "required": ["name", "quantity"]          }        }      },      "required": ["recipe_name", "ingredients"]    }  }}
console
# The model value is from the docs example. Use the model you have.$ curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \    -H "x-goog-api-key: $GEMINI_API_KEY" \    -H 'Content-Type: application/json' \    -d @request.json

[2] Read the response. The example response in the docs looks like this (excerpt).

output
{  "recipe_name": "Delicious Chocolate Chip Cookies",  "ingredients": [    { "name": "all-purpose flour", "quantity": "2 and 1/4 cups" },    ...  ],  ...}

In the Python SDK, interaction.output_text is this JSON string. The docs build the schema from a Pydantic class (Recipe.model_json_schema()) and turn the string back into an object with Recipe.model_validate_json().

[3] Existing generateContent code uses different field names. The "Before" example in the migration guide puts responseMimeType and responseSchema inside generationConfig. Its schema writes type in capitals: OBJECT, STRING.

generate-content.json
"generationConfig": {  "responseMimeType": "application/json",  "responseSchema": {    "type": "OBJECT",    "properties": {      "recipe_name": { "type": "STRING" },      "ingredients": { "type": "ARRAY", "items": { "type": "STRING" } }    },    "required": ["recipe_name", "ingredients"]  }}

[4] Check which schema keywords you can use. Only part of JSON Schema is supported.

Applies toSupported
typestring · number · integer · boolean · object · array · null
Descriptiontitle · description
objectproperties · required · additionalProperties
stringenum · format (date-time · date · time)
number · integerenum · minimum · maximum
arrayitems · prefixItems · minItems · maxItems

Narrow fixed-choice values with enum (the moderation example in the docs limits spam_type to phishing, scam, unsolicited promotion, other). To allow null, put "null" in a type array: {"type": ["string", "null"]}. A November 2025 Google blog post announces added support for anyOf, $ref and prefixItems, and says response keys now follow the key order in the schema (post).

[5] Put the task in the prompt and the field guidance in description. The docs' best practices say so: "Use the description field to guide the model" and "Clearly state what you want the model to do". The docs do not say to repeat the schema in the prompt; no such advice was found.

[6] Validate the result anyway. The docs say the output is syntactically correct JSON but the values must be validated in your application. check.py checks a response file against a schema with Draft202012Validator from jsonschema. The test schema.json is not the step 1 schema: it adds prep_time_minutes (integer or null) and instructions (array of strings), and requires recipe_name, ingredients and instructions.

schema.json
{  "type": "object",  "properties": {    "recipe_name": { "type": "string" },    "prep_time_minutes": { "type": ["integer", "null"] },    "ingredients": {      "type": "array",      "items": {        "type": "object",        "properties": { "name": { "type": "string" }, "quantity": { "type": "string" } },        "required": ["name", "quantity"]      }    },    "instructions": { "type": "array", "items": { "type": "string" } }  },  "required": ["recipe_name", "ingredients", "instructions"]}

It was run on good.json, which follows the schema, and bad.json, which breaks the types and leaves out instructions.

bad.json
{"recipe_name":"Cookies","ingredients":[{"name":"large eggs","quantity":2}],"prep_time_minutes":"10"}
console
$ python3 -I check.py schema.json good.json bad.jsongood.json OKbad.json FAIL   prep_time_minutes - '10' is not of type 'integer', 'null'   ingredients/0/quantity - 2 is not of type 'string'   (root) - 'instructions' is a required property

Check it with the JSON · YAML formatter and converter tool ↗ — calculated in your browser.

#gemini#json#structured-output