• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

deepset-ai / haystack / 14175682842

31 Mar 2025 03:42PM UTC coverage: 90.241% (+0.05%) from 90.191%
14175682842

Pull #9149

github

web-flow
Merge eb93659cc into adc3dfc5d
Pull Request #9149: chore: use `deserialize_chatgenerator_inplace` utility function in `Agent`

10246 of 11354 relevant lines covered (90.24%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
haystack/utils/requests_utils.py
1
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
2
#
3
# SPDX-License-Identifier: Apache-2.0
4

5
import logging
×
6
from typing import List, Optional
×
7

8
import requests
×
9
from tenacity import after_log, before_log, retry, retry_if_exception_type, stop_after_attempt, wait_exponential
×
10

11
logger = logging.getLogger(__file__)
×
12

13

14
def request_with_retry(
×
15
    attempts: int = 3, status_codes_to_retry: Optional[List[int]] = None, **kwargs
16
) -> requests.Response:
17
    """
18
    Executes an HTTP request with a configurable exponential backoff retry on failures.
19

20
    Usage example:
21
    ```python
22
    from haystack.utils import request_with_retry
23

24
    # Sending an HTTP request with default retry configs
25
    res = request_with_retry(method="GET", url="https://example.com")
26

27
    # Sending an HTTP request with custom number of attempts
28
    res = request_with_retry(method="GET", url="https://example.com", attempts=10)
29

30
    # Sending an HTTP request with custom HTTP codes to retry
31
    res = request_with_retry(method="GET", url="https://example.com", status_codes_to_retry=[408, 503])
32

33
    # Sending an HTTP request with custom timeout in seconds
34
    res = request_with_retry(method="GET", url="https://example.com", timeout=5)
35

36
    # Sending an HTTP request with custom authorization handling
37
    class CustomAuth(requests.auth.AuthBase):
38
        def __call__(self, r):
39
            r.headers["authorization"] = "Basic <my_token_here>"
40
            return r
41

42
    res = request_with_retry(method="GET", url="https://example.com", auth=CustomAuth())
43

44
    # All of the above combined
45
    res = request_with_retry(
46
        method="GET",
47
        url="https://example.com",
48
        auth=CustomAuth(),
49
        attempts=10,
50
        status_codes_to_retry=[408, 503],
51
        timeout=5
52
    )
53

54
    # Sending a POST request
55
    res = request_with_retry(method="POST", url="https://example.com", data={"key": "value"}, attempts=10)
56

57
    # Retry all 5xx status codes
58
    res = request_with_retry(method="GET", url="https://example.com", status_codes_to_retry=list(range(500, 600)))
59
    ```
60

61
    :param attempts:
62
        Maximum number of attempts to retry the request.
63
    :param status_codes_to_retry:
64
        List of HTTP status codes that will trigger a retry.
65
        When param is `None`, HTTP 408, 418, 429 and 503 will be retried.
66
    :param kwargs:
67
        Optional arguments that `request` accepts.
68
    :returns:
69
        The `Response` object.
70
    """
71

72
    if status_codes_to_retry is None:
×
73
        status_codes_to_retry = [408, 418, 429, 503]
×
74

75
    @retry(
×
76
        reraise=True,
77
        wait=wait_exponential(),
78
        retry=retry_if_exception_type((requests.HTTPError, TimeoutError)),
79
        stop=stop_after_attempt(attempts),
80
        before=before_log(logger, logging.DEBUG),
81
        after=after_log(logger, logging.DEBUG),
82
    )
83
    def run():
×
84
        timeout = kwargs.pop("timeout", 10)
×
85
        res = requests.request(**kwargs, timeout=timeout)
×
86

87
        if res.status_code in status_codes_to_retry:
×
88
            # We raise only for the status codes that must trigger a retry
89
            res.raise_for_status()
×
90

91
        return res
×
92

93
    res = run()
×
94
    # We raise here too in case the request failed with a status code that
95
    # won't trigger a retry, this way the call will still cause an explicit exception
96
    res.raise_for_status()
×
97
    return res
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc