Skip to content
Open
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
17 changes: 9 additions & 8 deletions google/genai/_gaos/google_genai.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ def _get_google_genai_security(api_client: Any) -> Optional[Any]:
return None


# Default backoff shape (ms): 0.5s initial, 8s cap, base 2. Individual fields
# are overridden by the matching `retry_options` value when set.
_DEFAULT_INITIAL_INTERVAL_MS = 500
_DEFAULT_MAX_INTERVAL_MS = 8000
# Default backoff shape: initial delay, max delay and jitter come from
# `_api_client` (`_RETRY_INITIAL_DELAY`, `_RETRY_MAX_DELAY`, `_RETRY_JITTER`),
# shared with the rest of the SDK; base 2. Individual fields are overridden
# by the matching `retry_options` value when set.
_DEFAULT_EXPONENT = 2
_MAX_ELAPSED_TIME_MS = 30000

Expand All @@ -203,7 +203,8 @@ def _translate_retry_config(http_options: Any) -> RetryConfig:
`_api_client._RETRY_ATTEMPTS`, shared with the rest of the SDK). The
delay-shaping options (`initial_delay`,
`max_delay`, `exp_base`, `jitter`) and `http_status_codes` are honored when
provided, each defaulting to the built-in behavior when omitted.
provided, each defaulting to the `_api_client` retry defaults
(`_RETRY_INITIAL_DELAY`, `_RETRY_MAX_DELAY`, `_RETRY_JITTER`) when omitted.
"""
options = getattr(http_options, 'retry_options', None)

Expand All @@ -212,10 +213,10 @@ def _translate_retry_config(http_options: Any) -> RetryConfig:
attempts = _api_client._RETRY_ATTEMPTS
max_retries = max(attempts - 1, 0)

initial_interval = _DEFAULT_INITIAL_INTERVAL_MS
max_interval = _DEFAULT_MAX_INTERVAL_MS
initial_interval = int(_api_client._RETRY_INITIAL_DELAY * 1000)
max_interval = int(_api_client._RETRY_MAX_DELAY * 1000)
exponent = _DEFAULT_EXPONENT
jitter_ms = None
jitter_ms = int(_api_client._RETRY_JITTER * 1000)
status_codes_override = None

if options is not None:
Expand Down
32 changes: 32 additions & 0 deletions google/genai/tests/interactions/test_retry_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,38 @@ def test_interactions_retry_up_to_attempts(monkeypatch):
assert mock_send.call_count == 3


def test_translate_retry_config_delay_defaults_match_api_client():
http_options = types.HttpOptions(
retry_options=types.HttpRetryOptions()
)

config = google_genai._translate_retry_config(http_options)

assert config.backoff.initial_interval == int(
google_genai._api_client._RETRY_INITIAL_DELAY * 1000
)
assert config.backoff.max_interval == int(
google_genai._api_client._RETRY_MAX_DELAY * 1000
)
assert config.backoff.jitter_ms == int(
google_genai._api_client._RETRY_JITTER * 1000
)


def test_translate_retry_config_explicit_delays_override_defaults():
http_options = types.HttpOptions(
retry_options=types.HttpRetryOptions(
initial_delay=0.25, max_delay=3.0, jitter=0.0
)
)

config = google_genai._translate_retry_config(http_options)

assert config.backoff.initial_interval == 250
assert config.backoff.max_interval == 3000
assert config.backoff.jitter_ms == 0


def test_interactions_default_matches_generate_content_attempts(monkeypatch):
monkeypatch.setenv('GOOGLE_API_KEY', 'test-api-key')
client = Client(
Expand Down