Skip to content

Commit be32e9e

Browse files
yyyu-googlecopybara-github
authored andcommitted
fix: fix the function declaration parser for union types. Fixes #3060
PiperOrigin-RevId: 999635503
1 parent efbe9d7 commit be32e9e

3 files changed

Lines changed: 47 additions & 11 deletions

File tree

‎google/genai/_automatic_function_calling_util.py‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -379,10 +379,6 @@ def parse_function_declaration_json_schema(
379379
root_defs.update(json_schema_dict.pop('$defs'))
380380
if 'definitions' in json_schema_dict:
381381
root_defs.update(json_schema_dict.pop('definitions'))
382-
# pydantic doesn't assign the `type` field when the schema has 'anyOf'.
383-
# but Vertex requires it.
384-
if not 'type' in json_schema_dict and 'anyOf' in json_schema_dict:
385-
json_schema_dict['type'] = 'object'
386382
if param.default is not inspect._empty:
387383
json_schema_dict['default'] = param.default
388384
parameters_properties_json_schema[name] = json_schema_dict
@@ -437,10 +433,6 @@ def parse_function_declaration_json_schema(
437433
response_json_schema = _add_unevaluated_items_to_fixed_len_tuple_schema(
438434
response_json_schema
439435
)
440-
# pydantic doesn't assign the `type` field when the schema has 'anyOf'.
441-
# but Vertex requires it.
442-
if 'type' not in response_json_schema and 'anyOf' in response_json_schema:
443-
response_json_schema['type'] = 'object'
444436
except Exception as e:
445437
_raise_for_unsupported_param(
446438
return_value, callable.__name__, e

‎google/genai/tests/models/test_generate_content_tools.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# Unless required by applicable law or agreed to in writing, software
1010
# distributed under the License is distributed on an "AS IS" BASIS,
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
# See the License for the specific language governing permissions and
12+
# See the License for the specific languate governing permissions and
1313
# limitations under the License.
1414
#
1515

@@ -1310,7 +1310,7 @@ def get_information(
13101310
return 'The animal is not supported'
13111311

13121312
response = client.models.generate_content(
1313-
model='gemini-3.5-flash',
1313+
model='gemini-4-argon',
13141314
contents=(
13151315
'I have a one year old cat named Sundae, can you get the'
13161316
' information of the cat for me?'
@@ -1426,7 +1426,7 @@ def describe_cities(
14261426
)
14271427

14281428
response = client.models.generate_content(
1429-
model='gemini-3.1-pro-preview',
1429+
model='gemini-4-argon',
14301430
contents='Can you describe the city of San Francisco, USA?',
14311431
config={
14321432
'tools': [describe_cities],

‎google/genai/tests/transformers/test_t_tool.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,50 @@ def test_func(arg1: str, arg2: int):
7676
)
7777

7878

79+
@pytest.mark.usefixtures('client')
80+
def test_function_with_optional_and_union_types(client):
81+
def test_func(
82+
query: str | int,
83+
start_line: int | None = None,
84+
) -> str | None:
85+
del query, start_line
86+
return None
87+
88+
assert t.t_tool(client, test_func) == types.Tool(
89+
function_declarations=[
90+
types.FunctionDeclaration(
91+
name='test_func',
92+
description='',
93+
parameters_json_schema={
94+
'type': 'object',
95+
'properties': {
96+
'query': {
97+
'anyOf': [
98+
{'type': 'string'},
99+
{'type': 'integer'},
100+
],
101+
},
102+
'start_line': {
103+
'anyOf': [
104+
{'type': 'integer'},
105+
{'type': 'null'},
106+
],
107+
'default': None,
108+
},
109+
},
110+
'required': ['query'],
111+
},
112+
response_json_schema={
113+
'anyOf': [
114+
{'type': 'string'},
115+
{'type': 'null'},
116+
],
117+
},
118+
)
119+
]
120+
)
121+
122+
79123
@pytest.mark.usefixtures('client')
80124
def test_dictionary(client):
81125
assert t.t_tool(

0 commit comments

Comments
 (0)