Skip to content

Commit 2856c0a

Browse files
yinghsienwucopybara-github
authored andcommitted
fix: Treat attempts=0 as attempts=1 in retry options to ensure no retries
PiperOrigin-RevId: 885118530
1 parent 6c3379f commit 2856c0a

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

google/genai/_api_client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,8 @@ def retry_args(options: Optional[HttpRetryOptions]) -> _common.StringDict:
486486
"""
487487
if options is None:
488488
return {'stop': tenacity.stop_after_attempt(1), 'reraise': True}
489-
489+
if options.attempts == 0:
490+
options.attempts = 1
490491
stop = tenacity.stop_after_attempt(options.attempts or _RETRY_ATTEMPTS)
491492
retriable_codes = options.http_status_codes or _RETRY_HTTP_STATUS_CODES
492493
retry = tenacity.retry_if_exception(

google/genai/tests/client/test_retries.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,33 @@ def test_retries_failed_request_retries_unsuccessfully():
342342
mock_transport.handle_request.assert_called()
343343

344344

345+
def test_retries_failed_request_no_retries_unsuccessfully():
346+
mock_transport = mock.Mock(spec=httpx.BaseTransport)
347+
mock_transport.handle_request.side_effect = (
348+
_httpx_response(429),
349+
)
350+
351+
client = api_client.BaseApiClient(
352+
vertexai=True,
353+
project='test_project',
354+
location='global',
355+
http_options=_transport_options(
356+
http_options=types.HttpOptions(
357+
retry_options=types.HttpRetryOptions(attempts=0)
358+
),
359+
transport=mock_transport,
360+
),
361+
)
362+
363+
with _patch_auth_default():
364+
try:
365+
client.request(http_method='GET', path='path', request_dict={})
366+
assert False, 'Expected APIError to be raised.'
367+
except errors.APIError as e:
368+
assert e.code == 429
369+
mock_transport.handle_request.assert_called()
370+
371+
345372
def test_retries_failed_request_retries_unsuccessfully_at_request_level():
346373
mock_transport = mock.Mock(spec=httpx.BaseTransport)
347374
mock_transport.handle_request.side_effect = (

0 commit comments

Comments
 (0)