Skip to content

honeyhive.utils.retry

Retry utilities for HTTP requests.

BackoffStrategy dataclass

Backoff strategy for retries.

Source code in src/honeyhive/utils/retry.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
@dataclass
class BackoffStrategy:
    """Backoff strategy for retries."""

    initial_delay: float = 1.0
    max_delay: float = 60.0
    multiplier: float = 2.0
    jitter: float = 0.1

    def get_delay(self, attempt: int) -> float:
        """Calculate delay for the given attempt."""
        if attempt == 0:
            return 0

        # Exponential backoff with jitter
        delay = min(
            self.initial_delay * (self.multiplier ** (attempt - 1)), self.max_delay
        )

        # Add jitter to prevent thundering herd
        if self.jitter > 0:
            jitter_amount = delay * self.jitter
            delay += random.uniform(-jitter_amount, jitter_amount)

        return max(0, delay)

initial_delay class-attribute instance-attribute

initial_delay: float = 1.0

max_delay class-attribute instance-attribute

max_delay: float = 60.0

multiplier class-attribute instance-attribute

multiplier: float = 2.0

jitter class-attribute instance-attribute

jitter: float = 0.1

get_delay

get_delay(attempt: int) -> float

Calculate delay for the given attempt.

Source code in src/honeyhive/utils/retry.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def get_delay(self, attempt: int) -> float:
    """Calculate delay for the given attempt."""
    if attempt == 0:
        return 0

    # Exponential backoff with jitter
    delay = min(
        self.initial_delay * (self.multiplier ** (attempt - 1)), self.max_delay
    )

    # Add jitter to prevent thundering herd
    if self.jitter > 0:
        jitter_amount = delay * self.jitter
        delay += random.uniform(-jitter_amount, jitter_amount)

    return max(0, delay)

RetryConfig dataclass

Configuration for retry behavior.

Source code in src/honeyhive/utils/retry.py
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
@dataclass
class RetryConfig:
    """Configuration for retry behavior."""

    strategy: str = "exponential"  # "exponential", "linear", "constant"
    backoff_strategy: Optional[BackoffStrategy] = None
    max_retries: int = 3
    retry_on_status_codes: Optional[set] = None

    def __post_init__(self) -> None:
        """Initialize default values."""
        if self.backoff_strategy is None:
            self.backoff_strategy = BackoffStrategy()

        if self.retry_on_status_codes is None:
            self.retry_on_status_codes = {408, 429, 500, 502, 503, 504}

    @classmethod
    def default(cls) -> "RetryConfig":
        """Create a default retry configuration."""
        return cls()

    @classmethod
    def exponential(
        cls,
        initial_delay: float = 1.0,
        max_delay: float = 60.0,
        multiplier: float = 2.0,
        max_retries: int = 3,
    ) -> "RetryConfig":
        """Create an exponential backoff retry configuration."""
        backoff = BackoffStrategy(
            initial_delay=initial_delay,
            max_delay=max_delay,
            multiplier=multiplier,
        )
        return cls(
            strategy="exponential",
            backoff_strategy=backoff,
            max_retries=max_retries,
        )

    @classmethod
    def linear(
        cls,
        delay: float = 1.0,
        max_retries: int = 3,
    ) -> "RetryConfig":
        """Create a linear backoff retry configuration."""
        backoff = BackoffStrategy(
            initial_delay=delay,
            max_delay=delay,
            multiplier=1.0,
        )
        return cls(
            strategy="linear",
            backoff_strategy=backoff,
            max_retries=max_retries,
        )

    @classmethod
    def constant(
        cls,
        delay: float = 1.0,
        max_retries: int = 3,
    ) -> "RetryConfig":
        """Create a constant delay retry configuration."""
        backoff = BackoffStrategy(
            initial_delay=delay,
            max_delay=delay,
            multiplier=1.0,
        )
        return cls(
            strategy="constant",
            backoff_strategy=backoff,
            max_retries=max_retries,
        )

    def should_retry(self, response: httpx.Response) -> bool:
        """Determine if a response should be retried."""
        # Check status code
        if (
            self.retry_on_status_codes
            and response.status_code in self.retry_on_status_codes
        ):
            return True

        # Check for connection errors
        if response.status_code == 0:  # Connection error
            return True

        return False

    def should_retry_exception(self, exc: Exception) -> bool:
        """Determine if an exception should be retried."""
        # Retry on connection errors
        if isinstance(
            exc,
            (
                httpx.ConnectError,
                httpx.ConnectTimeout,
                httpx.ReadTimeout,
                httpx.WriteTimeout,
                httpx.PoolTimeout,
            ),
        ):
            return True

        # Retry on HTTP errors that are retryable
        if isinstance(exc, httpx.HTTPStatusError):
            return bool(
                self.retry_on_status_codes
                and exc.response.status_code in self.retry_on_status_codes
            )

        return False

    def _raise_for_failure(
        self,
        operation: str,
        last_response: Optional[httpx.Response],
        last_exception: Optional[Exception],
    ) -> NoReturn:
        """Raise an appropriate error after all retries are exhausted.

        Args:
            operation: Name of the operation that failed (for error messages).
            last_response: The last HTTP response received (if any).
            last_exception: The last exception caught (if any).
        """
        if last_response is not None:
            raise APIError(
                f"{operation} failed after {self.max_retries + 1} attempts "
                f"with status code: {last_response.status_code}, "
                f"response: {last_response.text}",
                error_response=ErrorResponse(
                    error_type="APIError",
                    error_message=last_response.text,
                    status_code=last_response.status_code,
                ),
            )
        if last_exception is not None:
            raise last_exception
        # Should never happen — guard against unbound response after the loop.
        raise RuntimeError(f"{operation} retry loop exited unexpectedly")

    @staticmethod
    def _raise_non_retryable(operation: str, response: httpx.Response) -> NoReturn:
        """Raise an APIError for a non-retryable HTTP error.

        Args:
            operation: Name of the operation that failed.
            response: The failed HTTP response.
        """
        raise APIError(
            f"{operation} failed with status code: {response.status_code}, "
            f"response: {response.text}",
            error_response=ErrorResponse(
                error_type="APIError",
                error_message=response.text,
                status_code=response.status_code,
            ),
        )

    def execute(
        self,
        request_fn: Callable[[], httpx.Response],
        operation: str = "request",
    ) -> httpx.Response:
        """Execute a sync HTTP request with retries.

        Args:
            request_fn: A callable that performs the HTTP request and returns
                an httpx.Response.
            operation: Name of the operation (used in error messages).

        Returns:
            The successful httpx.Response.

        Raises:
            APIError: On non-retryable errors or after all retries exhausted.
        """
        last_exception: Optional[Exception] = None
        last_response: Optional[httpx.Response] = None

        for attempt in range(self.max_retries + 1):
            try:
                response = request_fn()

                if response.status_code == 200:
                    return response

                # Check if we should retry this status code
                if self.should_retry(response):
                    last_response = response
                    if attempt < self.max_retries:
                        delay = self.backoff_strategy.get_delay(attempt + 1)
                        time.sleep(delay)
                        continue
                    # Last attempt exhausted — break to _raise_for_failure
                    break

                # Non-retryable error — raise immediately
                self._raise_non_retryable(operation, response)

            except httpx.HTTPError as e:
                if self.should_retry_exception(e):
                    last_exception = e
                    if attempt < self.max_retries:
                        delay = self.backoff_strategy.get_delay(attempt + 1)
                        time.sleep(delay)
                        continue
                    # Last attempt exhausted — break to _raise_for_failure
                    break
                raise

        # All retries exhausted (loop finished without return/raise)
        self._raise_for_failure(operation, last_response, last_exception)

    async def execute_async(
        self,
        request_fn: Callable[[], Awaitable[httpx.Response]],
        operation: str = "request",
    ) -> httpx.Response:
        """Execute an async HTTP request with retries.

        Same contract as execute(), but awaits the request_fn and uses
        asyncio.sleep for backoff delays.

        Args:
            request_fn: An async callable that performs the HTTP request and
                returns an httpx.Response.
            operation: Name of the operation (used in error messages).

        Returns:
            The successful httpx.Response.

        Raises:
            APIError: On non-retryable errors or after all retries exhausted.
        """
        last_exception: Optional[Exception] = None
        last_response: Optional[httpx.Response] = None

        for attempt in range(self.max_retries + 1):
            try:
                response = await request_fn()

                if response.status_code == 200:
                    return response

                # Check if we should retry this status code
                if self.should_retry(response):
                    last_response = response
                    if attempt < self.max_retries:
                        delay = self.backoff_strategy.get_delay(attempt + 1)
                        await asyncio.sleep(delay)
                        continue
                    # Last attempt exhausted — break to _raise_for_failure
                    break

                # Non-retryable error — raise immediately
                self._raise_non_retryable(operation, response)

            except httpx.HTTPError as e:
                if self.should_retry_exception(e):
                    last_exception = e
                    if attempt < self.max_retries:
                        delay = self.backoff_strategy.get_delay(attempt + 1)
                        await asyncio.sleep(delay)
                        continue
                    # Last attempt exhausted — break to _raise_for_failure
                    break
                raise

        # All retries exhausted (loop finished without return/raise)
        self._raise_for_failure(operation, last_response, last_exception)

strategy class-attribute instance-attribute

strategy: str = 'exponential'

backoff_strategy class-attribute instance-attribute

backoff_strategy: Optional[BackoffStrategy] = None

max_retries class-attribute instance-attribute

max_retries: int = 3

retry_on_status_codes class-attribute instance-attribute

retry_on_status_codes: Optional[set] = None

default classmethod

default() -> RetryConfig

Create a default retry configuration.

Source code in src/honeyhive/utils/retry.py
60
61
62
63
@classmethod
def default(cls) -> "RetryConfig":
    """Create a default retry configuration."""
    return cls()

exponential classmethod

exponential(
    initial_delay: float = 1.0,
    max_delay: float = 60.0,
    multiplier: float = 2.0,
    max_retries: int = 3,
) -> RetryConfig

Create an exponential backoff retry configuration.

Source code in src/honeyhive/utils/retry.py
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
@classmethod
def exponential(
    cls,
    initial_delay: float = 1.0,
    max_delay: float = 60.0,
    multiplier: float = 2.0,
    max_retries: int = 3,
) -> "RetryConfig":
    """Create an exponential backoff retry configuration."""
    backoff = BackoffStrategy(
        initial_delay=initial_delay,
        max_delay=max_delay,
        multiplier=multiplier,
    )
    return cls(
        strategy="exponential",
        backoff_strategy=backoff,
        max_retries=max_retries,
    )

linear classmethod

linear(
    delay: float = 1.0, max_retries: int = 3
) -> RetryConfig

Create a linear backoff retry configuration.

Source code in src/honeyhive/utils/retry.py
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
@classmethod
def linear(
    cls,
    delay: float = 1.0,
    max_retries: int = 3,
) -> "RetryConfig":
    """Create a linear backoff retry configuration."""
    backoff = BackoffStrategy(
        initial_delay=delay,
        max_delay=delay,
        multiplier=1.0,
    )
    return cls(
        strategy="linear",
        backoff_strategy=backoff,
        max_retries=max_retries,
    )

constant classmethod

constant(
    delay: float = 1.0, max_retries: int = 3
) -> RetryConfig

Create a constant delay retry configuration.

Source code in src/honeyhive/utils/retry.py
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
@classmethod
def constant(
    cls,
    delay: float = 1.0,
    max_retries: int = 3,
) -> "RetryConfig":
    """Create a constant delay retry configuration."""
    backoff = BackoffStrategy(
        initial_delay=delay,
        max_delay=delay,
        multiplier=1.0,
    )
    return cls(
        strategy="constant",
        backoff_strategy=backoff,
        max_retries=max_retries,
    )

should_retry

should_retry(response: Response) -> bool

Determine if a response should be retried.

Source code in src/honeyhive/utils/retry.py
121
122
123
124
125
126
127
128
129
130
131
132
133
134
def should_retry(self, response: httpx.Response) -> bool:
    """Determine if a response should be retried."""
    # Check status code
    if (
        self.retry_on_status_codes
        and response.status_code in self.retry_on_status_codes
    ):
        return True

    # Check for connection errors
    if response.status_code == 0:  # Connection error
        return True

    return False

should_retry_exception

should_retry_exception(exc: Exception) -> bool

Determine if an exception should be retried.

Source code in src/honeyhive/utils/retry.py
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
def should_retry_exception(self, exc: Exception) -> bool:
    """Determine if an exception should be retried."""
    # Retry on connection errors
    if isinstance(
        exc,
        (
            httpx.ConnectError,
            httpx.ConnectTimeout,
            httpx.ReadTimeout,
            httpx.WriteTimeout,
            httpx.PoolTimeout,
        ),
    ):
        return True

    # Retry on HTTP errors that are retryable
    if isinstance(exc, httpx.HTTPStatusError):
        return bool(
            self.retry_on_status_codes
            and exc.response.status_code in self.retry_on_status_codes
        )

    return False

execute

execute(
    request_fn: Callable[[], Response],
    operation: str = "request",
) -> Response

Execute a sync HTTP request with retries.

Parameters:

Name Type Description Default
request_fn Callable[[], Response]

A callable that performs the HTTP request and returns an httpx.Response.

required
operation str

Name of the operation (used in error messages).

'request'

Returns:

Type Description
Response

The successful httpx.Response.

Raises:

Type Description
APIError

On non-retryable errors or after all retries exhausted.

Source code in src/honeyhive/utils/retry.py
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
def execute(
    self,
    request_fn: Callable[[], httpx.Response],
    operation: str = "request",
) -> httpx.Response:
    """Execute a sync HTTP request with retries.

    Args:
        request_fn: A callable that performs the HTTP request and returns
            an httpx.Response.
        operation: Name of the operation (used in error messages).

    Returns:
        The successful httpx.Response.

    Raises:
        APIError: On non-retryable errors or after all retries exhausted.
    """
    last_exception: Optional[Exception] = None
    last_response: Optional[httpx.Response] = None

    for attempt in range(self.max_retries + 1):
        try:
            response = request_fn()

            if response.status_code == 200:
                return response

            # Check if we should retry this status code
            if self.should_retry(response):
                last_response = response
                if attempt < self.max_retries:
                    delay = self.backoff_strategy.get_delay(attempt + 1)
                    time.sleep(delay)
                    continue
                # Last attempt exhausted — break to _raise_for_failure
                break

            # Non-retryable error — raise immediately
            self._raise_non_retryable(operation, response)

        except httpx.HTTPError as e:
            if self.should_retry_exception(e):
                last_exception = e
                if attempt < self.max_retries:
                    delay = self.backoff_strategy.get_delay(attempt + 1)
                    time.sleep(delay)
                    continue
                # Last attempt exhausted — break to _raise_for_failure
                break
            raise

    # All retries exhausted (loop finished without return/raise)
    self._raise_for_failure(operation, last_response, last_exception)

execute_async async

execute_async(
    request_fn: Callable[[], Awaitable[Response]],
    operation: str = "request",
) -> Response

Execute an async HTTP request with retries.

Same contract as execute(), but awaits the request_fn and uses asyncio.sleep for backoff delays.

Parameters:

Name Type Description Default
request_fn Callable[[], Awaitable[Response]]

An async callable that performs the HTTP request and returns an httpx.Response.

required
operation str

Name of the operation (used in error messages).

'request'

Returns:

Type Description
Response

The successful httpx.Response.

Raises:

Type Description
APIError

On non-retryable errors or after all retries exhausted.

Source code in src/honeyhive/utils/retry.py
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
async def execute_async(
    self,
    request_fn: Callable[[], Awaitable[httpx.Response]],
    operation: str = "request",
) -> httpx.Response:
    """Execute an async HTTP request with retries.

    Same contract as execute(), but awaits the request_fn and uses
    asyncio.sleep for backoff delays.

    Args:
        request_fn: An async callable that performs the HTTP request and
            returns an httpx.Response.
        operation: Name of the operation (used in error messages).

    Returns:
        The successful httpx.Response.

    Raises:
        APIError: On non-retryable errors or after all retries exhausted.
    """
    last_exception: Optional[Exception] = None
    last_response: Optional[httpx.Response] = None

    for attempt in range(self.max_retries + 1):
        try:
            response = await request_fn()

            if response.status_code == 200:
                return response

            # Check if we should retry this status code
            if self.should_retry(response):
                last_response = response
                if attempt < self.max_retries:
                    delay = self.backoff_strategy.get_delay(attempt + 1)
                    await asyncio.sleep(delay)
                    continue
                # Last attempt exhausted — break to _raise_for_failure
                break

            # Non-retryable error — raise immediately
            self._raise_non_retryable(operation, response)

        except httpx.HTTPError as e:
            if self.should_retry_exception(e):
                last_exception = e
                if attempt < self.max_retries:
                    delay = self.backoff_strategy.get_delay(attempt + 1)
                    await asyncio.sleep(delay)
                    continue
                # Last attempt exhausted — break to _raise_for_failure
                break
            raise

    # All retries exhausted (loop finished without return/raise)
    self._raise_for_failure(operation, last_response, last_exception)