A programming framework for agentic AI
replaced asyncio.coroutines.iscoroutinefunction with inspect.iscoroutine (#1267)
Davor Runjebde2fc9398f7
diff --git a/autogen/agentchat/contrib/compressible_agent.py b/autogen/agentchat/contrib/compressible_agent.pyindex a8a186edc..f9ea6bd26 100644--- a/autogen/agentchat/contrib/compressible_agent.py+++ b/autogen/agentchat/contrib/compressible_agent.py@@ -4,6 +4,7 @@ from autogen import Agent, ConversableAgentimport copyimport asyncioimport logging+import inspectfrom autogen.token_count_utils import count_token, get_max_token_limit, num_tokens_from_functionstry:@@ -201,7 +202,7 @@ Reply "TERMINATE" in the end when everything is done.reply_func = reply_func_tuple["reply_func"]if exclude and reply_func in exclude:continue- if asyncio.coroutines.iscoroutinefunction(reply_func):+ if inspect.iscoroutinefunction(reply_func):continueif self._match_trigger(reply_func_tuple["trigger"], sender):final, reply = reply_func(self, messages=messages, sender=sender, config=reply_func_tuple["config"])diff --git a/autogen/agentchat/conversable_agent.py b/autogen/agentchat/conversable_agent.pyindex 0a94905ff..ca1686198 100644--- a/autogen/agentchat/conversable_agent.py+++ b/autogen/agentchat/conversable_agent.py@@ -229,7 +229,7 @@ class ConversableAgent(Agent):"reset_config": reset_config,},)- if ignore_async_in_sync_chat and asyncio.coroutines.iscoroutinefunction(reply_func):+ if ignore_async_in_sync_chat and inspect.iscoroutinefunction(reply_func):self._ignore_async_func_in_sync_chat_list.append(reply_func)@property@@ -629,7 +629,7 @@ class ConversableAgent(Agent):self._ignore_async_func_in_sync_chat_list)- async_reply_functions = [f for f in reply_functions if asyncio.coroutines.iscoroutinefunction(f)]+ async_reply_functions = [f for f in reply_functions if inspect.iscoroutinefunction(f)]if async_reply_functions != []:msg = ("Async reply functions can only be used with ConversableAgent.a_initiate_chat(). The following async reply functions are found: "@@ -849,7 +849,7 @@ class ConversableAgent(Agent):if "function_call" in message and message["function_call"]:func_call = message["function_call"]func = self._function_map.get(func_call.get("name", None), None)- if asyncio.coroutines.iscoroutinefunction(func):+ if inspect.iscoroutinefunction(func):return False, None_, func_return = self.execute_function(message["function_call"])@@ -877,7 +877,7 @@ class ConversableAgent(Agent):func_call = message["function_call"]func_name = func_call.get("name", "")func = self._function_map.get(func_name, None)- if func and asyncio.coroutines.iscoroutinefunction(func):+ if func and inspect.iscoroutinefunction(func):_, func_return = await self.a_execute_function(func_call)return True, func_return@@ -906,7 +906,7 @@ class ConversableAgent(Agent):id = tool_call["id"]function_call = tool_call.get("function", {})func = self._function_map.get(function_call.get("name", None), None)- if asyncio.coroutines.iscoroutinefunction(func):+ if inspect.iscoroutinefunction(func):continue_, func_return = self.execute_function(function_call)tool_returns.append(@@ -1231,7 +1231,7 @@ class ConversableAgent(Agent):reply_func = reply_func_tuple["reply_func"]if exclude and reply_func in exclude:continue- if asyncio.coroutines.iscoroutinefunction(reply_func):+ if inspect.iscoroutinefunction(reply_func):continueif self._match_trigger(reply_func_tuple["trigger"], sender):final, reply = reply_func(self, messages=messages, sender=sender, config=reply_func_tuple["config"])@@ -1288,7 +1288,7 @@ class ConversableAgent(Agent):if exclude and reply_func in exclude:continueif self._match_trigger(reply_func_tuple["trigger"], sender):- if asyncio.coroutines.iscoroutinefunction(reply_func):+ if inspect.iscoroutinefunction(reply_func):final, reply = await reply_func(self, messages=messages, sender=sender, config=reply_func_tuple["config"])@@ -1537,7 +1537,7 @@ class ConversableAgent(Agent):flush=True,)try:- if asyncio.coroutines.iscoroutinefunction(func):+ if inspect.iscoroutinefunction(func):content = await func(**arguments)else:# Fallback to sync function if the function is not asyncdiff --git a/test/agentchat/test_conversable_agent.py b/test/agentchat/test_conversable_agent.pyindex 4acaa1da5..2fa10602d 100644--- a/test/agentchat/test_conversable_agent.py+++ b/test/agentchat/test_conversable_agent.py@@ -4,6 +4,7 @@ import sysimport timefrom typing import Any, Callable, Dict, Literalimport unittest+import inspectimport pytestfrom unittest.mock import patch@@ -559,7 +560,7 @@ def test__wrap_function_sync():== '{"currency":"EUR","amount":100.1}')- assert not asyncio.coroutines.iscoroutinefunction(currency_calculator)+ assert not inspect.iscoroutinefunction(currency_calculator)@pytest.mark.asyncio@@ -597,7 +598,7 @@ async def test__wrap_function_async():== '{"currency":"EUR","amount":100.1}')- assert asyncio.coroutines.iscoroutinefunction(currency_calculator)+ assert inspect.iscoroutinefunction(currency_calculator)def get_origin(d: Dict[str, Callable[..., Any]]) -> Dict[str, Callable[..., Any]]:diff --git a/test/test_function_utils.py b/test/test_function_utils.pyindex 705453e42..b3417860b 100644--- a/test/test_function_utils.py+++ b/test/test_function_utils.py@@ -364,7 +364,7 @@ def test_load_basemodels_if_needed_sync() -> None:) -> Tuple[Currency, CurrencySymbol]:return base, quote_currency- assert not asyncio.coroutines.iscoroutinefunction(f)+ assert not inspect.iscoroutinefunction(f)actual = f(base={"currency": "USD", "amount": 123.45}, quote_currency="EUR")assert isinstance(actual[0], Currency)@@ -382,7 +382,7 @@ async def test_load_basemodels_if_needed_async() -> None:) -> Tuple[Currency, CurrencySymbol]:return base, quote_currency- assert asyncio.coroutines.iscoroutinefunction(f)+ assert inspect.iscoroutinefunction(f)actual = await f(base={"currency": "USD", "amount": 123.45}, quote_currency="EUR")assert isinstance(actual[0], Currency)