googleapis / googleapis/python-genai
Gemini 3.1 pro can't return structured output with empty arrays when using web search
- Dominant language
- Python
- Stars
- 4k
- Forks
- 1k
- Avg merge
- 2d 11h
- Merged PRs (30d)
- 40
Description
#### 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`
```python
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:
```json
{"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):
```json
{"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):
```json
{
"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.
Contributor guide
Assessment
This issue has not been assessed yet.