diff --git a/src/google/adk/tools/function_tool.py b/src/google/adk/tools/function_tool.py index e14898424e6..f900e37809b 100644 --- a/src/google/adk/tools/function_tool.py +++ b/src/google/adk/tools/function_tool.py @@ -683,8 +683,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 14c48173b9b..8cd0289605b 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."""