fix: support Callable that has __call__ as coroutine function in FunctionTool

PiperOrigin-RevId: 760913537
This commit is contained in:
Xiang (Sean) Zhou
2025-05-19 22:09:08 -07:00
committed by Copybara-Service
parent 5115474f2b
commit f67ccf32c3
2 changed files with 54 additions and 2 deletions

View File

@@ -39,6 +39,14 @@ async def async_function_for_testing_with_2_arg_and_no_tool_context(arg1, arg2):
return arg1
class AsyncCallableWith2ArgsAndNoToolContext:
async def __call__(self, arg1, arg2):
assert arg1
assert arg2
return arg1
def function_for_testing_with_1_arg_and_tool_context(arg1, tool_context):
"""Function for testing with 1 arge and tool context."""
assert arg1
@@ -46,6 +54,14 @@ def function_for_testing_with_1_arg_and_tool_context(arg1, tool_context):
return arg1
class AsyncCallableWith1ArgAndToolContext:
async def __call__(self, arg1, tool_context):
assert arg1
assert tool_context
return arg1
def function_for_testing_with_2_arg_and_no_tool_context(arg1, arg2):
"""Function for testing with 2 arge and no tool context."""
assert arg1
@@ -83,6 +99,16 @@ async def test_run_async_with_tool_context_async_func():
assert result == "test_value_1"
@pytest.mark.asyncio
async def test_run_async_with_tool_context_async_callable():
"""Test that run_async calls the callable with tool_context when tool_context is in signature (async callable)."""
tool = FunctionTool(AsyncCallableWith1ArgAndToolContext())
args = {"arg1": "test_value_1"}
result = await tool.run_async(args=args, tool_context=MagicMock())
assert result == "test_value_1"
@pytest.mark.asyncio
async def test_run_async_without_tool_context_async_func():
"""Test that run_async calls the function without tool_context when tool_context is not in signature (async function)."""
@@ -92,6 +118,15 @@ async def test_run_async_without_tool_context_async_func():
assert result == "test_value_1"
@pytest.mark.asyncio
async def test_run_async_without_tool_context_async_callable():
"""Test that run_async calls the callable without tool_context when tool_context is not in signature (async callable)."""
tool = FunctionTool(AsyncCallableWith2ArgsAndNoToolContext())
args = {"arg1": "test_value_1", "arg2": "test_value_2"}
result = await tool.run_async(args=args, tool_context=MagicMock())
assert result == "test_value_1"
@pytest.mark.asyncio
async def test_run_async_with_tool_context_sync_func():
"""Test that run_async calls the function with tool_context when tool_context is in signature (synchronous function)."""