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
3 changes: 2 additions & 1 deletion google/genai/_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,8 @@ def retry_args(options: Optional[HttpRetryOptions]) -> _common.StringDict:
"""
if options is None:
return {'stop': tenacity.stop_after_attempt(1), 'reraise': True}

if options.attempts == 0:
options.attempts = 1
stop = tenacity.stop_after_attempt(options.attempts or _RETRY_ATTEMPTS)
retriable_codes = options.http_status_codes or _RETRY_HTTP_STATUS_CODES
retry = tenacity.retry_if_exception(
Expand Down
27 changes: 27 additions & 0 deletions google/genai/tests/client/test_retries.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,33 @@ def test_retries_failed_request_retries_unsuccessfully():
mock_transport.handle_request.assert_called()


def test_retries_failed_request_no_retries_unsuccessfully():
mock_transport = mock.Mock(spec=httpx.BaseTransport)
mock_transport.handle_request.side_effect = (
_httpx_response(429),
)

client = api_client.BaseApiClient(
vertexai=True,
project='test_project',
location='global',
http_options=_transport_options(
http_options=types.HttpOptions(
retry_options=types.HttpRetryOptions(attempts=0)
),
transport=mock_transport,
),
)

with _patch_auth_default():
try:
client.request(http_method='GET', path='path', request_dict={})
assert False, 'Expected APIError to be raised.'
except errors.APIError as e:
assert e.code == 429
mock_transport.handle_request.assert_called()


def test_retries_failed_request_retries_unsuccessfully_at_request_level():
mock_transport = mock.Mock(spec=httpx.BaseTransport)
mock_transport.handle_request.side_effect = (
Expand Down
Loading