| 793 | return timeout if timeout >= 0 else 0 |
| 794 | |
| 795 | def _should_retry(self, response: httpx.Response) -> bool: |
| 796 | # Note: this is not a standard header |
| 797 | should_retry_header = response.headers.get("x-should-retry") |
| 798 | |
| 799 | # If the server explicitly says whether or not to retry, obey. |
| 800 | if should_retry_header == "true": |
| 801 | log.debug("Retrying as header `x-should-retry` is set to `true`") |
| 802 | return True |
| 803 | if should_retry_header == "false": |
| 804 | log.debug("Not retrying as header `x-should-retry` is set to `false`") |
| 805 | return False |
| 806 | |
| 807 | # Retry on request timeouts. |
| 808 | if response.status_code == 408: |
| 809 | log.debug("Retrying due to status code %i", response.status_code) |
| 810 | return True |
| 811 | |
| 812 | # Retry on lock timeouts. |
| 813 | if response.status_code == 409: |
| 814 | log.debug("Retrying due to status code %i", response.status_code) |
| 815 | return True |
| 816 | |
| 817 | # Retry on rate limits. |
| 818 | if response.status_code == 429: |
| 819 | log.debug("Retrying due to status code %i", response.status_code) |
| 820 | return True |
| 821 | |
| 822 | # Retry internal errors. |
| 823 | if response.status_code >= 500: |
| 824 | log.debug("Retrying due to status code %i", response.status_code) |
| 825 | return True |
| 826 | |
| 827 | log.debug("Not retrying") |
| 828 | return False |
| 829 | |
| 830 | def _idempotency_key(self) -> str: |
| 831 | return f"stainless-python-retry-{uuid.uuid4()}" |