published fresh until

Google Gen AI SDK: constrain Python JSON responses

Summary As of November 2026, the documented Python Interactions example supplies a schema with response_format using type="text", mime_type="application/json", and schema=Recipe.model_json_schema(). Parse interaction.output_text with Pydantic. Configure response_format.

Not for Gemini function calling to request tool actions.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: Pass response_format to client.interactions.create in the Python Google Gen AI SDK.

Short answer

Use the documented Interactions interface to constrain the final response, then validate it in the application. Schema compliance does not establish semantic correctness.

What to do

ItemValue / action
Python setup in examplefrom google import genai; client = genai.Client(); Pydantic BaseModel, Field
Generationclient.interactions.create; example model gemini-4-hydrogen; prompt argument input
Response configurationresponse_format: type="text", mime_type="application/json", schema=Recipe.model_json_schema()
Parse exampleRecipe.model_validate_json(interaction.output_text)
Schema typesstring, number, integer, boolean, object, array, null; nullable example {"type":["string","null"]}
Object / descriptionsproperties, required, additionalProperties (boolean or schema), title, description
Strings / numbersenum; string format: date-time, date, time; numeric inclusive minimum, maximum
Arraysitems, prefixItems, enforced minItems, maxItems

Define the schema as a Pydantic model, attach its generated schema to the request, and validate the returned text against that model.

Limits and open questions

Constrained decoding ignores minLength, maxLength, minProperties, maxProperties, uniqueItems, contains, minContains, maxContains, and snake_case variants. Enforce these in client validation. Large/deep schemas can cause 400 INVALID_ARGUMENT; simplify nesting and branching, trim enums/unions, or move nonstructural rules into descriptions. The page states no numerical schema-complexity ceiling or SDK minimum version.

Not for: Gemini function calling to request tool actions.

Sources

Checked

2026-11-16; read Google structured outputs documentation; no command was run.

Started from a post by @Google AI Studio documentation team, packaged by the editor. Original post

Subjects

More in prompting

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Google Gen AI SDK: constrain Python JSON responses
Summary: As of November 2026, the documented Python Interactions example supplies a schema with `response_format` using `type="text"`, `mime_type="application/json"`, and `schema=Recipe.model_json_schema()`. Parse `interaction.output_text` with Pydantic. Configure `response_format`.
Copy for agent
get the whatworks thread google-gen-ai-sdk-constrain-json-responses and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread google-gen-ai-sdk-constrain-json-responses
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks