Skip to content

Commit 1671a3f

Browse files
committed
fix(interactions): map HttpRetryOptions.attempts onto the retry count
`attempts` counts the original request, so 0 and 1 both mean no retries. `_translate_retry_config` passed it straight through as `max_retries`, which counts retries, so attempts=1 retried once. attempts=0 is rewritten to 1 by `retry_args` in the parent client before the Interactions client is built, so it retried once as well, sleeping for the full Retry-After before the second request. Map attempts to attempts - 1, floored at zero. The default when attempts is unset does not change. Fixes googleapis#3260
1 parent 51285c8 commit 1671a3f

2 files changed

Lines changed: 128 additions & 2 deletions

File tree

‎google/genai/_gaos/google_genai.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,19 @@ def _get_google_genai_security(api_client: Any) -> Optional[Any]:
156156
def _translate_retry_config(http_options: Any) -> RetryConfig:
157157
"""Maps parent `HttpOptions.retry_options` onto a `RetryConfig`.
158158
159-
`attempts` becomes the retry count (an unset `attempts` falls back to the
159+
`attempts` counts the original request, so `attempts - 1` becomes the retry
160+
count: 0 and 1 both mean no retries (an unset `attempts` falls back to the
160161
default retry count). The delay-shaping options (`initial_delay`,
161162
`max_delay`, `exp_base`, `jitter`) and `http_status_codes` are honored when
162163
provided, each defaulting to the built-in behavior when omitted.
163164
"""
164165
options = getattr(http_options, 'retry_options', None)
165166

166167
attempts = getattr(options, 'attempts', None) if options is not None else None
167-
max_retries = attempts if attempts is not None else _DEFAULT_MAX_RETRIES + 1
168+
if attempts is None:
169+
max_retries = _DEFAULT_MAX_RETRIES + 1
170+
else:
171+
max_retries = max(attempts - 1, 0)
168172

169173
initial_interval = _DEFAULT_INITIAL_INTERVAL_MS
170174
max_interval = _DEFAULT_MAX_INTERVAL_MS
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
17+
"""Tests for how `HttpRetryOptions` applies to the Interactions API."""
18+
19+
from unittest import mock
20+
21+
import pytest
22+
23+
from ... import Client, _api_client, types
24+
from ..._gaos import google_genai
25+
from ..._gaos.lib import compat_errors
26+
27+
if _api_client._get_http_client_backend() == "httpx2":
28+
from httpx2 import Client as HTTPClient, Request, Response # pylint: disable=g-import-not-at-top
29+
else:
30+
from httpx import Client as HTTPClient, Request, Response # pylint: disable=g-import-not-at-top
31+
32+
ENV_VARS = [
33+
"GOOGLE_CLOUD_PROJECT",
34+
"GEMINI_API_KEY",
35+
"GOOGLE_API_KEY",
36+
"GOOGLE_CLOUD_LOCATION",
37+
]
38+
39+
40+
@pytest.fixture(autouse=True)
41+
def clear_env_vars(monkeypatch):
42+
for var in ENV_VARS:
43+
monkeypatch.delenv(var, raising=False)
44+
45+
46+
def _rate_limited():
47+
return Response(
48+
429,
49+
request=Request('POST', ''),
50+
headers={'retry-after-ms': '1', 'content-type': 'application/json'},
51+
content='{"error": {"code": "too_many_requests", "message": "quota"}}',
52+
)
53+
54+
55+
def _completed():
56+
return Response(
57+
200,
58+
request=Request('POST', ''),
59+
headers={'content-type': 'application/json'},
60+
content='{"status": "completed"}',
61+
)
62+
63+
64+
@pytest.mark.parametrize(
65+
'attempts, expected_retries',
66+
[
67+
(None, google_genai._DEFAULT_MAX_RETRIES + 1),
68+
(0, 0),
69+
(1, 0),
70+
(2, 1),
71+
(5, 4),
72+
],
73+
)
74+
def test_translate_retry_config_counts_the_original_request(
75+
attempts, expected_retries
76+
):
77+
retry_options = None
78+
if attempts is not None:
79+
retry_options = types.HttpRetryOptions(attempts=attempts)
80+
http_options = types.HttpOptions(retry_options=retry_options)
81+
82+
config = google_genai._translate_retry_config(http_options)
83+
84+
assert config.max_retries == expected_retries
85+
86+
87+
@pytest.mark.parametrize('attempts', [0, 1])
88+
def test_interactions_do_not_retry_when_attempts_disable_retries(
89+
monkeypatch, attempts
90+
):
91+
monkeypatch.setenv('GOOGLE_API_KEY', 'test-api-key')
92+
client = Client(
93+
http_options=types.HttpOptions(
94+
retry_options=types.HttpRetryOptions(attempts=attempts)
95+
)
96+
)
97+
98+
with mock.patch.object(HTTPClient, 'send') as mock_send:
99+
mock_send.side_effect = [_rate_limited(), _rate_limited()]
100+
with pytest.raises(compat_errors.RateLimitError):
101+
client.interactions.create(model='gemini-1.5-flash', input='Hello')
102+
assert mock_send.call_count == 1
103+
104+
105+
def test_interactions_retry_up_to_attempts(monkeypatch):
106+
monkeypatch.setenv('GOOGLE_API_KEY', 'test-api-key')
107+
client = Client(
108+
http_options=types.HttpOptions(
109+
retry_options=types.HttpRetryOptions(attempts=3)
110+
)
111+
)
112+
113+
with mock.patch.object(HTTPClient, 'send') as mock_send:
114+
mock_send.side_effect = [_rate_limited(), _rate_limited(), _completed()]
115+
client.interactions.create(model='gemini-1.5-flash', input='Hello')
116+
assert mock_send.call_count == 3
117+
118+
with mock.patch.object(HTTPClient, 'send') as mock_send:
119+
mock_send.side_effect = [_rate_limited()] * 4
120+
with pytest.raises(compat_errors.RateLimitError):
121+
client.interactions.create(model='gemini-1.5-flash', input='Hello')
122+
assert mock_send.call_count == 3

0 commit comments

Comments
 (0)