retry_utils
Retry utilities for AI client implementations.
This module provides retry decorators and utilities for handling transient failures like rate limits, service unavailability, and network errors.
Retry utilities for AI client implementations.
This module provides retry decorators and utilities for handling transient failures like rate limits, service unavailability, and network errors.
Bases:
ExceptionException for service unavailable errors (503).
- get_retry_decorator(max_attempts=3, min_wait=1, max_wait=60, exponential_base=2, jitter=True, retry_exceptions=(<class 'ffai.retry_utils.RateLimitError'>, <class 'ffai.retry_utils.ServiceUnavailableError'>, <class 'ConnectionError'>, <class 'TimeoutError'>), log_level=20)[source]
Create a retry decorator with configurable parameters.
- Parameters:
max_attempts (int) – Maximum number of retry attempts
min_wait (float) – Minimum wait time in seconds
max_wait (float) – Maximum wait time in seconds
exponential_base (float) – Base for exponential backoff
jitter (bool) – Whether to add jitter to wait times
retry_exceptions (tuple) – Tuple of exception types to retry on
log_level (int) – Logging level for retry messages
- Returns:
Configured retry decorator
Example
>>> @get_retry_decorator(max_attempts=5, min_wait=2) ... def my_api_call(): ... return client.generate_response("Hello")
- get_configured_retry_decorator()[source]
Get retry decorator configured from global config, with fallback to defaults.
Reads retry settings from the application config (
config/main.yaml). Falls back toget_retry_decorator()defaults when config is unavailable.- Returns:
Configured retry decorator
- retry_with_backoff(func, max_attempts=3, min_wait=1, max_wait=60, exponential_base=2, jitter=True, *args, **kwargs)[source]
Execute a function with retry and exponential backoff.
- Parameters:
func (Any) – Function to execute
max_attempts (int) – Maximum number of retry attempts
min_wait (float) – Minimum wait time in seconds
max_wait (float) – Maximum wait time in seconds
exponential_base (float) – Base for exponential backoff
jitter (bool) – Whether to add jitter to wait times
*args – Positional arguments for func
**kwargs – Keyword arguments for func
- Returns:
Result of func if successful
- Raises:
RetryError – If all retry attempts fail
Exception – Original exception if not retryable
- Return type:
Example
>>> result = retry_with_backoff( ... client.generate_response, ... max_attempts=5, ... prompt="Hello" ... )
- create_rate_limit_error(exception)[source]
Create a RateLimitError from another exception.
- Parameters:
exception (Exception) – Original exception
- Returns:
RateLimitError with information from original exception
- Return type:
Classes
Functions
- create_rate_limit_error(exception)[source]
Create a RateLimitError from another exception.
- Parameters:
exception (Exception) – Original exception
- Returns:
RateLimitError with information from original exception
- Return type:
- get_configured_retry_decorator()[source]
Get retry decorator configured from global config, with fallback to defaults.
Reads retry settings from the application config (
config/main.yaml). Falls back toget_retry_decorator()defaults when config is unavailable.- Returns:
Configured retry decorator
- get_retry_decorator(max_attempts=3, min_wait=1, max_wait=60, exponential_base=2, jitter=True, retry_exceptions=(<class 'ffai.retry_utils.RateLimitError'>, <class 'ffai.retry_utils.ServiceUnavailableError'>, <class 'ConnectionError'>, <class 'TimeoutError'>), log_level=20)[source]
Create a retry decorator with configurable parameters.
- Parameters:
max_attempts (int) – Maximum number of retry attempts
min_wait (float) – Minimum wait time in seconds
max_wait (float) – Maximum wait time in seconds
exponential_base (float) – Base for exponential backoff
jitter (bool) – Whether to add jitter to wait times
retry_exceptions (tuple) – Tuple of exception types to retry on
log_level (int) – Logging level for retry messages
- Returns:
Configured retry decorator
Example
>>> @get_retry_decorator(max_attempts=5, min_wait=2) ... def my_api_call(): ... return client.generate_response("Hello")
- retry_with_backoff(func, max_attempts=3, min_wait=1, max_wait=60, exponential_base=2, jitter=True, *args, **kwargs)[source]
Execute a function with retry and exponential backoff.
- Parameters:
func (Any) – Function to execute
max_attempts (int) – Maximum number of retry attempts
min_wait (float) – Minimum wait time in seconds
max_wait (float) – Maximum wait time in seconds
exponential_base (float) – Base for exponential backoff
jitter (bool) – Whether to add jitter to wait times
*args – Positional arguments for func
**kwargs – Keyword arguments for func
- Returns:
Result of func if successful
- Raises:
RetryError – If all retry attempts fail
Exception – Original exception if not retryable
- Return type:
Example
>>> result = retry_with_backoff( ... client.generate_response, ... max_attempts=5, ... prompt="Hello" ... )