Skip to content

Commit 671c0fc

Browse files
committed
Enhance timeout error messaging in Async providers to include provider name for better debugging
1 parent 6aea573 commit 671c0fc

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

g4f/providers/base_provider.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
from ..typing import CreateResult, AsyncResult, Messages
1818
from .types import BaseProvider
19-
from .asyncio import get_running_loop, to_sync_generator, to_async_iterator, await_callback
19+
from .asyncio import get_running_loop, to_sync_generator, to_async_iterator
2020
from .response import BaseConversation, AuthResult
2121
from .helper import concat_chunks
2222
from ..cookies import get_cookies_dir
@@ -121,9 +121,10 @@ def create_func() -> str:
121121
return concat_chunks(cls.create_completion(model, messages, **kwargs))
122122
try:
123123
return await asyncio.wait_for(
124-
loop.run_in_executor(executor, create_func), timeout=timeout)
124+
loop.run_in_executor(executor, create_func), timeout=timeout
125+
)
125126
except TimeoutError as e:
126-
raise TimeoutError("The operation timed out after {} seconds".format(timeout)) from e
127+
raise TimeoutError("The operation timed out after {} seconds in {}".format(timeout, cls.__name__)) from e
127128

128129
@classmethod
129130
def create_function(cls, *args, **kwargs) -> CreateResult:
@@ -358,7 +359,7 @@ async def async_create_function(cls, *args, **kwargs) -> AsyncResult:
358359
timeout=timeout
359360
)
360361
except TimeoutError as e:
361-
raise TimeoutError("The operation timed out after {} seconds".format(timeout)) from e
362+
raise TimeoutError("The operation timed out after {} seconds in {}".format(timeout, cls.__name__)) from e
362363
except StopAsyncIteration:
363364
break
364365
else:
@@ -525,12 +526,15 @@ async def create_async_generator(
525526
auth_result = cls.get_auth_result()
526527
response = to_async_iterator(cls.create_authed(model, messages, **kwargs, auth_result=auth_result))
527528
if "stream_timeout" in kwargs or "timeout" in kwargs:
529+
timeout = kwargs.get("stream_timeout") if cls.use_stream_timeout else kwargs.get("timeout")
528530
while True:
529531
try:
530-
yield await await_callback(
532+
yield await asyncio.wait_for(
531533
response.__anext__(),
532-
timeout=kwargs.get("stream_timeout") if cls.use_stream_timeout else kwargs.get("timeout")
534+
timeout=timeout
533535
)
536+
except TimeoutError as e:
537+
raise TimeoutError("The operation timed out after {} seconds in {}".format(timeout, cls.__name__)) from e
534538
except StopAsyncIteration:
535539
break
536540
else:

0 commit comments

Comments
 (0)