|
| 1 | +# Copyright 2026 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 | +"""Tests for the FunctionTool declaration cache. |
| 16 | +
|
| 17 | +`_get_declaration` is called once per tool on every LLM request, so its result |
| 18 | +is cached. Callers are allowed to mutate the declaration they receive, so each |
| 19 | +call must still get a private copy, and the cache must follow every input the |
| 20 | +declaration depends on. |
| 21 | +""" |
| 22 | + |
| 23 | +import copy |
| 24 | + |
| 25 | +from google.adk.tools import _function_tool_declarations |
| 26 | +from google.adk.tools.function_tool import FunctionTool |
| 27 | +from google.adk.tools.long_running_tool import LongRunningFunctionTool |
| 28 | +from google.adk.tools.transfer_to_agent_tool import TransferToAgentTool |
| 29 | +from google.adk.utils.variant_utils import GoogleLLMVariant |
| 30 | +import pytest |
| 31 | + |
| 32 | + |
| 33 | +def sample_tool(device_id: str, status: str = 'ON') -> str: |
| 34 | + """Sets a device status. |
| 35 | +
|
| 36 | + Args: |
| 37 | + device_id: The device. |
| 38 | + status: The desired status. |
| 39 | +
|
| 40 | + Returns: |
| 41 | + A confirmation message. |
| 42 | + """ |
| 43 | + return 'ok' |
| 44 | + |
| 45 | + |
| 46 | +def poll_job(job_id: str) -> str: |
| 47 | + """Polls a job. |
| 48 | +
|
| 49 | + Args: |
| 50 | + job_id: The job. |
| 51 | +
|
| 52 | + Returns: |
| 53 | + The job status. |
| 54 | + """ |
| 55 | + return 'PENDING' |
| 56 | + |
| 57 | + |
| 58 | +def test_repeated_calls_return_equal_declarations(): |
| 59 | + tool = FunctionTool(sample_tool) |
| 60 | + |
| 61 | + first = tool._get_declaration() |
| 62 | + second = tool._get_declaration() |
| 63 | + |
| 64 | + assert first.model_dump() == second.model_dump() |
| 65 | + |
| 66 | + |
| 67 | +def test_repeated_calls_return_distinct_objects(): |
| 68 | + tool = FunctionTool(sample_tool) |
| 69 | + |
| 70 | + first = tool._get_declaration() |
| 71 | + second = tool._get_declaration() |
| 72 | + |
| 73 | + assert first is not second |
| 74 | + assert first.parameters_json_schema is not second.parameters_json_schema |
| 75 | + |
| 76 | + |
| 77 | +def test_declaration_is_built_once_for_repeated_calls(): |
| 78 | + tool = FunctionTool(sample_tool) |
| 79 | + calls = [] |
| 80 | + original = ( |
| 81 | + _function_tool_declarations.build_function_declaration_with_json_schema |
| 82 | + ) |
| 83 | + |
| 84 | + def counting(*args, **kwargs): |
| 85 | + calls.append(1) |
| 86 | + return original(*args, **kwargs) |
| 87 | + |
| 88 | + _function_tool_declarations.build_function_declaration_with_json_schema = ( |
| 89 | + counting |
| 90 | + ) |
| 91 | + try: |
| 92 | + for _ in range(5): |
| 93 | + tool._get_declaration() |
| 94 | + finally: |
| 95 | + _function_tool_declarations.build_function_declaration_with_json_schema = ( |
| 96 | + original |
| 97 | + ) |
| 98 | + |
| 99 | + assert len(calls) == 1 |
| 100 | + |
| 101 | + |
| 102 | +def test_mutating_a_returned_declaration_does_not_affect_later_calls(): |
| 103 | + tool = FunctionTool(sample_tool) |
| 104 | + |
| 105 | + first = tool._get_declaration() |
| 106 | + first.name = 'renamed' |
| 107 | + first.description = 'mutated' |
| 108 | + first.parameters_json_schema['properties']['device_id']['enum'] = ['x'] |
| 109 | + |
| 110 | + second = tool._get_declaration() |
| 111 | + |
| 112 | + assert second.name == 'sample_tool' |
| 113 | + assert second.description != 'mutated' |
| 114 | + assert 'enum' not in second.parameters_json_schema['properties']['device_id'] |
| 115 | + |
| 116 | + |
| 117 | +def test_long_running_tool_description_is_stable_across_calls(): |
| 118 | + """LongRunningFunctionTool appends a note to the declaration it is given.""" |
| 119 | + tool = LongRunningFunctionTool(poll_job) |
| 120 | + |
| 121 | + descriptions = [tool._get_declaration().description for _ in range(3)] |
| 122 | + |
| 123 | + assert len(set(descriptions)) == 1 |
| 124 | + assert descriptions[0].count('long-running operation') == 1 |
| 125 | + |
| 126 | + |
| 127 | +def test_transfer_to_agent_tool_sets_enum_on_every_call(): |
| 128 | + """TransferToAgentTool writes an enum into the parameter schema it is given.""" |
| 129 | + tool = TransferToAgentTool(agent_names=['alpha', 'beta']) |
| 130 | + |
| 131 | + for _ in range(3): |
| 132 | + declaration = tool._get_declaration() |
| 133 | + assert declaration.parameters_json_schema['properties']['agent_name'][ |
| 134 | + 'enum' |
| 135 | + ] == ['alpha', 'beta'] |
| 136 | + |
| 137 | + |
| 138 | +def test_toolset_name_prefixing_does_not_rename_the_original_tool(): |
| 139 | + """BaseToolset prefixing writes .name onto the declaration it is given.""" |
| 140 | + tool = FunctionTool(sample_tool) |
| 141 | + prefixed = copy.copy(tool) |
| 142 | + original_get_declaration = tool._get_declaration |
| 143 | + |
| 144 | + def prefixed_declaration(): |
| 145 | + declaration = original_get_declaration() |
| 146 | + declaration.name = 'prefix_sample_tool' |
| 147 | + return declaration |
| 148 | + |
| 149 | + prefixed._get_declaration = prefixed_declaration |
| 150 | + |
| 151 | + assert prefixed._get_declaration().name == 'prefix_sample_tool' |
| 152 | + assert tool._get_declaration().name == 'sample_tool' |
| 153 | + |
| 154 | + |
| 155 | +def test_changing_ignore_params_rebuilds_the_declaration(): |
| 156 | + tool = FunctionTool(sample_tool) |
| 157 | + |
| 158 | + before = tool._get_declaration().parameters_json_schema['properties'] |
| 159 | + assert 'status' in before |
| 160 | + |
| 161 | + tool._ignore_params = list(tool._ignore_params) + ['status'] |
| 162 | + after = tool._get_declaration().parameters_json_schema['properties'] |
| 163 | + |
| 164 | + assert 'status' not in after |
| 165 | + |
| 166 | + |
| 167 | +def test_changing_func_rebuilds_the_declaration(): |
| 168 | + tool = FunctionTool(sample_tool) |
| 169 | + assert tool._get_declaration().name == 'sample_tool' |
| 170 | + |
| 171 | + tool.func = poll_job |
| 172 | + |
| 173 | + assert tool._get_declaration().name == 'poll_job' |
| 174 | + |
| 175 | + |
| 176 | +@pytest.mark.parametrize( |
| 177 | + 'variant', |
| 178 | + [GoogleLLMVariant.GEMINI_API, GoogleLLMVariant.VERTEX_AI], |
| 179 | +) |
| 180 | +def test_changing_api_variant_rebuilds_the_declaration(monkeypatch, variant): |
| 181 | + """The api variant is resolved from the environment on every call.""" |
| 182 | + tool = FunctionTool(sample_tool) |
| 183 | + |
| 184 | + monkeypatch.setattr( |
| 185 | + type(tool), |
| 186 | + '_api_variant', |
| 187 | + property(lambda self: GoogleLLMVariant.GEMINI_API), |
| 188 | + ) |
| 189 | + studio = tool._get_declaration() |
| 190 | + |
| 191 | + monkeypatch.setattr( |
| 192 | + type(tool), '_api_variant', property(lambda self: variant) |
| 193 | + ) |
| 194 | + switched = tool._get_declaration() |
| 195 | + |
| 196 | + if variant is GoogleLLMVariant.GEMINI_API: |
| 197 | + assert switched.model_dump() == studio.model_dump() |
| 198 | + else: |
| 199 | + assert switched.response_json_schema is not None |
| 200 | + assert studio.response_json_schema is None |
0 commit comments