| 746 | return timeout if timeout >= 0 else 0 |
| 747 | |
| 748 | def _should_retry(self, response: httpx.Response) -> bool: |
| 749 | # Note: this is not a standard header |
| 750 | should_retry_header = response.headers.get("x-should-retry") |
| 751 | |
| 752 | # If the server explicitly says whether or not to retry, obey. |
| 753 | if should_retry_header == "true": |
| 754 | log.debug("Retrying as header `x-should-retry` is set to `true`") |
| 755 | return True |
| 756 | if should_retry_header == "false": |
| 757 | log.debug("Not retrying as header `x-should-retry` is set to `false`") |
| 758 | return False |
| 759 | |
| 760 | # Retry on request timeouts. |
| 761 | if response.status_code == 408: |
| 762 | log.debug("Retrying due to status code %i", response.status_code) |
| 763 | return True |
| 764 | |
| 765 | # Retry on lock timeouts. |
| 766 | if response.status_code == 409: |
| 767 | log.debug("Retrying due to status code %i", response.status_code) |
| 768 | return True |
| 769 | |
| 770 | # Retry on rate limits. |
| 771 | if response.status_code == 429: |
| 772 | log.debug("Retrying due to status code %i", response.status_code) |
| 773 | return True |
| 774 | |
| 775 | # Retry internal errors. |
| 776 | if response.status_code >= 500: |
| 777 | log.debug("Retrying due to status code %i", response.status_code) |
| 778 | return True |
| 779 | |
| 780 | log.debug("Not retrying") |
| 781 | return False |
| 782 | |
| 783 | def _idempotency_key(self) -> str: |
| 784 | return f"stainless-python-retry-{uuid.uuid4()}" |