google-gemini

How can I configure Structured Output for model: gemini-2.5-flash-image?


In the official documentation, it says Structured Output supported:

Enter image description here (Gemini Models, Gemini 2.5 Flash)

But when making requests, it returns an HTTP 400 error:

JSON mode is not enabled for this model

Below is a 400 error example (using the prompt and JSON schema on Structured Outputs):

curl --location 'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-image:generateContent?key=<GEMINI_API_KEY>' \
--header 'Content-Type: application/json' \
--data '{
  "contents": [
    {
      "parts": [
        { "text": "List a few popular cookie recipes, and include the amounts of ingredients, then create an image of them." }
      ]
    }
  ],
    "generationConfig": {
        "responseMimeType": "application/json",
        "responseSchema": {
          "type": "ARRAY",
          "items": {
            "type": "OBJECT",
            "properties": {
              "recipeName": { "type": "STRING" },
              "ingredients": {
                "type": "ARRAY",
                "items": { "type": "STRING" }
              }
            },
            "propertyOrdering": ["recipeName", "ingredients"]
          }
        }
      }
}'

Response:

{
    "error": {
        "code": 400,
        "message": "JSON mode is not enabled for this model",
        "status": "INVALID_ARGUMENT"
    }
}

If I change the model from gemini-2.5-flash-image to gemini-2.5-flash while keeping everything else the same, the response becomes OK:

{
    "candidates": [
        {
            "content": {
                "parts": [
                    {
                        "text": "[{\"recipeName\":\"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 (12 oz) chocolate chips\"]}, {\"recipeName\":\"Sugar Cookies\",\"ingredients\":[\"1 cup (2 sticks) unsalted butter, softened\",\"1 1/2 cups granulated sugar\",\"1 large egg\",\"1 teaspoon vanilla extract\",\"2 3/4 cups all-purpose flour\",\"1 teaspoon baking powder\",\"1/2 teaspoon salt\"]}, {\"recipeName\":\"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\"]}]"
                    }
                ],
                "role": "model"
            },
            "finishReason": "STOP",
            "index": 0
        }
    ],
    "usageMetadata": {
        "promptTokenCount": 21,
        "candidatesTokenCount": 229,
        "totalTokenCount": 784,
        "promptTokensDetails": [
            {
                "modality": "TEXT",
                "tokenCount": 21
            }
        ],
        "thoughtsTokenCount": 534
    },
    "modelVersion": "gemini-2.5-flash",
    "responseId": "MSgKae3rG_Hhz7IPosfrqQ8"
}

Solution

  • I got the same error when using JSON as the response type with Nano banana (gemini-flash-image). The model doesn't seem to support a JSON response. As a workaround, here is what I tried (the idea is to use the prompt or system instruction):

    curl --location 'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-image:generateContent?key=[you_api_key' \
    --header 'Content-Type: application/json' \
    --data '{
      "contents": [
        {
          "parts": [
            { "text": "List a few popular cookie recipes, and include the amounts of ingredients, then create an image of them." }
            ,{ "text": "Please format the recipes as a valid JSON array of objects, where each object has a 'recipeName' (string) and an 'ingredients' (array of strings). Do not include any text outside of the JSON array for the recipe part. Here is an example of the desired format: [{\"recipeName\": \"Example Cookie\", \"ingredients\": [\"1 cup flour\", \"1/2 cup sugar\"]}]" }
          ]
        }
      ],
        "generationConfig": {
            "response_modalities": ["TEXT", "IMAGE"]
          }
    }'
    

    This gave me result like:

    "candidates": [
        {
          "content": {
            "parts": [
              {
                "text": "```json\n[\n  {\n    \"recipeName\": \"Classic Chocolate Chip Cookies\",\n    \"ingredients\": [\n      \"2 1/4 cups all-purpose flour\",\n      \"1 teaspoon baking soda\",\n      \"1 teaspoon salt\",\n      \"1 cup (2 sticks) unsalted butter, softened\",\n      \"3/4 cup granulated sugar\",\n      \"3/4 cup packed light brown sugar\",\n      \"1 teaspoon vanilla extract\",\n      \"2 large eggs\",\n      \"2 cups (12-ounce package) semi-sweet chocolate chips\"\n    ]\n  },\n  {\n    \"recipeName\": \"Oatmeal Raisin Cookies\",\n    \"ingredients\": [\n      \"1 1/2 cups all-purpose flour\",\n      \"1 teaspoon baking soda\",\n      \"1 teaspoon ground cinnamon\",\n      \"1/2 teaspoon salt\",\n      \"1 cup (2 sticks) unsalted butter, softened\",\n      \"1 cup packed light brown sugar\",\n      \"1/2 cup granulated sugar\",\n      \"2 large eggs\",\n      \"1 teaspoon vanilla extract\",\n      \"3 cups old-fashioned rolled oats\",\n      \"1 1/2 cups raisins\"\n    ]\n  },\n  {\n    \"recipeName\": \"Peanut Butter Cookies\",\n    \"ingredients\": [\n      \"1 cup unsalted butter, softened\",\n      \"1 cup granulated sugar\",\n      \"1 cup packed light brown sugar\",\n      \"1 cup creamy peanut butter\",\n      \"2 large eggs\",\n      \"1 teaspoon vanilla extract\",\n      \"2 1/2 cups all-purpose flour\",\n      \"1 teaspoon baking soda\",\n      \"1/2 teaspoon baking powder\",\n      \"1/2 teaspoon salt\"\n    ]\n  }\n]\n```\nHere are some popular cookie recipes for you! "
              },
              {
                "inlineData": {
                  "mimeType": "image/png",
                  "data": "encoded-image-data"
                }
              }
            ],
            "role": "model"
          },
          "finishReason": "STOP",
          "index": 0
        }
      ],
    

    Yeah, it is not a pretty workaround.