Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions core/utils/retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

logger = logging.getLogger(__name__)

# Configure a clean file logging mechanism without side effects
if not logger.handlers:
handler = logging.FileHandler("retry.log")
formatter = logging.Formatter(
Expand All @@ -17,10 +18,14 @@
logger.addHandler(handler)
logger.setLevel(logging.WARNING)

logging.basicConfig(filename="retry.log", level=logging.WARNING)

def retry(max_retries: int = 3, base_delay: float = 1.0):
"""
Decorator to automatically retry both sync and async functions
upon hitting OpenAI API or Rate Limit errors with exponential backoff.
"""
def decorator(func):
# Handle Asynchronous Functions
if inspect.iscoroutinefunction(func):

@wraps(func)
Expand All @@ -43,6 +48,7 @@ async def async_wrapper(*args, **kwargs):

return async_wrapper

# Handle Synchronous Functions
else:

@wraps(func)
Expand All @@ -65,4 +71,4 @@ def sync_wrapper(*args, **kwargs):

return sync_wrapper

return decorator
return decorator