|
| 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 | +from __future__ import annotations |
| 16 | + |
| 17 | +from typing import Any |
| 18 | +from typing import Dict |
| 19 | + |
| 20 | +from google.adk.tools import _automatic_function_calling_util |
| 21 | +from google.adk.utils.variant_utils import GoogleLLMVariant |
| 22 | +from google.genai import types |
| 23 | + |
| 24 | + |
| 25 | +def test_string_annotation_none_return_vertex(): |
| 26 | + """Test function with string annotation 'None' return for VERTEX_AI.""" |
| 27 | + |
| 28 | + def test_function(_param: str) -> None: |
| 29 | + """A test function that returns None with string annotation.""" |
| 30 | + pass |
| 31 | + |
| 32 | + declaration = _automatic_function_calling_util.from_function_with_options( |
| 33 | + test_function, GoogleLLMVariant.VERTEX_AI |
| 34 | + ) |
| 35 | + |
| 36 | + assert declaration.name == 'test_function' |
| 37 | + assert declaration.parameters.type == 'OBJECT' |
| 38 | + assert declaration.parameters.properties['_param'].type == 'STRING' |
| 39 | + # VERTEX_AI should have response schema for None return (stored as string) |
| 40 | + assert declaration.response is not None |
| 41 | + assert declaration.response.type == types.Type.NULL |
| 42 | + |
| 43 | + |
| 44 | +def test_string_annotation_none_return_gemini(): |
| 45 | + """Test function with string annotation 'None' return for GEMINI_API.""" |
| 46 | + |
| 47 | + def test_function(_param: str) -> None: |
| 48 | + """A test function that returns None with string annotation.""" |
| 49 | + pass |
| 50 | + |
| 51 | + declaration = _automatic_function_calling_util.from_function_with_options( |
| 52 | + test_function, GoogleLLMVariant.GEMINI_API |
| 53 | + ) |
| 54 | + |
| 55 | + assert declaration.name == 'test_function' |
| 56 | + assert declaration.parameters.type == 'OBJECT' |
| 57 | + assert declaration.parameters.properties['_param'].type == 'STRING' |
| 58 | + # GEMINI_API should not have response schema |
| 59 | + assert declaration.response is None |
| 60 | + |
| 61 | + |
| 62 | +def test_string_annotation_str_return_vertex(): |
| 63 | + """Test function with string annotation 'str' return for VERTEX_AI.""" |
| 64 | + |
| 65 | + def test_function(_param: str) -> str: |
| 66 | + """A test function that returns a string with string annotation.""" |
| 67 | + return _param |
| 68 | + |
| 69 | + declaration = _automatic_function_calling_util.from_function_with_options( |
| 70 | + test_function, GoogleLLMVariant.VERTEX_AI |
| 71 | + ) |
| 72 | + |
| 73 | + assert declaration.name == 'test_function' |
| 74 | + assert declaration.parameters.type == 'OBJECT' |
| 75 | + assert declaration.parameters.properties['_param'].type == 'STRING' |
| 76 | + # VERTEX_AI should have response schema for string return (stored as string) |
| 77 | + assert declaration.response is not None |
| 78 | + assert declaration.response.type == types.Type.STRING |
| 79 | + |
| 80 | + |
| 81 | +def test_string_annotation_int_return_vertex(): |
| 82 | + """Test function with string annotation 'int' return for VERTEX_AI.""" |
| 83 | + |
| 84 | + def test_function(_param: str) -> int: |
| 85 | + """A test function that returns an int with string annotation.""" |
| 86 | + return 42 |
| 87 | + |
| 88 | + declaration = _automatic_function_calling_util.from_function_with_options( |
| 89 | + test_function, GoogleLLMVariant.VERTEX_AI |
| 90 | + ) |
| 91 | + |
| 92 | + assert declaration.name == 'test_function' |
| 93 | + assert declaration.parameters.type == 'OBJECT' |
| 94 | + assert declaration.parameters.properties['_param'].type == 'STRING' |
| 95 | + # VERTEX_AI should have response schema for int return (stored as string) |
| 96 | + assert declaration.response is not None |
| 97 | + assert declaration.response.type == types.Type.INTEGER |
| 98 | + |
| 99 | + |
| 100 | +def test_string_annotation_dict_return_vertex(): |
| 101 | + """Test function with string annotation Dict return for VERTEX_AI.""" |
| 102 | + |
| 103 | + def test_function(_param: str) -> Dict[str, str]: |
| 104 | + """A test function that returns a dict with string annotation.""" |
| 105 | + return {'result': _param} |
| 106 | + |
| 107 | + declaration = _automatic_function_calling_util.from_function_with_options( |
| 108 | + test_function, GoogleLLMVariant.VERTEX_AI |
| 109 | + ) |
| 110 | + |
| 111 | + assert declaration.name == 'test_function' |
| 112 | + assert declaration.parameters.type == 'OBJECT' |
| 113 | + assert declaration.parameters.properties['_param'].type == 'STRING' |
| 114 | + # VERTEX_AI should have response schema for dict return (stored as string) |
| 115 | + assert declaration.response is not None |
| 116 | + assert declaration.response.type == types.Type.OBJECT |
| 117 | + |
| 118 | + |
| 119 | +def test_string_annotation_any_return_vertex(): |
| 120 | + """Test function with string annotation 'Any' return for VERTEX_AI.""" |
| 121 | + |
| 122 | + def test_function(_param: Any) -> Any: |
| 123 | + """A test function that uses Any type with string annotations.""" |
| 124 | + return _param |
| 125 | + |
| 126 | + declaration = _automatic_function_calling_util.from_function_with_options( |
| 127 | + test_function, GoogleLLMVariant.VERTEX_AI |
| 128 | + ) |
| 129 | + |
| 130 | + assert declaration.name == 'test_function' |
| 131 | + assert declaration.parameters.type == 'OBJECT' |
| 132 | + # Any type should map to None in schema (TYPE_UNSPECIFIED behavior) |
| 133 | + assert declaration.parameters.properties['_param'].type is None |
| 134 | + # VERTEX_AI should have response schema for Any return (stored as string) |
| 135 | + assert declaration.response is not None |
| 136 | + assert declaration.response.type is None # Any type maps to None in schema |
| 137 | + |
| 138 | + |
| 139 | +def test_string_annotation_mixed_parameters_vertex(): |
| 140 | + """Test function with mixed string annotations for parameters.""" |
| 141 | + |
| 142 | + def test_function(str_param: str, int_param: int, any_param: Any) -> str: |
| 143 | + """A test function with mixed parameter types as string annotations.""" |
| 144 | + return f'{str_param}-{int_param}-{any_param}' |
| 145 | + |
| 146 | + declaration = _automatic_function_calling_util.from_function_with_options( |
| 147 | + test_function, GoogleLLMVariant.VERTEX_AI |
| 148 | + ) |
| 149 | + |
| 150 | + assert declaration.name == 'test_function' |
| 151 | + assert declaration.parameters.type == 'OBJECT' |
| 152 | + assert declaration.parameters.properties['str_param'].type == 'STRING' |
| 153 | + assert declaration.parameters.properties['int_param'].type == 'INTEGER' |
| 154 | + assert declaration.parameters.properties['any_param'].type is None # Any type |
| 155 | + # VERTEX_AI should have response schema for string return (stored as string) |
| 156 | + assert declaration.response is not None |
| 157 | + assert declaration.response.type == types.Type.STRING |
| 158 | + |
| 159 | + |
| 160 | +def test_string_annotation_no_params_vertex(): |
| 161 | + """Test function with no parameters but string annotation return.""" |
| 162 | + |
| 163 | + def test_function() -> str: |
| 164 | + """A test function with no parameters that returns string (string annotation).""" |
| 165 | + return 'hello' |
| 166 | + |
| 167 | + declaration = _automatic_function_calling_util.from_function_with_options( |
| 168 | + test_function, GoogleLLMVariant.VERTEX_AI |
| 169 | + ) |
| 170 | + |
| 171 | + assert declaration.name == 'test_function' |
| 172 | + # No parameters should result in no parameters field or empty parameters |
| 173 | + assert ( |
| 174 | + declaration.parameters is None |
| 175 | + or len(declaration.parameters.properties) == 0 |
| 176 | + ) |
| 177 | + # VERTEX_AI should have response schema for string return (stored as string) |
| 178 | + assert declaration.response is not None |
| 179 | + assert declaration.response.type == types.Type.STRING |
0 commit comments