diff --git a/sdks/python/apache_beam/ml/inference/anthropic_inference.py b/sdks/python/apache_beam/ml/inference/anthropic_inference.py index e35ae52b9a22..28c19c7d993a 100644 --- a/sdks/python/apache_beam/ml/inference/anthropic_inference.py +++ b/sdks/python/apache_beam/ml/inference/anthropic_inference.py @@ -75,6 +75,7 @@ from typing import Union from anthropic import Anthropic +from anthropic import APIConnectionError from anthropic import APIStatusError from apache_beam.ml.inference import utils @@ -86,14 +87,18 @@ def _retry_on_appropriate_error(exception: Exception) -> bool: - """Retry filter that returns True for 5xx errors or 429 (rate limiting). + """Retry filter that returns True for 5xx errors, 429 (rate limiting) and + connection errors. Args: exception: the exception encountered during the request/response loop. Returns: - True if the exception is retriable (429 or 5xx), False otherwise. + True if the exception is retriable (429, 5xx or a connection error), + False otherwise. """ + if isinstance(exception, APIConnectionError): + return True if not isinstance(exception, APIStatusError): return False return exception.status_code == 429 or exception.status_code >= 500 @@ -260,13 +265,14 @@ def batch_elements_kwargs(self): def create_client(self) -> Anthropic: """Creates the Anthropic client used to send requests. - If api_key was provided at construction time, it is used directly. - Otherwise, the client will fall back to the ANTHROPIC_API_KEY - environment variable. + Sets ``max_retries=0`` so that Apache Beam's ``RemoteModelHandler`` retry + and client-side throttling mechanics are the only retry layer. If api_key + was provided at construction time, it is used directly. Otherwise, the + client will fall back to the ANTHROPIC_API_KEY environment variable. """ if self.api_key: - return Anthropic(api_key=self.api_key) - return Anthropic() + return Anthropic(api_key=self.api_key, max_retries=0) + return Anthropic(max_retries=0) def request( self, diff --git a/sdks/python/apache_beam/ml/inference/anthropic_inference_test.py b/sdks/python/apache_beam/ml/inference/anthropic_inference_test.py index eb5102edad3f..4cfec83c87cb 100644 --- a/sdks/python/apache_beam/ml/inference/anthropic_inference_test.py +++ b/sdks/python/apache_beam/ml/inference/anthropic_inference_test.py @@ -21,7 +21,9 @@ from unittest import mock try: + from anthropic import APIConnectionError from anthropic import APIStatusError + from anthropic import APITimeoutError from apache_beam.ml.inference.anthropic_inference import AnthropicModelHandler from apache_beam.ml.inference.anthropic_inference import _retry_on_appropriate_error @@ -94,6 +96,13 @@ def test_no_retry_on_401(self): body=None) self.assertFalse(_retry_on_appropriate_error(e)) + def test_retry_on_connection_error(self): + request = mock.MagicMock() + self.assertTrue( + _retry_on_appropriate_error(APIConnectionError(request=request))) + self.assertTrue( + _retry_on_appropriate_error(APITimeoutError(request=request))) + def test_no_retry_on_non_api_error(self): self.assertFalse(_retry_on_appropriate_error(ValueError("oops"))) self.assertFalse(_retry_on_appropriate_error(RuntimeError("fail"))) @@ -167,14 +176,15 @@ def test_create_client_with_api_key(self, mock_anthropic): request_fn=message_from_string, api_key='test-key-123') handler.create_client() - mock_anthropic.assert_called_once_with(api_key='test-key-123') + mock_anthropic.assert_called_once_with( + api_key='test-key-123', max_retries=0) @mock.patch('apache_beam.ml.inference.anthropic_inference.Anthropic') def test_create_client_from_env(self, mock_anthropic): handler = AnthropicModelHandler( model_name=_TEST_MODEL, request_fn=message_from_string) handler.create_client() - mock_anthropic.assert_called_once_with() + mock_anthropic.assert_called_once_with(max_retries=0) def test_request_returns_prediction_results(self): handler = AnthropicModelHandler(