From b45440084e65520101222a00c66d0c4d69234132 Mon Sep 17 00:00:00 2001 From: DZH166 <248446924+DZH166@users.noreply.github.com> Date: Wed, 4 Nov 2026 12:28:37 +0800 Subject: [PATCH] fix(tools): compare a parameter default with the empty sentinel by identity param.default == inspect.Parameter.empty runs the default's __eq__, so a numpy/pandas default made an optional parameter mandatory or raised on ambiguous truth values. Use 'is'. --- src/google/adk/tools/function_tool.py | 6 ++- tests/unittests/tools/test_function_tool.py | 51 +++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/google/adk/tools/function_tool.py b/src/google/adk/tools/function_tool.py index 3bd12241426..38613767c74 100644 --- a/src/google/adk/tools/function_tool.py +++ b/src/google/adk/tools/function_tool.py @@ -659,8 +659,12 @@ def _get_mandatory_args( # 3. It's not an internal parameter to ignore (e.g. tool_context, input_stream) # # For more refer to: https://docs.python.org/3/library/inspect.html#inspect.Parameter.kind + # Identity, not equality: a default that reports elementwise equality + # (a numpy array or a pandas Series, as a numeric tool parameter + # naturally has) makes `==` return a non-boolean, which either raises + # when tested for truth or marks an optional parameter as mandatory. if ( - param.default == inspect.Parameter.empty + param.default is inspect.Parameter.empty and param.kind not in ( inspect.Parameter.VAR_POSITIONAL, diff --git a/tests/unittests/tools/test_function_tool.py b/tests/unittests/tools/test_function_tool.py index 1e28eb109ce..cfd4b90fc73 100644 --- a/tests/unittests/tools/test_function_tool.py +++ b/tests/unittests/tools/test_function_tool.py @@ -837,6 +837,57 @@ async def streaming_tool_req(req_param: str): assert "mandatory input parameters are not present" in result["error"] +class _AmbiguousTruth: + """Stands in for the boolean array `numpy.ndarray == sentinel` returns.""" + + def __bool__(self) -> bool: + raise ValueError( + "The truth value of an array with more than one element is ambiguous." + ) + + +class _ElementwiseDefault: + """A default whose equality is elementwise, the way a numpy array's is.""" + + def __eq__(self, other: object) -> Any: + return _AmbiguousTruth() + + __hash__ = None + + +_WEIGHTS_DEFAULT = _ElementwiseDefault() + + +def function_for_testing_with_elementwise_default( + text: str, weights: Any = _WEIGHTS_DEFAULT +): + """Function whose optional parameter has an elementwise-equality default.""" + return {"text": text, "weights_is_default": weights is _WEIGHTS_DEFAULT} + + +@pytest.mark.asyncio +async def test_optional_param_with_elementwise_equality_default_is_optional( + mock_tool_context, +): + """A default is found by identity, not by testing its equality. + + A default that reports elementwise equality, as a numeric array default in a + tool signature naturally does, turns `default == inspect.Parameter.empty` + into a non-boolean truthy value: the optional parameter was then reported as + mandatory, so every call that omitted it was answered with the missing + mandatory parameter error and its default was never used. + """ + tool = FunctionTool(function_for_testing_with_elementwise_default) + + assert tool._get_mandatory_args() == ["text"] + + result = await tool.run_async( + args={"text": "hello"}, + tool_context=mock_tool_context, + ) + assert result == {"text": "hello", "weights_is_default": True} + + @pytest.mark.asyncio async def test_run_async_coerces_integral_float_to_int_param(mock_tool_context): """A proto Struct round-trip turns an int arg into a float; it is coerced back."""