Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions sdks/python/apache_beam/ml/inference/anthropic_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 12 additions & 2 deletions sdks/python/apache_beam/ml/inference/anthropic_inference_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")))
Expand Down Expand Up @@ -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(
Expand Down
Loading