Skip to content

Commit e5801f6

Browse files
authored
replaced asyncio.coroutines.iscoroutinefunction with inspect.iscoroutine (microsoft#1267)
1 parent 284490d commit e5801f6

File tree

4 files changed

+15
-13
lines changed

4 files changed

+15
-13
lines changed

autogen/agentchat/contrib/compressible_agent.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import copy
55
import asyncio
66
import logging
7+
import inspect
78
from autogen.token_count_utils import count_token, get_max_token_limit, num_tokens_from_functions
89

910
try:
@@ -201,7 +202,7 @@ def generate_reply(
201202
reply_func = reply_func_tuple["reply_func"]
202203
if exclude and reply_func in exclude:
203204
continue
204-
if asyncio.coroutines.iscoroutinefunction(reply_func):
205+
if inspect.iscoroutinefunction(reply_func):
205206
continue
206207
if self._match_trigger(reply_func_tuple["trigger"], sender):
207208
final, reply = reply_func(self, messages=messages, sender=sender, config=reply_func_tuple["config"])

autogen/agentchat/conversable_agent.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def reply_func(
229229
"reset_config": reset_config,
230230
},
231231
)
232-
if ignore_async_in_sync_chat and asyncio.coroutines.iscoroutinefunction(reply_func):
232+
if ignore_async_in_sync_chat and inspect.iscoroutinefunction(reply_func):
233233
self._ignore_async_func_in_sync_chat_list.append(reply_func)
234234

235235
@property
@@ -629,7 +629,7 @@ def _raise_exception_on_async_reply_functions(self) -> None:
629629
self._ignore_async_func_in_sync_chat_list
630630
)
631631

632-
async_reply_functions = [f for f in reply_functions if asyncio.coroutines.iscoroutinefunction(f)]
632+
async_reply_functions = [f for f in reply_functions if inspect.iscoroutinefunction(f)]
633633
if async_reply_functions != []:
634634
msg = (
635635
"Async reply functions can only be used with ConversableAgent.a_initiate_chat(). The following async reply functions are found: "
@@ -849,7 +849,7 @@ def generate_function_call_reply(
849849
if "function_call" in message and message["function_call"]:
850850
func_call = message["function_call"]
851851
func = self._function_map.get(func_call.get("name", None), None)
852-
if asyncio.coroutines.iscoroutinefunction(func):
852+
if inspect.iscoroutinefunction(func):
853853
return False, None
854854

855855
_, func_return = self.execute_function(message["function_call"])
@@ -877,7 +877,7 @@ async def a_generate_function_call_reply(
877877
func_call = message["function_call"]
878878
func_name = func_call.get("name", "")
879879
func = self._function_map.get(func_name, None)
880-
if func and asyncio.coroutines.iscoroutinefunction(func):
880+
if func and inspect.iscoroutinefunction(func):
881881
_, func_return = await self.a_execute_function(func_call)
882882
return True, func_return
883883

@@ -906,7 +906,7 @@ def generate_tool_calls_reply(
906906
id = tool_call["id"]
907907
function_call = tool_call.get("function", {})
908908
func = self._function_map.get(function_call.get("name", None), None)
909-
if asyncio.coroutines.iscoroutinefunction(func):
909+
if inspect.iscoroutinefunction(func):
910910
continue
911911
_, func_return = self.execute_function(function_call)
912912
tool_returns.append(
@@ -1231,7 +1231,7 @@ def generate_reply(
12311231
reply_func = reply_func_tuple["reply_func"]
12321232
if exclude and reply_func in exclude:
12331233
continue
1234-
if asyncio.coroutines.iscoroutinefunction(reply_func):
1234+
if inspect.iscoroutinefunction(reply_func):
12351235
continue
12361236
if self._match_trigger(reply_func_tuple["trigger"], sender):
12371237
final, reply = reply_func(self, messages=messages, sender=sender, config=reply_func_tuple["config"])
@@ -1288,7 +1288,7 @@ async def a_generate_reply(
12881288
if exclude and reply_func in exclude:
12891289
continue
12901290
if self._match_trigger(reply_func_tuple["trigger"], sender):
1291-
if asyncio.coroutines.iscoroutinefunction(reply_func):
1291+
if inspect.iscoroutinefunction(reply_func):
12921292
final, reply = await reply_func(
12931293
self, messages=messages, sender=sender, config=reply_func_tuple["config"]
12941294
)
@@ -1537,7 +1537,7 @@ async def a_execute_function(self, func_call):
15371537
flush=True,
15381538
)
15391539
try:
1540-
if asyncio.coroutines.iscoroutinefunction(func):
1540+
if inspect.iscoroutinefunction(func):
15411541
content = await func(**arguments)
15421542
else:
15431543
# Fallback to sync function if the function is not async

test/agentchat/test_conversable_agent.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import time
55
from typing import Any, Callable, Dict, Literal
66
import unittest
7+
import inspect
78

89
import pytest
910
from unittest.mock import patch
@@ -559,7 +560,7 @@ def currency_calculator(
559560
== '{"currency":"EUR","amount":100.1}'
560561
)
561562

562-
assert not asyncio.coroutines.iscoroutinefunction(currency_calculator)
563+
assert not inspect.iscoroutinefunction(currency_calculator)
563564

564565

565566
@pytest.mark.asyncio
@@ -597,7 +598,7 @@ async def currency_calculator(
597598
== '{"currency":"EUR","amount":100.1}'
598599
)
599600

600-
assert asyncio.coroutines.iscoroutinefunction(currency_calculator)
601+
assert inspect.iscoroutinefunction(currency_calculator)
601602

602603

603604
def get_origin(d: Dict[str, Callable[..., Any]]) -> Dict[str, Callable[..., Any]]:

test/test_function_utils.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ def f(
364364
) -> Tuple[Currency, CurrencySymbol]:
365365
return base, quote_currency
366366

367-
assert not asyncio.coroutines.iscoroutinefunction(f)
367+
assert not inspect.iscoroutinefunction(f)
368368

369369
actual = f(base={"currency": "USD", "amount": 123.45}, quote_currency="EUR")
370370
assert isinstance(actual[0], Currency)
@@ -382,7 +382,7 @@ async def f(
382382
) -> Tuple[Currency, CurrencySymbol]:
383383
return base, quote_currency
384384

385-
assert asyncio.coroutines.iscoroutinefunction(f)
385+
assert inspect.iscoroutinefunction(f)
386386

387387
actual = await f(base={"currency": "USD", "amount": 123.45}, quote_currency="EUR")
388388
assert isinstance(actual[0], Currency)

0 commit comments

Comments
 (0)