python-genai: Structured outputs is not working for gemini-2.5-pro-preview-03-25

Steps to reproduce

Just follow the example in the docs at https://ai.google.dev/gemini-api/docs/structured-output?lang=python.

If we do:

import os
from google import genai
from pydantic import BaseModel

GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
client = genai.Client(api_key=GEMINI_API_KEY)

MODEL = "gemini-2.0-flash"


class Recipe(BaseModel):
    recipe_name: str
    ingredients: list[str]


response = client.models.generate_content(
    model=MODEL,
    contents="List a few popular cookie recipes. Be sure to include the amounts of ingredients.",
    config={
        "response_mime_type": "application/json",
        "response_schema": list[Recipe],
    },
)
print(response.text)

my_recipes: list[Recipe] = response.parsed

print(my_recipes)

Everything works and we get the following output:

[
  {
    "recipe_name": "Chocolate Chip Cookies",
    "ingredients": [
      "1 cup (2 sticks) unsalted butter, softened",
      "3/4 cup granulated sugar",
      "3/4 cup packed brown sugar",
      "1 teaspoon vanilla extract",
      "2 large eggs",
      "2 1/4 cups all-purpose flour",
      "1 teaspoon baking soda",
      "1 teaspoon salt",
      "2 cups chocolate chips"
    ]
  },
  {
    "recipe_name": "Peanut Butter Cookies",
    "ingredients": [
      "1 cup (2 sticks) unsalted butter, softened",
      "1 cup granulated sugar",
      "1 cup packed brown sugar",
      "1 teaspoon vanilla extract",
      "2 large eggs",
      "1 cup peanut butter",
      "2 1/2 cups all-purpose flour",
      "1 teaspoon baking soda",
      "1/2 teaspoon salt"
    ]
  },
  {
    "recipe_name": "Sugar Cookies",
    "ingredients": [
      "1 1/2 cups (3 sticks) unsalted butter, softened",
      "2 cups granulated sugar",
      "4 large eggs",
      "1 teaspoon vanilla extract",
      "5 cups all-purpose flour",
      "2 teaspoons baking powder",
      "1 teaspoon salt"
    ]
  }
]
[Recipe(recipe_name='Chocolate Chip Cookies', ingredients=['1 cup (2 sticks) unsalted butter, softened', '3/4 cup granulated sugar', '3/4 cup packed brown sugar', '1 teaspoon vanilla extract', '2 large eggs', '2 1/4 cups all-purpose flour', '1 teaspoon baking soda', '1 teaspoon salt', '2 cups chocolate chips']), Recipe(recipe_name='Peanut Butter Cookies', ingredients=['1 cup (2 sticks) unsalted butter, softened', '1 cup granulated sugar', '1 cup packed brown sugar', '1 teaspoon vanilla extract', '2 large eggs', '1 cup peanut butter', '2 1/2 cups all-purpose flour', '1 teaspoon baking soda', '1/2 teaspoon salt']), Recipe(recipe_name='Sugar Cookies', ingredients=['1 1/2 cups (3 sticks) unsalted butter, softened', '2 cups granulated sugar', '4 large eggs', '1 teaspoon vanilla extract', '5 cups all-purpose flour', '2 teaspoons baking powder', '1 teaspoon salt'])]

If we change the model to gemini-2.5-pro-preview-03-25 or gemini-2.5-pro-exp-03-25 and keep everything else identical:

import os
from google import genai
from pydantic import BaseModel

GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
client = genai.Client(api_key=GEMINI_API_KEY)

MODEL = "gemini-2.5-pro-preview-03-25"


class Recipe(BaseModel):
    recipe_name: str
    ingredients: list[str]


response = client.models.generate_content(
    model=MODEL,
    contents="List a few popular cookie recipes. Be sure to include the amounts of ingredients.",
    config={
        "response_mime_type": "application/json",
        "response_schema": list[Recipe],
    },
)
print(response.text)

my_recipes: list[Recipe] = response.parsed

print(my_recipes)

We’ll get an output like:

ny
```json
[
  {
    "recipe_name": "Classic Chocolate Chip Cookies",
    "ingredients": [
      "1 cup (2 sticks) unsalted butter, softened",
      "3/4 cup granulated sugar",
      "3/4 cup packed light brown sugar",
      "2 large eggs",
      "1 teaspoon vanilla extract",
      "2 1/4 cups all-purpose flour",
      "1 teaspoon baking soda",
      "1/2 teaspoon salt",
      "2 cups semi-sweet chocolate chips"
    ]
  },
  {
    "recipe_name": "Simple Peanut Butter Cookies",
    "ingredients": [
      "1 cup creamy peanut butter",
      "1 cup granulated sugar",
      "1 large egg",
      "1 teaspoon vanilla extract (optional)",
      "1/2 teaspoon baking soda"
    ]
  },
  {
    "recipe_name": "Oatmeal Raisin Cookies",
    "ingredients": [
      "1 cup (2 sticks) unsalted butter, softened",
      "1 cup packed light brown sugar",
      "1/2 cup granulated sugar",
      "2 large eggs",
      "1 teaspoon vanilla extract",
      "1 1/2 cups all-purpose flour",
      "1 teaspoon baking soda",
      "1 teaspoon ground cinnamon",
      "1/2 teaspoon salt",
      "3 cups old-fashioned rolled oats",
      "1 cup raisins"
    ]
  },
  {
    "recipe_name": "Sugar Cookies",
    "ingredients": [
      "2 3/4 cups all-purpose flour",
      "1 teaspoon baking soda",
      "1/2 teaspoon salt",
      "1 cup (2 sticks) unsalted butter, softened",
      "1 1/2 cups granulated sugar",
      "1 large egg",
      "1 teaspoon vanilla extract"
    ]
  }
]
``
None

Something is broken for Gemini 2.5 structured outputs. response.text includes markdown ticks among other things, and response.parsed is None.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 11
  • Comments: 30

Most upvoted comments

@pamorgan can we please bump up the priority of this one to a P1? This impacts basically all API consumers of Google AI Studio.

Thank you for reporting. We are investigating the issue.

Fix has been rolled out to Gemini API - Please let us know if you are still seeing the issue.

@benpml we don’t typically share ETAs externally. will share once it’s done 😃

We have identified the root cause and working on a fix soon

Same thing for us:

Image

Thank you - I’m closing this now. Please let us know if you continue to see the issue.

Yes - We will continue updating the issue.

Will you update us when it’s pushed to the VertexAI API? Still seeing it with VertexAI.