Skip to content

A programming framework for agentic AI

FunctionTool partial support (#5183)

Nour Bouzid02e968a531b7
diff --git a/python/packages/autogen-core/src/autogen_core/_function_utils.py b/python/packages/autogen-core/src/autogen_core/_function_utils.py
index 503b89120..cbf157d97 100644
--- a/python/packages/autogen-core/src/autogen_core/_function_utils.py
+++ b/python/packages/autogen-core/src/autogen_core/_function_utils.py
@@ -3,6 +3,7 @@
import inspect
import typing
+from functools import partial
from logging import getLogger
from typing import (
Annotated,
@@ -41,7 +42,8 @@ def get_typed_signature(call: Callable[..., Any]) -> inspect.Signature:
"""
signature = inspect.signature(call)
globalns = getattr(call, "__globals__", {})
- type_hints = typing.get_type_hints(call, globalns, include_extras=True)
+ func_call = call.func if isinstance(call, partial) else call
+ type_hints = typing.get_type_hints(func_call, globalns, include_extras=True)
typed_params = [
inspect.Parameter(
name=param.name,
diff --git a/python/packages/autogen-core/src/autogen_core/tools/_function_tool.py b/python/packages/autogen-core/src/autogen_core/tools/_function_tool.py
index 12cee716f..7cd4d0a9e 100644
--- a/python/packages/autogen-core/src/autogen_core/tools/_function_tool.py
+++ b/python/packages/autogen-core/src/autogen_core/tools/_function_tool.py
@@ -88,7 +88,7 @@ class FunctionTool(BaseTool[BaseModel, BaseModel], Component[FunctionToolConfig]
self._func = func
self._global_imports = global_imports
signature = get_typed_signature(func)
- func_name = name or func.__name__
+ func_name = name or func.func.__name__ if isinstance(func, functools.partial) else name or func.__name__
args_model = args_base_model_from_signature(func_name + "args", signature)
return_type = signature.return_annotation
self._has_cancellation_support = "cancellation_token" in signature.parameters
diff --git a/python/packages/autogen-core/tests/test_tools.py b/python/packages/autogen-core/tests/test_tools.py
index f6910b507..f1c2e44f0 100644
--- a/python/packages/autogen-core/tests/test_tools.py
+++ b/python/packages/autogen-core/tests/test_tools.py
@@ -1,4 +1,5 @@
import inspect
+from functools import partial
from typing import Annotated, List
import pytest
@@ -109,6 +110,67 @@ def test_func_tool_schema_generation_only_default_arg() -> None:
assert "required" not in schema["parameters"]
+def test_func_tool_with_partial_positional_arguments_schema_generation() -> None:
+ """Test correct schema generation for a partial function with positional arguments."""
+
+ def get_weather(country: str, city: str) -> str:
+ return f"The temperature in {city}, {country} is 75°"
+
+ partial_function = partial(get_weather, "Germany")
+ tool = FunctionTool(partial_function, description="Partial function tool.")
+ schema = tool.schema
+
+ assert schema["name"] == "get_weather"
+ assert "description" in schema
+ assert schema["description"] == "Partial function tool."
+ assert "parameters" in schema
+ assert schema["parameters"]["type"] == "object"
+ assert schema["parameters"]["properties"].keys() == {"city"}
+ assert schema["parameters"]["properties"]["city"]["type"] == "string"
+ assert schema["parameters"]["properties"]["city"]["description"] == "city"
+ assert "required" in schema["parameters"]
+ assert schema["parameters"]["required"] == ["city"]
+ assert "country" not in schema["parameters"]["properties"] # check country not in schema params
+ assert len(schema["parameters"]["properties"]) == 1
+
+
+def test_func_call_tool_with_kwargs_schema_generation() -> None:
+ """Test correct schema generation for a partial function with kwargs."""
+
+ def get_weather(country: str, city: str) -> str:
+ return f"The temperature in {city}, {country} is 75°"
+
+ partial_function = partial(get_weather, country="Germany")
+ tool = FunctionTool(partial_function, description="Partial function tool.")
+ schema = tool.schema
+
+ assert schema["name"] == "get_weather"
+ assert "description" in schema
+ assert schema["description"] == "Partial function tool."
+ assert "parameters" in schema
+ assert schema["parameters"]["type"] == "object"
+ assert schema["parameters"]["properties"].keys() == {"country", "city"}
+ assert schema["parameters"]["properties"]["city"]["type"] == "string"
+ assert schema["parameters"]["properties"]["country"]["type"] == "string"
+ assert "required" in schema["parameters"]
+ assert schema["parameters"]["required"] == ["city"] # only city is required
+ assert len(schema["parameters"]["properties"]) == 2
+
+
+async def test_run_func_call_tool_with_kwargs_and_args() -> None:
+ """Test run partial function with kwargs and args."""
+
+ def get_weather(country: str, city: str, unit: str = "Celsius") -> str:
+ return f"The temperature in {city}, {country} is 75° {unit}"
+
+ partial_function = partial(get_weather, "Germany", unit="Fahrenheit")
+ tool = FunctionTool(partial_function, description="Partial function tool.")
+ result = await tool.run_json({"city": "Berlin"}, CancellationToken())
+ assert isinstance(result, str)
+ assert result == "The temperature in Berlin, Germany is 75° Fahrenheit"
+
+
@pytest.mark.asyncio
async def test_tool_run() -> None:
tool = MyTool()