Skip to content

Gemini 3.1 pro can't return structured output with empty arrays when using web search #2395

@davidgilbertson

Description

@davidgilbertson

Environment details

  • Programming language: Python
  • OS: Windows 11
  • Language runtime version: 3.14
  • Package version: 2.0.0

Steps to reproduce

Combine these things:

  1. Structured output with a list that the model should decide is empty (e.g. errors: list[str])
  2. Web search
  3. Model: gemini-3.1-pro-preview
import json

from google import genai
from google.genai.types import GenerateContentConfig, GoogleSearch, Tool
from dotenv import load_dotenv

load_dotenv()


client = genai.Client()

response = client.models.generate_content(
    model="gemini-3.1-pro-preview",
    contents="What's the capital of Kazakhstan",
    config=GenerateContentConfig(
        tools=[Tool(google_search=GoogleSearch())],
        response_mime_type="application/json",
        response_schema={
            "type": "object",
            "properties": {
                "response": {"type": "string"},
                "errors": {"type": "array", "items": {"type": "string"}},
            },
            "required": ["response", "errors"],
        },
    ),
)


print("Raw response:")
print(response.parts[-1].text)

output = json.loads(response.parts[-1].text)
print(output)

The raw text response will be something like this:

{"response":"The capital of Kazakhstan is Astana.","errors":

The bad responses always end on a colon, as though the [] that it was about to output causes it to end prematurely.

If you remove the web search tool it will return something like this (correct):

{"response":"The capital of Kazakhstan is Astana.","errors":[]}

If you switch the model to gemini-3.1-flash-lite-preview it returns something like this (correct):

{
  "response": "The capital of Kazakhstan is Astana.",
  "errors": []
}

I'm using a JSON schema here for a minimal reproduction but it also fails using a Pydantic model's model_json_schema() method.

Metadata

Metadata

Labels

priority: p2Moderately-important priority. Fix may not be included in next release.status:awaiting user responsetype: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions