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