|
5 | 5 | exponential backoff and jitter. It uses tenacity for retry handling |
6 | 6 | and integrates with httpx transports. |
7 | 7 |
|
| 8 | +The retry logic automatically respects the `Retry-After` or `x-retry-after` |
| 9 | +HTTP headers when present in error responses. If the header specifies a wait |
| 10 | +time, that value is used (capped at max_delay). Otherwise, exponential backoff |
| 11 | +with jitter is applied. |
| 12 | +
|
8 | 13 | Example: |
9 | 14 | >>> from uipath_llm_client.utils.retry import RetryableHTTPTransport, RetryConfig |
10 | 15 | >>> |
|
32 | 37 | from httpx import AsyncHTTPTransport, HTTPTransport, Request, Response |
33 | 38 | from tenacity import ( |
34 | 39 | AsyncRetrying, |
| 40 | + RetryCallState, |
35 | 41 | Retrying, |
36 | 42 | before_sleep_log, |
37 | 43 | retry_if_exception_type, |
38 | 44 | stop_after_attempt, |
39 | 45 | wait_exponential_jitter, |
40 | 46 | ) |
| 47 | +from tenacity.wait import wait_base |
41 | 48 | from typing_extensions import TypedDict |
42 | 49 |
|
43 | 50 | from uipath_llm_client.utils.exceptions import UiPathAPIError, UiPathRateLimitError |
|
57 | 64 | _DEFAULT_JITTER: float = 1.0 |
58 | 65 |
|
59 | 66 |
|
| 67 | +class wait_retry_after_with_fallback(wait_base): |
| 68 | + """Custom wait strategy that uses Retry-After header when available. |
| 69 | +
|
| 70 | + This wait strategy checks if the exception has a retry_after attribute |
| 71 | + (from the Retry-After or x-retry-after HTTP headers) and uses that value. |
| 72 | + If not available, falls back to exponential backoff with jitter. |
| 73 | +
|
| 74 | + Attributes: |
| 75 | + fallback_wait: The fallback wait strategy (exponential backoff with jitter). |
| 76 | + max_delay: Maximum delay in seconds (caps retry-after values). |
| 77 | + """ |
| 78 | + |
| 79 | + def __init__( |
| 80 | + self, |
| 81 | + *, |
| 82 | + initial: float, |
| 83 | + max: float, |
| 84 | + exp_base: float, |
| 85 | + jitter: float, |
| 86 | + ) -> None: |
| 87 | + """Initialize the wait strategy. |
| 88 | +
|
| 89 | + Args: |
| 90 | + initial: Initial delay for exponential backoff. |
| 91 | + max: Maximum delay in seconds (also caps retry-after values). |
| 92 | + exp_base: Exponential backoff base multiplier. |
| 93 | + jitter: Random jitter to add to delays. |
| 94 | + """ |
| 95 | + self.fallback_wait = wait_exponential_jitter( |
| 96 | + initial=initial, |
| 97 | + max=max, |
| 98 | + exp_base=exp_base, |
| 99 | + jitter=jitter, |
| 100 | + ) |
| 101 | + self.max_delay = max |
| 102 | + |
| 103 | + def __call__(self, retry_state: RetryCallState) -> float: |
| 104 | + """Calculate the wait time for the next retry. |
| 105 | +
|
| 106 | + Args: |
| 107 | + retry_state: The current retry state from tenacity. |
| 108 | +
|
| 109 | + Returns: |
| 110 | + The number of seconds to wait before the next retry. |
| 111 | + """ |
| 112 | + # Check if we have a rate limit exception with retry_after |
| 113 | + if retry_state.outcome is not None and retry_state.outcome.failed: |
| 114 | + exception = retry_state.outcome.exception() |
| 115 | + if isinstance(exception, UiPathRateLimitError) and exception.retry_after is not None: |
| 116 | + # Use retry-after value, but cap at max_delay |
| 117 | + return min(exception.retry_after, self.max_delay) |
| 118 | + |
| 119 | + # Fall back to exponential backoff with jitter |
| 120 | + return self.fallback_wait(retry_state) |
| 121 | + |
| 122 | + |
60 | 123 | class RetryConfig(TypedDict): |
61 | 124 | """Configuration for retry behavior on failed requests. |
62 | 125 |
|
@@ -126,7 +189,7 @@ def _build_retryer( |
126 | 189 | retryer_class = AsyncRetrying if async_mode else Retrying |
127 | 190 | return retryer_class( |
128 | 191 | stop=stop_after_attempt(max_retries), |
129 | | - wait=wait_exponential_jitter( |
| 192 | + wait=wait_retry_after_with_fallback( |
130 | 193 | initial=initial_delay, |
131 | 194 | max=max_delay, |
132 | 195 | exp_base=exp_base, |
|
0 commit comments