Skip to content

Retry Policy

Bounded retry policies for remote integrations, decided by the normalized error.

The loops here take their limits from a core RetryConfig and decide with is_transient_error, which reads the error's exported recovery.retryable, so the retry decision and the guidance a caller sees cannot disagree. Each attempt must raise a normalized error; anything else stops the loop unchanged. failure_scope is the one decision about what a failure makes invalid, and stops_run is the one decision about whether it stops the run: metrics, judge chains, evaluators and the runner all read them.

FailureScope

Bases: IntEnum

What one normalized failure makes invalid. A larger value makes more invalid.

The scope decides the outcome of a failure during the run: fail the test case, or stop the run.

Attributes:

Name Type Description
JUDGE_CALL

One call to one judge failed, for example a rate limit, a timeout or an unusable judge reply. The next judge in a fallback chain is tried. If no judge succeeds, the test case fails.

TEST_CASE

No judge can score this test case as given, for example a broken attachment or an operation the component cannot do for this test case. A fallback chain stops, the test case fails, and the other test cases continue.

JUDGE_SETUP

The key, billing or model of one judge is wrong. The next judge in a fallback chain is tried. When every judge fails this way, the run stops.

RUN

The configuration or the code is wrong: configuration, a missing dependency, denied access, an invalid result or an unexpected defect. The run stops at once.

fail_fast_scope(enabled)

Makes a failed test case stop the run, for the work started inside this block.

The setting is local to the current task and the tasks it starts, so concurrent runs do not share it.

Parameters:

Name Type Description Default
enabled bool

Whether a failed test case stops the run.

required

Yields:

Name Type Description
None None

Control returns to the caller while the setting is active.

fails_fast(error)

Returns whether a normalized failure stops the run only because fail fast is active.

Parameters:

Name Type Description Default
error BaseException

The normalized failure.

required

Returns:

Name Type Description
bool bool

True when fail fast is active and the failure would otherwise fail only its test case.

failure_scope(error)

Returns what a normalized failure makes invalid.

The JUDGE_CALL, TEST_CASE and JUDGE_SETUP classes are listed. Every other failure stops the run, so a new error class is fatal until it is classified here. A provider's invalid-request error is JUDGE_CALL: the same HTTP 400 reports a wrong parameter and a content-filter rejection of one test case, so it must not stop the run.

Parameters:

Name Type Description Default
error BaseException

The normalized failure.

required

Returns:

Name Type Description
FailureScope FailureScope

The scope of the failure's class. For a summary of judge failures, see _summary_scope.

is_transient_error(error)

Returns whether another attempt of the same idempotent request can succeed after this failure.

Parameters:

Name Type Description Default
error BaseException

The normalized failure of one attempt.

required

Returns:

Name Type Description
bool bool

True when the error's recovery says it is retryable, for example connection failures, throttling, HTTP 500, 502, 503 and 504, and judge or provider timeouts. False otherwise.

retry_delay(attempt, config, error)

Computes the wait before the next attempt, honoring a reported retry_after_seconds up to max_delay.

The loops never call it for a reported wait longer than max_delay: they raise instead of retrying early.

Parameters:

Name Type Description Default
attempt int

The failed attempt number (0-based).

required
config RetryConfig

Backoff settings.

required
error BaseException

The normalized failure of the attempt.

required

Returns:

Name Type Description
float float

Seconds to wait.

retry_transient_async(operation, config, *, label, context, should_retry=is_transient_error) async

Runs an async operation with a bounded attempt budget and an optional overall deadline.

The operation runs at most config.max_retries + 1 times. A failure is retried only when it is a BaseEvalError accepted by should_retry. config.timeout, when set, bounds every attempt and wait together and is never restarted. config.retry_on_exceptions and config.non_retryable_exceptions are not used.

Parameters:

Name Type Description Default
operation Callable[[], Awaitable[T]]

One attempt. It must raise normalized errors.

required
config RetryConfig

Attempt budget, backoff and overall deadline.

required
label str

Static name of the operation for retry logs.

required
context ErrorContext

Where the operation runs, attached to a deadline error.

required
should_retry Callable[[BaseException], bool]

Decides whether a normalized failure is retried. Defaults to is_transient_error.

is_transient_error

Returns:

Name Type Description
T T

The result of the first successful attempt.

Raises:

Type Description
EvalTimeoutError

If the overall deadline expires.

Exception

The failure of the last attempt, the first failure that is not retried, or a failure whose reported retry_after_seconds exceeds config.max_delay.

retry_transient_sync(operation, *, total_attempts, config, label, should_retry=is_transient_error)

Runs a blocking operation with a bounded attempt budget.

Parameters:

Name Type Description Default
operation Callable[[], T]

One attempt. It must raise normalized errors.

required
total_attempts int

Maximum number of attempts, at least 1.

required
config RetryConfig

Backoff settings; its max_retries, timeout and exception tuples are not used.

required
label str

Static name of the operation for retry logs.

required
should_retry Callable[[BaseException], bool]

Decides whether a normalized failure is retried. Defaults to is_transient_error.

is_transient_error

Returns:

Name Type Description
T T

The result of the first successful attempt.

Raises:

Type Description
Exception

The failure of the last attempt, the first failure that is not retried, or a failure whose reported retry_after_seconds exceeds config.max_delay.

stops_run(error)

Returns whether a normalized failure stops the run instead of failing its test case.

Parameters:

Name Type Description Default
error BaseException

The normalized failure.

required

Returns:

Name Type Description
bool bool

True when the failure scope is JUDGE_SETUP or RUN, or when fail fast is active.