The code sample for generating structured output here in the docs does not work
from google import genai
from pydantic import BaseModel, Field
from typing import List, Optional
class Ingredient(BaseModel):
name: str = Field(description="Name of the ingredient.")
quantity: str = Field(description="Quantity of the ingredient, including units.")
class Recipe(BaseModel):
recipe_name: str = Field(description="The name of the recipe.")
prep_time_minutes: Optional[int] = Field(description="Optional time in minutes to prepare the recipe.")
ingredients: List[Ingredient]
instructions: List[str]
client = genai.Client()
prompt = """..."""
response = client.models.generate_content(
model="gemini-3.5-flash",
contents=prompt,
config={
"response_format": {"text": {"mime_type": "application/json", "schema": Recipe.model_json_schema()}}, # This line causes an error.
},
)
recipe = Recipe.model_validate_json(response.text)
print(recipe)
A Pydantic error gets raised with the “schema"parameter, it seems in the latest version the format of that parameter changed. I had to use the following in my code for it to work.
response = client.models.generate_content(
model="gemini-2.5-flash",
contents=[prompt],
config={
"response_mime_type": "application/json",
"response_json_schema": CorrectnessScore.model_json_schema(),
},
)
Instead of a single “response_format" key value pair it looks like now it needs “response_mime_type” and "response_json_schema"