Skip to content

Commit d41b8fc

Browse files
speedstorm1copybara-github
authored andcommitted
fix: honor an explicit zero in HttpRetryOptions delay fields
fixes #2026 PiperOrigin-RevId: 966685771
1 parent 4c1abcc commit d41b8fc

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

‎google/genai/_api_client.py‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,11 @@ def _load_json_from_response(cls, response: Any) -> Any:
609609
)
610610

611611

612+
def _delay_or_default(value: Optional[float], default: float) -> float:
613+
"""Returns the default only when the field is unset, so 0.0 is honored."""
614+
return default if value is None else value
615+
616+
612617
def retry_args(options: Optional[HttpRetryOptions]) -> _common.StringDict:
613618
"""Returns the retry args for the given http retry options.
614619
@@ -630,10 +635,10 @@ def retry_args(options: Optional[HttpRetryOptions]) -> _common.StringDict:
630635
or isinstance(e, _HTTPX_TRANSIENT_EXC),
631636
)
632637
wait = tenacity.wait_exponential_jitter(
633-
initial=options.initial_delay or _RETRY_INITIAL_DELAY,
634-
max=options.max_delay or _RETRY_MAX_DELAY,
635-
exp_base=options.exp_base or _RETRY_EXP_BASE,
636-
jitter=options.jitter or _RETRY_JITTER,
638+
initial=_delay_or_default(options.initial_delay, _RETRY_INITIAL_DELAY),
639+
max=_delay_or_default(options.max_delay, _RETRY_MAX_DELAY),
640+
exp_base=_delay_or_default(options.exp_base, _RETRY_EXP_BASE),
641+
jitter=_delay_or_default(options.jitter, _RETRY_JITTER),
637642
)
638643
return {
639644
'stop': stop,

‎google/genai/tests/client/test_retries.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,21 @@ def test_retry_args_enabled_with_custom_values_are_not_overridden():
187187
assert not retry.predicate(e)
188188

189189

190+
def test_retry_args_explicit_zero_values_are_not_overridden():
191+
# A field set to 0.0 is specified, so it must reach tenacity instead of
192+
# falling back to the default.
193+
options = types.HttpRetryOptions(
194+
initial_delay=0.0, max_delay=0.0, exp_base=0.0, jitter=0.0
195+
)
196+
197+
wait = api_client.retry_args(options)['wait']
198+
199+
assert wait.jitter == 0.0
200+
assert wait.initial == 0.0
201+
assert wait.max == 0.0
202+
assert wait.exp_base == 0.0
203+
204+
190205
def test_retry_args_retries_httpx_transport_errors():
191206
# httpx transport errors (timeouts, connect errors) bypass APIError but are
192207
# transient infrastructure failures, so the predicate must still retry them

0 commit comments

Comments
 (0)