nemoguardrails.http.retry

View as Markdown

Bounded retry policies and a transport-neutral retrying HTTP client.

Module Contents

Classes

NameDescription
RetryPolicyConfigure bounded retries for an HTTP client.
RetryingHTTPClientApply a retry policy around another transport-neutral HTTP client.

Functions

NameDescription
_header-

API

class nemoguardrails.http.retry.RetryPolicy(
max_attempts: int = 3,
retryable_methods: frozenset[str] = (lambda: frozenset({'DELETE...,
retryable_status_codes: frozenset[int] = (lambda: frozenset({408, 40...,
initial_delay: float = 0.5,
max_delay: float = 8.0,
max_retry_after: float = 60.0,
retry_transport_errors: bool = True,
honor_retry_override_header: bool = False,
clamp_retry_after: bool = False
)
Dataclass

Configure bounded retries for an HTTP client.

max_attempts includes the initial request. Methods are normalized to uppercase, and POST is intentionally absent from the safe default set. Retry-After values are honored only when they fall within max_retry_after unless clamping is explicitly enabled. Vendor override headers are ignored unless honor_retry_override_header is enabled.

clamp_retry_after
bool = False
honor_retry_override_header
bool = False
initial_delay
float = 0.5
max_attempts
int = 3
max_delay
float = 8.0
max_retry_after
float = 60.0
retry_transport_errors
bool = True
retryable_methods
frozenset[str]
retryable_status_codes
frozenset[int]
nemoguardrails.http.retry.RetryPolicy.__post_init__() -> None
nemoguardrails.http.retry.RetryPolicy.can_retry_method(
method: str
) -> bool

Return whether the policy permits retrying the HTTP method.

nemoguardrails.http.retry.RetryPolicy.retry_after(
response: nemoguardrails.http.types.HTTPResponse,
now: datetime.datetime
) -> float | None

Return a usable Retry-After delay in seconds.

Both delta-seconds and HTTP-date values are supported. Invalid or out-of-policy values return None so the client uses exponential backoff instead.

nemoguardrails.http.retry.RetryPolicy.should_retry(
response: nemoguardrails.http.types.HTTPResponse
) -> bool

Return whether a response status or opted-in override requests a retry.

class nemoguardrails.http.retry.RetryingHTTPClient(
client: nemoguardrails.http.client.HTTPClient,
policy: nemoguardrails.http.retry.RetryPolicy | None = None,
sleep: collections.abc.Callable[[float], collections.abc.Awaitable[None]] = asyncio.sleep,
random_value: collections.abc.Callable[[], float] = random.random,
now: collections.abc.Callable[[], datetime.datetime] | None = None
)

Apply a retry policy around another transport-neutral HTTP client.

Closing this wrapper closes the wrapped client only when it implements :class:ClosableHTTPClient.

_now
= now or (lambda: datetime.now(timezone.utc))
_policy
= policy or RetryPolicy()
wrapped_client
HTTPClient

Return the underlying client.

async

Return this client from an asynchronous context manager.

nemoguardrails.http.retry.RetryingHTTPClient.__aexit__(
exc_type: object,
exc_value: object,
traceback: object
) -> None
async

Close wrapped resources when leaving an asynchronous context.

nemoguardrails.http.retry.RetryingHTTPClient._backoff(
retries: int
) -> float

Apply the current retry settings to another client.

nemoguardrails.http.retry.RetryingHTTPClient.close() -> None
async

Close the wrapped closable client at most once.

nemoguardrails.http.retry.RetryingHTTPClient.request(
method: str,
url: str,
headers: typing.Mapping[str, str] | None = None,
params: typing.Mapping[str, typing.Any] | None = None,
json: typing.Any = None,
content: bytes | str | None = None,
timeout: float | None = None
) -> nemoguardrails.http.types.HTTPResponse
async

Send a request and retry eligible failures within policy bounds.

The returned response includes retry_count in its extensions. Transport errors also expose their completed retry count before being re-raised.

nemoguardrails.http.retry._header(
headers: typing.Mapping[str, str],
name: str
) -> str | None