|
| 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