Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/google/adk/tools/function_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
51 changes: 51 additions & 0 deletions tests/unittests/tools/test_function_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
Loading