Skip to content

Commit b22eabd

Browse files
committed
fix: filter thought parts client-side when include_thoughts=False on Vertex AI
On Vertex AI, ThinkingConfig(include_thoughts=False) does not suppress thought parts from the API response for image generation models like gemini-3.1-flash-image-preview. The part.thought flag is correctly set to True on thought parts by the API, so client-side filtering is safe and reliable. This change adds _filter_thought_parts(), called in all four response paths (sync/async, unary/streaming), which removes parts where part.thought is True when include_thoughts is explicitly False. The filter is a strict opt-in: it only activates when include_thoughts is set to False, leaving all other configurations unchanged. Fixes: googleapis#2239
1 parent a39c679 commit b22eabd

2 files changed

Lines changed: 181 additions & 4 deletions

File tree

‎google/genai/models.py‎

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,44 @@
3737
logger = logging.getLogger('google_genai.models')
3838

3939

40+
def _filter_thought_parts(
41+
return_value: 'types.GenerateContentResponse',
42+
parameter_model: 'types._GenerateContentParameters',
43+
) -> 'types.GenerateContentResponse':
44+
"""Filters thought parts from response when include_thoughts is False.
45+
46+
When the Vertex AI API returns thought parts despite include_thoughts=False
47+
being set in ThinkingConfig, this function performs client-side filtering
48+
to suppress them. The part.thought flag is reliably set by the API, so
49+
filtering on it is safe.
50+
51+
Args:
52+
return_value: The GenerateContentResponse to filter.
53+
parameter_model: The request parameters, used to read ThinkingConfig.
54+
55+
Returns:
56+
The response with thought parts removed if include_thoughts=False,
57+
otherwise the response unchanged.
58+
"""
59+
config = parameter_model.config
60+
if config is None:
61+
return return_value
62+
thinking_config = getattr(config, 'thinking_config', None)
63+
if thinking_config is None:
64+
return return_value
65+
include_thoughts = getattr(thinking_config, 'include_thoughts', None)
66+
if include_thoughts is not False:
67+
return return_value
68+
if not return_value.candidates:
69+
return return_value
70+
for candidate in return_value.candidates:
71+
if candidate.content and candidate.content.parts:
72+
candidate.content.parts = [
73+
part for part in candidate.content.parts if not part.thought
74+
]
75+
return return_value
76+
77+
4078
def _PersonGeneration_to_mldev_enum_validate(enum_value: Any) -> None:
4179
if enum_value in set(['ALLOW_ALL']):
4280
raise ValueError(f'{enum_value} enum value is not supported in Gemini API.')
@@ -4710,7 +4748,7 @@ def _generate_content(
47104748
headers=response.headers
47114749
)
47124750
self._api_client._verify_response(return_value)
4713-
return return_value
4751+
return _filter_thought_parts(return_value, parameter_model)
47144752

47154753
def _generate_content_stream(
47164754
self,
@@ -4796,7 +4834,7 @@ def _generate_content_stream(
47964834
headers=response.headers
47974835
)
47984836
self._api_client._verify_response(return_value)
4799-
yield return_value
4837+
yield _filter_thought_parts(return_value, parameter_model)
48004838

48014839
def _embed_content(
48024840
self,
@@ -6669,7 +6707,7 @@ async def _generate_content(
66696707
headers=response.headers
66706708
)
66716709
self._api_client._verify_response(return_value)
6672-
return return_value
6710+
return _filter_thought_parts(return_value, parameter_model)
66736711

66746712
async def _generate_content_stream(
66756713
self,
@@ -6758,7 +6796,7 @@ async def async_generator(): # type: ignore[no-untyped-def]
67586796
headers=response.headers
67596797
)
67606798
self._api_client._verify_response(return_value)
6761-
yield return_value
6799+
yield _filter_thought_parts(return_value, parameter_model)
67626800

67636801
return async_generator() # type: ignore[no-untyped-call, no-any-return]
67646802

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
"""Unit tests for _filter_thought_parts.
17+
18+
Verifies client-side filtering of thought parts when include_thoughts=False
19+
is set in ThinkingConfig. Regression test for:
20+
https://github.com/googleapis/python-genai/issues/2239
21+
"""
22+
23+
import pytest
24+
25+
from ... import _transformers as t
26+
from ... import types
27+
from ...models import _filter_thought_parts
28+
29+
30+
def _make_response_with_thoughts() -> types.GenerateContentResponse:
31+
"""Build a synthetic response resembling Vertex AI image gen with thoughts."""
32+
parts = [
33+
types.Part(text='thinking step 1', thought=True),
34+
types.Part(text='thinking step 2', thought=True),
35+
types.Part(inline_data=types.Blob(mime_type='image/png', data=b'draft'), thought=True),
36+
types.Part(text='self-critique', thought=True),
37+
types.Part(inline_data=types.Blob(mime_type='image/png', data=b'final')),
38+
]
39+
content = types.Content(role='model', parts=parts)
40+
candidate = types.Candidate(content=content)
41+
return types.GenerateContentResponse(candidates=[candidate])
42+
43+
44+
def _make_parameter_model(include_thoughts) -> types._GenerateContentParameters:
45+
return types._GenerateContentParameters(
46+
model='gemini-3.1-flash-image-preview',
47+
contents=t.t_contents('Draw a red car'),
48+
config=types.GenerateContentConfig(
49+
thinking_config=types.ThinkingConfig(include_thoughts=include_thoughts)
50+
),
51+
)
52+
53+
54+
class TestFilterThoughtParts:
55+
56+
def test_include_thoughts_false_removes_thought_parts(self):
57+
"""When include_thoughts=False, all parts with thought=True are removed."""
58+
response = _make_response_with_thoughts()
59+
parameter_model = _make_parameter_model(include_thoughts=False)
60+
61+
result = _filter_thought_parts(response, parameter_model)
62+
63+
parts = result.candidates[0].content.parts
64+
assert all(not part.thought for part in parts), (
65+
'Expected no thought parts but found some'
66+
)
67+
assert len(parts) == 1, f'Expected 1 non-thought part, got {len(parts)}'
68+
assert parts[0].inline_data is not None
69+
assert parts[0].inline_data.data == b'final'
70+
71+
def test_include_thoughts_true_preserves_all_parts(self):
72+
"""When include_thoughts=True, no parts are filtered."""
73+
response = _make_response_with_thoughts()
74+
parameter_model = _make_parameter_model(include_thoughts=True)
75+
76+
result = _filter_thought_parts(response, parameter_model)
77+
78+
parts = result.candidates[0].content.parts
79+
assert len(parts) == 5, f'Expected 5 parts, got {len(parts)}'
80+
81+
def test_include_thoughts_none_preserves_all_parts(self):
82+
"""When include_thoughts is unset, no parts are filtered."""
83+
response = _make_response_with_thoughts()
84+
parameter_model = _make_parameter_model(include_thoughts=None)
85+
86+
result = _filter_thought_parts(response, parameter_model)
87+
88+
parts = result.candidates[0].content.parts
89+
assert len(parts) == 5
90+
91+
def test_no_thinking_config_preserves_all_parts(self):
92+
"""When ThinkingConfig is absent entirely, no parts are filtered."""
93+
response = _make_response_with_thoughts()
94+
parameter_model = types._GenerateContentParameters(
95+
model='gemini-3.1-flash-image-preview',
96+
contents=t.t_contents('Draw a red car'),
97+
config=types.GenerateContentConfig(),
98+
)
99+
100+
result = _filter_thought_parts(response, parameter_model)
101+
102+
parts = result.candidates[0].content.parts
103+
assert len(parts) == 5
104+
105+
def test_no_config_preserves_all_parts(self):
106+
"""When config is None entirely, no parts are filtered."""
107+
response = _make_response_with_thoughts()
108+
parameter_model = types._GenerateContentParameters(
109+
model='gemini-3.1-flash-image-preview',
110+
contents=t.t_contents('Draw a red car'),
111+
)
112+
113+
result = _filter_thought_parts(response, parameter_model)
114+
115+
parts = result.candidates[0].content.parts
116+
assert len(parts) == 5
117+
118+
def test_empty_candidates_is_safe(self):
119+
"""Response with no candidates does not raise."""
120+
response = types.GenerateContentResponse(candidates=[])
121+
parameter_model = _make_parameter_model(include_thoughts=False)
122+
123+
result = _filter_thought_parts(response, parameter_model)
124+
125+
assert result.candidates == []
126+
127+
def test_no_thought_parts_in_response(self):
128+
"""If API returns no thought parts, filtering is a no-op."""
129+
parts = [
130+
types.Part(inline_data=types.Blob(mime_type='image/png', data=b'final')),
131+
]
132+
content = types.Content(role='model', parts=parts)
133+
candidate = types.Candidate(content=content)
134+
response = types.GenerateContentResponse(candidates=[candidate])
135+
parameter_model = _make_parameter_model(include_thoughts=False)
136+
137+
result = _filter_thought_parts(response, parameter_model)
138+
139+
assert len(result.candidates[0].content.parts) == 1

0 commit comments

Comments
 (0)