This repository was archived by the owner on Jun 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
106 lines (87 loc) · 3.84 KB
/
Copy pathutils.py
File metadata and controls
106 lines (87 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# Copyright (c) 2023 - 2025, AG2ai, Inc., AG2ai open-source projects maintainers and core contributors
#
# SPDX-License-Identifier: Apache-2.0
#
# Portions derived from https://github.com/microsoft/autogen are under the MIT License.
# SPDX-License-Identifier: MIT
import asyncio
import functools
import time
from collections.abc import Callable
from json.decoder import JSONDecodeError
from typing import Any, TypeVar
import pytest
from autogen.fast_depends.utils import is_coroutine_callable
from autogen.import_utils import optional_import_block
T = TypeVar("T", bound=Callable[..., Any])
def suppress(
exception: type[BaseException],
*,
retries: int = 0,
timeout: int = 60,
error_filter: Callable[[BaseException], bool] | None = None,
) -> Callable[[T], T]:
"""Suppresses the specified exception and retries the function a specified number of times.
Args:
exception: The exception to suppress.
retries: The number of times to retry the function. If None, the function will tried once and just return in case of exception raised. Defaults to None.
timeout: The time to wait between retries in seconds. Defaults to 60.
error_filter: A function that takes an exception as input and returns a boolean indicating whether the exception should be suppressed. Defaults to None.
"""
def decorator(
func: T,
exception: type[BaseException] = exception,
retries: int = retries,
timeout: int = timeout,
error_filter: Callable[[BaseException], bool] | None = error_filter,
) -> T:
if is_coroutine_callable(func):
@functools.wraps(func)
async def wrapper(
*args: Any,
exception: type[BaseException] = exception,
retries: int = retries,
timeout: int = timeout,
**kwargs: Any,
) -> Any:
for i in range(retries + 1):
try:
return await func(*args, **kwargs)
except exception as e:
if error_filter and not error_filter(e): # type: ignore [arg-type]
raise
if i >= retries - 1:
pytest.xfail(f"Suppressed '{exception}' raised {i + 1} times")
raise
await asyncio.sleep(timeout)
else:
@functools.wraps(func)
def wrapper(
*args: Any,
exception: type[BaseException] = exception,
retries: int = retries,
timeout: int = timeout,
**kwargs: Any,
) -> Any:
for i in range(retries + 1):
try:
return func(*args, **kwargs)
except exception as e:
if error_filter and not error_filter(e): # type: ignore [arg-type]
raise
if i >= retries - 1:
pytest.xfail(f"Suppressed '{exception}' raised {i + 1} times")
raise
time.sleep(timeout)
return wrapper # type: ignore[return-value]
return decorator
def suppress_gemini_resource_exhausted(func: T) -> T:
with optional_import_block():
from google.genai.errors import ClientError
# Catch only code 429 which is RESOURCE_EXHAUSTED error instead of catching all the client errors
def is_resource_exhausted_error(e: BaseException) -> bool:
return isinstance(e, ClientError) and getattr(e, "code", None) in [429, 503]
return suppress(ClientError, retries=2, error_filter=is_resource_exhausted_error)(func)
return func
def suppress_json_decoder_error(func: T) -> T:
return suppress(JSONDecodeError)(func)