Environment details
- Programming language: Python
- OS: Windows 11
- Language runtime version: 3.14
- Package version: 2.0.0
Steps to reproduce
Combine these things:
- Structured output with a list that the model should decide is empty (e.g.
errors: list[str])
- Web search
- 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.
Environment details
Steps to reproduce
Combine these things:
errors: list[str])gemini-3.1-pro-previewThe 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-previewit 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.