Backtestspydantic-airun-20260918-215936#4937
Support configurable HTTP retry options for GoogleProvider
The merged fix was judged better than our patch.
In this judgment, Patch A = our agent, Patch B = the merged fix.
The source change is byte-for-byte identical in both patches (add `HttpRetryOptions` import, thread a `retry_options` param through all three overloads and the real `__init__`, forward it into `HttpOptions`, document it). Both correctly resolve the root cause—exposing the SDK's native `HttpOptions.retry_options` without forcing users to build their own Client—and both add a test that fails before the change (passing `retry_options=` raised TypeError) and passes after. So correctness and scope are a wash; the decision rests on tests and craft. Patch B is stronger on two counts: (1) location—it puts the test under tests/providers/, which matches the convention for a provider feature, whereas Patch A appends its test to tests/models/test_google.py, a mismatch for a change that lives entirely in providers/google.py; (2) coverage—B adds a negative test asserting `retry_options is None` by default, guarding the default/overload behavior against regression, in addition to the positive passthrough test. Patch A's single test is fine (its `is retry_options` identity check is marginally stronger than B's `is not None`, and it exercises more fields), but B's positive test already verifies attributes (attempts, initial_delay, max_delay) and its default-case test covers ground A leaves untested. The one caveat: B relies on tests/providers/test_google.py not already existing (declared as a new file); if it did, the patch would fail to apply—but taking the diff at face value, B's better-conventioned placement plus the extra default-behavior test give it the edge.
The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.
Solve
15m 17s2 turns
Wall-clock time in the container
Full test suite
No new failures
5,558 passed · 285 skipped · 5,885 total
Contamination audit
Unknown
See the audit notes for this run
Patch size
34vs 46 merged
Changed lines, ours vs merged
The issue
View on GitHubDescription
GoogleProvider currently doesn't expose the google-genai SDK's built-in HttpRetryOptions, which means users who want retry behavior for transient HTTP errors (429, 500, 502, 503, 504) must construct their own GenAIClient and pass it via GoogleProvider(client=...), bypassing the provider's normal setup.
The google-genai SDK supports this natively via HttpOptions.retry_options:
from google.genai.types import HttpOptions, HttpRetryOptions
HttpOptions(
retry_options=HttpRetryOptions(
initial_delay=1.0,
attempts=4,
exp_base=2,
max_delay=60,
jitter=1,
http_status_codes=[408, 429, 500, 502, 503, 504],
),
)
Reference: https://docs.cloud.google.com/vertex-ai/generative-ai/docs/retry-strategy
Proposal
Add a retry_options parameter to GoogleProvider.__init__ (or accept HttpRetryOptions as part of an existing config) so users can configure retries without having to manually construct the underlying Client.
Something like:
from google.genai.types import HttpRetryOptions
provider = GoogleProvider(
project="my-project",
location="us-central1",
retry_options=HttpRetryOptions(
initial_delay=1.0,
attempts=4,
http_status_codes=[408, 429, 500, 502, 503, 504],
),
)
Current Workaround
Construct a GenAIClient manually with retry options and pass it to GoogleProvider(client=...). This works but requires duplicating the provider's setup logic (User-Agent header, timeout, vertexai flag, etc.).
The two patches
pydantic_ai_slim/pydantic_ai/providers/google.pyindex 36d5e81f..859aaa7d 100644@@ -14,7 +14,7 @@ from pydantic_ai.providers import Provider try: from google.auth.credentials import Credentials from google.genai.client import Client- from google.genai.types import HttpOptions+ from google.genai.types import HttpOptions, HttpRetryOptions except ImportError as _import_error: raise ImportError( 'Please install the `google-genai` package to use the Google provider, '@@ -43,7 +43,12 @@ class GoogleProvider(Provider[Client]): @overload def __init__(- self, *, api_key: str, http_client: httpx.AsyncClient | None = None, base_url: str | None = None+ self,+ *,+ api_key: str,+ http_client: httpx.AsyncClient | None = None,+ base_url: str | None = None,+ retry_options: HttpRetryOptions | None = None, ) -> None: ... @overload@@ -55,6 +60,7 @@ class GoogleProvider(Provider[Client]): location: VertexAILocation | Literal['global'] | str | None = None, http_client: httpx.AsyncClient | None = None, base_url: str | None = None,+ retry_options: HttpRetryOptions | None = None, ) -> None: ... @overload@@ -68,6 +74,7 @@ class GoogleProvider(Provider[Client]): api_key: str | None = None, http_client: httpx.AsyncClient | None = None, base_url: str | None = None,+ retry_options: HttpRetryOptions | None = None, ) -> None: ... def __init__(@@ -81,6 +88,7 @@ class GoogleProvider(Provider[Client]): client: Client | None = None, http_client: httpx.AsyncClient | None = None, base_url: str | None = None,+ retry_options: HttpRetryOptions | None = None, ) -> None: """Create a new Google provider. @@ -99,6 +107,8 @@ class GoogleProvider(Provider[Client]): client: A pre-initialized client to use. http_client: An existing `httpx.AsyncClient` to use for making HTTP requests. base_url: The base URL for the Google API.+ retry_options: Options to configure automatic retries for transient HTTP errors (e.g. 429, 500, 502, 503, 504).+ See `google.genai.types.HttpRetryOptions`. """ if client is None: # NOTE: We are keeping GEMINI_API_KEY for backwards compatibility.@@ -123,6 +133,7 @@ class GoogleProvider(Provider[Client]): headers={'User-Agent': get_user_agent()}, httpx_async_client=http_client, timeout=timeout_ms,+ retry_options=retry_options, ) if not vertexai: if api_key is None:tests/models/test_google.pyindex 01a8f714..1f753b92 100644@@ -93,6 +93,7 @@ with try_import() as imports_successful: HarmBlockThreshold, HarmCategory, HarmProbability,+ HttpRetryOptions, LogprobsResult, LogprobsResultCandidate, LogprobsResultTopCandidates,@@ -2704,6 +2705,24 @@ async def test_google_timeout(allow_model_requests: None, google_provider: Googl await agent.run('Hello!', model_settings={'timeout': Timeout(10)}) +def test_google_provider_retry_options():+ retry_options = HttpRetryOptions(+ initial_delay=1.0,+ attempts=4,+ exp_base=2,+ max_delay=60,+ jitter=1,+ http_status_codes=[408, 429, 500, 502, 503, 504],+ )+ provider = GoogleProvider(api_key='foo', retry_options=retry_options)++ http_options = provider.client._api_client._http_options # pyright: ignore[reportPrivateUsage]+ assert http_options.retry_options is retry_options+ assert http_options.retry_options.attempts == 4+ assert http_options.retry_options.http_status_codes == [408, 429, 500, 502, 503, 504]+ assert http_options.retry_options.initial_delay == 1.0++ async def test_google_extra_headers(allow_model_requests: None, google_provider: GoogleProvider): m = GoogleModel('gemini-1.5-flash', provider=google_provider) agent = Agent(m, model_settings=GoogleModelSettings(extra_headers={'Extra-Header-Key': 'Extra-Header-Value'}))
pydantic_ai_slim/pydantic_ai/providers/google.pyindex 36d5e81fbd..c90b2046fc 100644@@ -14,7 +14,7 @@ from pydantic_ai.providers import Provider try: from google.auth.credentials import Credentials from google.genai.client import Client- from google.genai.types import HttpOptions+ from google.genai.types import HttpOptions, HttpRetryOptions except ImportError as _import_error: raise ImportError( 'Please install the `google-genai` package to use the Google provider, '@@ -43,7 +43,12 @@ class GoogleProvider(Provider[Client]): @overload def __init__(- self, *, api_key: str, http_client: httpx.AsyncClient | None = None, base_url: str | None = None+ self,+ *,+ api_key: str,+ http_client: httpx.AsyncClient | None = None,+ base_url: str | None = None,+ retry_options: HttpRetryOptions | None = None, ) -> None: ... @overload@@ -55,6 +60,7 @@ class GoogleProvider(Provider[Client]): location: VertexAILocation | Literal['global'] | str | None = None, http_client: httpx.AsyncClient | None = None, base_url: str | None = None,+ retry_options: HttpRetryOptions | None = None, ) -> None: ... @overload@@ -68,6 +74,7 @@ class GoogleProvider(Provider[Client]): api_key: str | None = None, http_client: httpx.AsyncClient | None = None, base_url: str | None = None,+ retry_options: HttpRetryOptions | None = None, ) -> None: ... def __init__(@@ -81,6 +88,7 @@ class GoogleProvider(Provider[Client]): client: Client | None = None, http_client: httpx.AsyncClient | None = None, base_url: str | None = None,+ retry_options: HttpRetryOptions | None = None, ) -> None: """Create a new Google provider. @@ -99,6 +107,8 @@ class GoogleProvider(Provider[Client]): client: A pre-initialized client to use. http_client: An existing `httpx.AsyncClient` to use for making HTTP requests. base_url: The base URL for the Google API.+ retry_options: HTTP retry options for transient errors (429, 5xx, etc.).+ See `google.genai.types.HttpRetryOptions` for available fields. """ if client is None: # NOTE: We are keeping GEMINI_API_KEY for backwards compatibility.@@ -123,6 +133,7 @@ class GoogleProvider(Provider[Client]): headers={'User-Agent': get_user_agent()}, httpx_async_client=http_client, timeout=timeout_ms,+ retry_options=retry_options, ) if not vertexai: if api_key is None:tests/providers/test_google.pynew file mode 100644index 0000000000..c4051c5d23@@ -0,0 +1,31 @@+from __future__ import annotations as _annotations++import pytest++from ..conftest import TestEnv, try_import++with try_import() as imports_successful:+ from google.genai.types import HttpRetryOptions++ from pydantic_ai.providers.google import GoogleProvider++pytestmark = pytest.mark.skipif(not imports_successful(), reason='google-genai not installed')+++def test_google_provider_retry_options(env: TestEnv):+ env.set('GOOGLE_API_KEY', 'test-key')+ retry = HttpRetryOptions(attempts=4, initial_delay=2.0, max_delay=30.0)+ provider = GoogleProvider(api_key='test-key', retry_options=retry)+ # Verify the retry options were passed through to the underlying HttpOptions.+ http_options = provider.client._api_client._http_options # type: ignore[reportPrivateUsage]+ assert http_options.retry_options is not None+ assert http_options.retry_options.attempts == 4+ assert http_options.retry_options.initial_delay == 2.0+ assert http_options.retry_options.max_delay == 30.0+++def test_google_provider_no_retry_options(env: TestEnv):+ env.set('GOOGLE_API_KEY', 'test-key')+ provider = GoogleProvider(api_key='test-key')+ http_options = provider.client._api_client._http_options # type: ignore[reportPrivateUsage]+ assert http_options.retry_options is None
What the agent reported
The diff is clean: only providers/google.py (+15) and tests/models/test_google.py (+19) reflect my change. The three deletions (.claude/settings.json, AGENTS.md, CLAUDE.md) were already present in the working tree before this task began.
Summary
What was wrong: GoogleProvider gave no way to enable the google-genai SDK's built-in HTTP retry behavior (HttpRetryOptions). Users wanting retries for transient errors (429/500/502/503/504) had to construct a GenAIClient themselves and pass it via GoogleProvider(client=...), duplicating the provider's User-Agent/timeout/vertexai setup.
What I changed (pydantic_ai_slim/pydantic_ai/providers/google.py):
- Imported
HttpRetryOptionsalongsideHttpOptionsfromgoogle.genai.types. - Added a
retry_options: HttpRetryOptions | None = Nonekeyword parameter to the real__init__and to the three non-client overloads (api_key, credentials/vertex, vertexai bool) — deliberately not the client-only overload, which bypassesHttpOptionsconstruction. - Added a docstring entry.
- Threaded
retry_options=retry_optionsinto theHttpOptions(...)construction, so it flows into both the GLA and VertexClientpaths.
Test evidence:
- Added
test_google_provider_retry_optionsintests/models/test_google.py(and addedHttpRetryOptionsto the guarded import). It constructsGoogleProvider(api_key='foo', retry_options=...)and asserts the options reachprovider.client._api_client._http_options.retry_options(identity + field checks). This is a new parameter, so before the fix construction raisesTypeError: unexpected keyword argument 'retry_options'. - The
bt-testwrapper refuses targeted runs (its base command chains with&&), so I ran the full frozen suite: 5566 passed, 28 failed. The new test passed (confirmed via junit: self-closing testcase, no failure/error child). All 28 failures are in unrelated modules —tests/test_dbos.py(databaseIntegrityError/UserWarning),tests/models/test_outlines.py(llamacpp), andtests/test_agent.py(context-manager/MCP) — none touch the Google provider, and they are environmental/pre-existing, not caused by this change.
Reviewer: Confirmed the change is correct and complete with no unrelated production/test edits and no weakened tests.
Requirements checklist:
- Add
retry_optionsparam toGoogleProvider.__init__acceptingHttpRetryOptions— Implemented (param + overloads + docstring), covered by the new test. - Thread it through to the underlying Client via
HttpOptions.retry_optionswithout requiring a manualClient— Implemented (HttpOptions(..., retry_options=retry_options)on both GLA and Vertex paths), asserted by the test readingclient._api_client._http_options.retry_options. - Report's "or accept as part of an existing config" alternative — Skipped in favor of the primary, explicitly-preferred
retry_options=parameter form shown in the report's own example; a dedicated parameter is the simplest, most defensible fit and requires no new config type.
All changes are left uncommitted in the working tree.