|
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | 15 | from enum import Enum |
| 16 | +import functools |
16 | 17 | import inspect |
17 | 18 | from typing import Any |
18 | 19 | from typing import Optional |
|
24 | 25 | from google.adk.agents.invocation_context import InvocationContext |
25 | 26 | from google.adk.features import FeatureName |
26 | 27 | from google.adk.features._feature_registry import temporary_feature_override |
| 28 | +from google.adk.models.llm_request import LlmRequest |
27 | 29 | from google.adk.sessions.session import Session |
28 | 30 | from google.adk.tools.function_tool import _build_declaration_cached |
29 | 31 | from google.adk.tools.function_tool import FunctionTool |
@@ -167,6 +169,56 @@ async def test_run_async_with_tool_context_async_callable(): |
167 | 169 | assert tool.description == "Async call doc" |
168 | 170 |
|
169 | 171 |
|
| 172 | +def test_callable_declaration_uses_call_docstring(): |
| 173 | + """The declaration carries the same __call__ docstring as tool.description.""" |
| 174 | + tool = FunctionTool(AsyncCallableWith1ArgAndToolContext()) |
| 175 | + |
| 176 | + llm_request = LlmRequest() |
| 177 | + llm_request.append_tools([tool]) |
| 178 | + |
| 179 | + declaration = llm_request.config.tools[0].function_declarations[0] |
| 180 | + assert declaration.description == "Async call doc" |
| 181 | + |
| 182 | + |
| 183 | +def test_partial_tools_do_not_shadow_each_other(): |
| 184 | + """Partials of different functions keep their own names and docstrings.""" |
| 185 | + |
| 186 | + def get_weather(api_key: str, city: str) -> str: |
| 187 | + """Returns the current weather for a city.""" |
| 188 | + return city |
| 189 | + |
| 190 | + def get_forecast(api_key: str, city: str, days: int = 3) -> str: |
| 191 | + """Returns a forecast for a city.""" |
| 192 | + return city |
| 193 | + |
| 194 | + llm_request = LlmRequest() |
| 195 | + llm_request.append_tools([ |
| 196 | + FunctionTool(functools.partial(get_weather, "key")), |
| 197 | + FunctionTool(functools.partial(get_forecast, "key")), |
| 198 | + ]) |
| 199 | + |
| 200 | + declarations = llm_request.config.tools[0].function_declarations |
| 201 | + assert [d.name for d in declarations] == ["get_weather", "get_forecast"] |
| 202 | + assert [d.description for d in declarations] == [ |
| 203 | + "Returns the current weather for a city.", |
| 204 | + "Returns a forecast for a city.", |
| 205 | + ] |
| 206 | + assert list(llm_request.tools_dict) == ["get_weather", "get_forecast"] |
| 207 | + |
| 208 | + with temporary_feature_override(FeatureName.JSON_SCHEMA_FOR_FUNC_DECL, False): |
| 209 | + legacy_request = LlmRequest() |
| 210 | + legacy_request.append_tools([ |
| 211 | + FunctionTool(functools.partial(get_weather, "key")), |
| 212 | + FunctionTool(functools.partial(get_forecast, "key")), |
| 213 | + ]) |
| 214 | + legacy_declarations = legacy_request.config.tools[0].function_declarations |
| 215 | + assert [d.name for d in legacy_declarations] == [ |
| 216 | + "get_weather", |
| 217 | + "get_forecast", |
| 218 | + ] |
| 219 | + assert list(legacy_request.tools_dict) == ["get_weather", "get_forecast"] |
| 220 | + |
| 221 | + |
170 | 222 | @pytest.mark.asyncio |
171 | 223 | async def test_run_async_without_tool_context_async_func(): |
172 | 224 | """Test that run_async calls the function without tool_context when tool_context is not in signature (async function).""" |
|
0 commit comments