From 08b051f412bf9f96687d81151e64c82aa02a42c6 Mon Sep 17 00:00:00 2001 From: Wei Zang Date: Wed, 10 Jul 2024 21:08:04 +0800 Subject: [PATCH] feat(bedrock): retry request on error 424 --- src/anthropic/__init__.py | 2 ++ src/anthropic/_base_client.py | 5 +++++ src/anthropic/_client.py | 6 ++++++ src/anthropic/_exceptions.py | 5 +++++ src/anthropic/lib/bedrock/_client.py | 3 +++ 5 files changed, 21 insertions(+) diff --git a/src/anthropic/__init__.py b/src/anthropic/__init__.py index bf3fef38..1b0e2861 100644 --- a/src/anthropic/__init__.py +++ b/src/anthropic/__init__.py @@ -38,6 +38,7 @@ InternalServerError, PermissionDeniedError, UnprocessableEntityError, + FailedDependencyError, APIResponseValidationError, ) from ._base_client import DefaultHttpxClient, DefaultAsyncHttpxClient @@ -64,6 +65,7 @@ "NotFoundError", "ConflictError", "UnprocessableEntityError", + "FailedDependencyError", "RateLimitError", "InternalServerError", "Timeout", diff --git a/src/anthropic/_base_client.py b/src/anthropic/_base_client.py index d9b6c23d..2435a4ae 100644 --- a/src/anthropic/_base_client.py +++ b/src/anthropic/_base_client.py @@ -708,6 +708,11 @@ def _should_retry(self, response: httpx.Response) -> bool: log.debug("Retrying due to status code %i", response.status_code) return True + # Retry on failed dependencies. + if response.status_code == 424: + log.debug("Retrying due to status code %i", response.status_code) + return True + # Retry on rate limits. if response.status_code == 429: log.debug("Retrying due to status code %i", response.status_code) diff --git a/src/anthropic/_client.py b/src/anthropic/_client.py index ac148940..7a61a1d5 100644 --- a/src/anthropic/_client.py +++ b/src/anthropic/_client.py @@ -309,6 +309,9 @@ def _make_status_error( if response.status_code == 422: return _exceptions.UnprocessableEntityError(err_msg, response=response, body=body) + if response.status_code == 424: + return _exceptions.FailedDependencyError(err_msg, response=response, body=body) + if response.status_code == 429: return _exceptions.RateLimitError(err_msg, response=response, body=body) @@ -571,6 +574,9 @@ def _make_status_error( if response.status_code == 422: return _exceptions.UnprocessableEntityError(err_msg, response=response, body=body) + if response.status_code == 424: + return _exceptions.FailedDependencyError(err_msg, response=response, body=body) + if response.status_code == 429: return _exceptions.RateLimitError(err_msg, response=response, body=body) diff --git a/src/anthropic/_exceptions.py b/src/anthropic/_exceptions.py index 28583feb..0eccbba5 100644 --- a/src/anthropic/_exceptions.py +++ b/src/anthropic/_exceptions.py @@ -13,6 +13,7 @@ "NotFoundError", "ConflictError", "UnprocessableEntityError", + "FailedDependencyError", "RateLimitError", "InternalServerError", ] @@ -100,6 +101,10 @@ class UnprocessableEntityError(APIStatusError): status_code: Literal[422] = 422 # pyright: ignore[reportIncompatibleVariableOverride] +class FailedDependencyError(APIStatusError): + status_code: Literal[424] = 424 # pyright: ignore[reportIncompatibleVariableOverride] + + class RateLimitError(APIStatusError): status_code: Literal[429] = 429 # pyright: ignore[reportIncompatibleVariableOverride] diff --git a/src/anthropic/lib/bedrock/_client.py b/src/anthropic/lib/bedrock/_client.py index b3f388e5..fcb89987 100644 --- a/src/anthropic/lib/bedrock/_client.py +++ b/src/anthropic/lib/bedrock/_client.py @@ -77,6 +77,9 @@ def _make_status_error( if response.status_code == 422: return _exceptions.UnprocessableEntityError(err_msg, response=response, body=body) + if response.status_code == 424: + return _exceptions.FailedDependencyError(err_msg, response=response, body=body) + if response.status_code == 429: return _exceptions.RateLimitError(err_msg, response=response, body=body)