nemoguardrails.library.clavata.utils

View as Markdown

Module Contents

Classes

NameDescription
AttemptsExceededErrorException raised when the maximum number of retries is exceeded.

Functions

NameDescription
calculate_exp_delayHandles calculation of the delay for a specific attempt. Note that we specifically
exponential_backoffExponential backoff retry mechanism.

Data

P

ReturnT

API

class nemoguardrails.library.clavata.utils.AttemptsExceededError(
attempts: int,
max_attempts: int,
last_exception: typing.Optional[Exception]
)
Exception

Bases: Exception

Exception raised when the maximum number of retries is exceeded.

nemoguardrails.library.clavata.utils.calculate_exp_delay(
retries: int,
initial_delay: float,
max_delay: float,
jitter: bool
) -> float

Handles calculation of the delay for a specific attempt. Note that we specifically ask for the number of retries, not the number of attempts, because the first attempt is the initial call and we want the first delay to be raised to the power of 0.

Using “retries” instead of “attempts” makes it clearer what the input is, even though a value called “attempts” is being passed in below.

Because this is an exponential backoff, the factor of increase is always 2.

Parameters:

retries
int

The number of retries made so far.

initial_delay
float

The initial delay.

max_delay
float

The maximum delay.

jitter
bool

Whether to apply jitter to the delay. We use a full-jitter approach.

nemoguardrails.library.clavata.utils.exponential_backoff(
max_attempts: int = 3,
initial_delay: float = 1.0,
max_delay: float = 10.0,
jitter: bool = True,
retry_exceptions: typing.Union[type[Exception], collections.abc.Iterable[type[Exception]]] = Exception,
on_permanent_failure: typing.Optional[collections.abc.Callable[[int, Exception], collections.abc.Awaitable[typing.Any]]] = None
)

Exponential backoff retry mechanism.

nemoguardrails.library.clavata.utils.P = ParamSpec('P')
nemoguardrails.library.clavata.utils.ReturnT = TypeVar('ReturnT')