Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

fix(utils): bind async wrappers to the correct sync functions (#4199)

Tao Sun3ae645e23132
diff --git a/camel/utils/async_func.py b/camel/utils/async_func.py
index b3d321194..8a6836515 100644
--- a/camel/utils/async_func.py
+++ b/camel/utils/async_func.py
@@ -29,14 +29,19 @@ def sync_funcs_to_async(funcs: list[FunctionTool]) -> list[FunctionTool]:
list[FunctionTool]: List of Python asynchronous functions
in the :obj:`FunctionTool` format.
"""
- async_funcs = []
- for func in funcs:
- sync_func = func.func
+ def _create_async_callable(sync_func):
async def async_callable(*args, **kwargs):
- return await asyncio.to_thread(sync_func, *args, **kwargs) # noqa: B023
+ return await asyncio.to_thread(sync_func, *args, **kwargs)
+
+ return async_callable
+ async_funcs = []
+ for func in funcs:
async_funcs.append(
- FunctionTool(async_callable, deepcopy(func.openai_tool_schema))
+ FunctionTool(
+ _create_async_callable(func.func),
+ deepcopy(func.openai_tool_schema),
+ )
)
return async_funcs
diff --git a/test/utils/test_async_func.py b/test/utils/test_async_func.py
new file mode 100644
index 000000000..bedfe6b5e
--- /dev/null
+++ b/test/utils/test_async_func.py
@@ -0,0 +1,37 @@
+# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========
+import pytest
+
+from camel.toolkits import FunctionTool
+from camel.utils.async_func import sync_funcs_to_async
+
+
+async def test_sync_funcs_to_async_binds_each_function():
+ def add(a: int, b: int) -> int:
+ r"""Add two integers."""
+ return a + b
+
+ def multiply(a: int, b: int) -> int:
+ r"""Multiply two integers."""
+ return a * b
+
+ add_tool, multiply_tool = sync_funcs_to_async(
+ [FunctionTool(add), FunctionTool(multiply)]
+ )
+
+ assert await add_tool.func(3, 4) == 7
+ assert await multiply_tool.func(3, 4) == 12
+ assert add_tool.get_function_name() == "add"
+ assert multiply_tool.get_function_name() == "multiply"