Raise a gave-up transport status or connection error from Google embeddings, image generation and `GoogleModel.count_tokens` as `ModelAPIError`
This pull request was posted by Claude Code using claude-opus-5-5 on behalf of David.
Closes #13082
Why we make these changes
A retrying transport re-raises httpx2.HTTPStatusError for the status it gave up on, and google.genai lets that through unwrapped, as it does connection errors and timeouts. GoogleModel requests already map both. Three other Google call sites didn't, so the raw httpx2 exception escaped and contradicted the documented contracts in docs/embeddings.md and docs/image-generation.md:
| Call site | Gave-up status | Connection error / timeout |
|---|---|---|
GoogleEmbeddingModel.embed / count_tokens | raw HTTPStatusError → fixed | already mapped |
GoogleImageGenerationModel.generate | raw HTTPStatusError → fixed | raw ConnectError / ReadTimeout → fixed |
GoogleModel.count_tokens | raw HTTPStatusError → fixed | raw ConnectError → fixed |
Each arm calls the same _model_errors helpers as models/google.py::_map_api_error. Embeddings and images don't reuse _map_api_error itself, because its APIError branch suggests chat model ids on NOT_FOUND.
[!WARNING]
Compatibility impact: Existing code that catches a raw httpx2.HTTPStatusError from any of the call sites above, or a raw httpx2.TransportError such as ConnectError or ReadTimeout from GoogleImageGenerationModel.generate or GoogleModel.count_tokens, will no longer catch it. It is now raised as a ModelHTTPError with its error category, or as ModelConnectionError or ModelTimeoutError, with the httpx2 exception as its __cause__.
Why this can ship in a minor release: It fixes a bug. docs/embeddings.md and docs/image-generation.md document ModelAPIError subclasses for these failures, and GoogleModel requests already raise them, so the raw exceptions were undocumented. except ModelAPIError and the error categories are unaffected.
Migration: Catch ModelHTTPError or an error category such as ModelRateLimitError, ModelConnectionError for connection failures, or ModelAPIError for all of them.
New public surface
None.
User-visible behavior
model = GoogleImageGenerationModel('gemini-2.5-flash-image', provider=provider)
await model.generate('tiny robot') # the transport raises `HTTPStatusError` for a 429 whose read body carries `RetryInfo.retryDelay: '14s'`
# before: httpx2.HTTPStatusError
# after: ModelRateLimitError (a ModelHTTPError) with status_code=429, retry_after=14.0
retry_after comes from the body only when the transport read it before raising, as for GoogleModel requests; otherwise only a Retry-After header carries through.
Verification
Each test below fails on main with the raw httpx2 exception and passes here. A separate verifier compared every surface field by field with GoogleModel.request over 49 error and success scenarios, and found no input that works on main and behaves differently here.
- Embeddings:
google-transport-gave-upandgoogle-transport-gave-up-retry-infocases - Image generation:
test_google_image_generation_gave_up_status_error,test_google_image_generation_transport_error count_tokens:test_google_count_tokens_transport_error_is_wrapped
What changes for existing users
Code that caught the raw httpx2 exceptions from these call sites must catch the documented ModelAPIError subclass instead; every other input behaves as before.
PR decisions
2026-11-12 · premises reproduced by blind validator · iter -
- Decision: Gaps 1-3 and the count_tokens sibling reproduce on upstream/main cb6728d06; embedding connection errors are already mapped; docs already state the contract the fix meets
- Why: Validator evidence copied to local-notes/repro*.py and re-run on this branch
- Source: https://github.com/pydantic/pydantic-ai/issues/13082
- Session: bf326360-1c79-4d5b-99d8-8508e7d44d45
2026-11-12 · fix shape: copy the two arms of models/google.py::_map_api_error · iter -
- Decision: Call _model_errors.transport_status_error + google_retry_after, and _model_errors.connection_error(timeout=isinstance(e, httpx2.TimeoutException)) directly; embeddings gets the HTTPStatusError arm before its TransportError arm; images gets both arms after except errors.APIError
- Why: Narrowest fix; same helpers the chat model path uses
- Source: https://github.com/pydantic/pydantic-ai/issues/13082
- Session: bf326360-1c79-4d5b-99d8-8508e7d44d45
2026-11-12 · do not reuse models/google.py::_map_api_error whole · iter -
- Decision: Embeddings and images keep their own except arms
- Why: Its APIError branch runs the chat-model-id suggestion on NOT_FOUND, wrong for embedding and image models
- Source: https://github.com/pydantic/pydantic-ai/issues/13082
- Session: bf326360-1c79-4d5b-99d8-8508e7d44d45
2026-11-12 · fold GoogleModel.count_tokens · iter -
- Decision: Widen count_tokens except to the (errors.APIError, httpx2.TransportError, httpx2.HTTPStatusError) tuple _generate_content uses; failing test first
- Why: Same provider, same root cause, same fix shape; changes output only for inputs that already fail on main
- Source: https://github.com/pydantic/pydantic-ai/issues/13082
- Session: bf326360-1c79-4d5b-99d8-8508e7d44d45
2026-11-12 · split out of this PR: other providers + images APIError category · iter -
- Decision: File one follow-up issue per root cause via /file-followup-issue after the PR opens, no labels or assignee: (a) embeddings/openai.py and images/openai.py leak a gave-up status (cohere and the models/google.py::_map_api_errors batch context manager unchecked); (b) images/google.py errors.APIError arm builds an uncategorized ModelHTTPError
- Why: Different root causes; (b) is an additive behavior change
- Source: https://github.com/pydantic/pydantic-ai/issues/13082
- Session: bf326360-1c79-4d5b-99d8-8508e7d44d45
2026-11-12 · out of scope: shared Google error-constructor refactor · iter -
- Decision: Not done here
- Why: The chooser's call, per the manager
- Source: https://github.com/pydantic/pydantic-ai/issues/13082
- Session: bf326360-1c79-4d5b-99d8-8508e7d44d45
2026-11-12 · tests plan · iter -
- Decision: Embeddings: Case rows google-transport-gave-up (503) and google-transport-gave-up-retry-info (429 RetryInfo 14s) with a _gave_up handler copied from tests/models/test_error_categories.py; images: tests next to test_google_image_generation_status_error via _mock_google_provider (429 RetryInfo gave-up; ConnectError/ReadTimeout parametrized), not VCR because the transport raises; count_tokens: one test in tests/models/test_google.py
- Why: Follows each file's existing error-test pattern
- Source: https://github.com/pydantic/pydantic-ai/issues/13082
- Session: bf326360-1c79-4d5b-99d8-8508e7d44d45
2026-11-12 · pre-push review 1 of 3 at cfa54a7a0ed3677bfa98759e4cb150c6a41e9d28 · iter -
- Decision: reserved; dispatching (haiku, claude -p from detached policy-base worktree, read-only tools, no fan-out)
- Why: tree cc61a636519285ae9f974d73705988a922c52961; scope cb6728d06e42df2cad1fd20a3ffebc3fe0d1ea66..cfa54a7a0ed3677bfa98759e4cb150c6a41e9d28 (full branch); policy base 511d00512b77ce34600a5c63dd63beff2160b4dc; first review
- Source: https://github.com/pydantic/pydantic-ai/tree/google-adapters-gave-up-transport-status
- Session: bf326360-1c79-4d5b-99d8-8508e7d44d45
2026-11-12 · pre-push review 1 of 3 at cfa54a7a0ed3677bfa98759e4cb150c6a41e9d28 · iter -
- Decision: no findings (current at cfa54a7a0ed3677bfa98759e4cb150c6a41e9d28)
- Why: reviewed tree cc61a636519285ae9f974d73705988a922c52961; scope cb6728d06e42df2cad1fd20a3ffebc3fe0d1ea66..cfa54a7a0ed3677bfa98759e4cb150c6a41e9d28 (full branch); haiku, claude -p from detached policy-base worktree at 511d00512b77ce34600a5c63dd63beff2160b4dc, read-only tools, no fan-out
- Source: https://github.com/pydantic/pydantic-ai/tree/google-adapters-gave-up-transport-status
- Session: bf326360-1c79-4d5b-99d8-8508e7d44d45
2026-11-12 · adversarial verifier at cfa54a7a0ed3677bfa98759e4cb150c6a41e9d28 · iter -
- Decision: dispatching fresh strong-tier verifier (opus, claude -p from detached checkouts of head and main), not a pre-push review
- Why: tree cc61a636519285ae9f974d73705988a922c52961; manager-requested: count_tokens fold falls under the fold rule; claims: (1) three gaps + count_tokens raise mapped ModelAPIError with right status/category/retry_after, (2) no main-working input fails or changes meaning, incl. images/count_tokens errors.APIError paths and GoogleModel NOT_FOUND suggestion
- Source: https://github.com/pydantic/pydantic-ai/pull/13312
- Session: bf326360-1c79-4d5b-99d8-8508e7d44d45
2026-11-12 · defer: gave-up status leak in OpenAI/Cohere embeddings, OpenAI images, Google batch methods · iter -
- Decision: Filed as #13313 instead of fixing here; no routing labels or assignee (manager routes)
- Why: Out of scope for this PR — blast radius: other adapters and the batch API; cohere and batch_status/batch_cancel verified on main and head with local-notes/repro_followup_siblings.py
- Source: https://github.com/pydantic/pydantic-ai/issues/13313
- Session: bf326360-1c79-4d5b-99d8-8508e7d44d45
2026-11-12 · defer: images/google.py APIError arm builds an uncategorized ModelHTTPError · iter -
- Decision: Filed as #13314 instead of fixing here; no routing labels or assignee (manager routes)
- Why: Out of scope for this PR — decisions: additive behavior change for inputs that already raise ModelHTTPError on main
- Source: https://github.com/pydantic/pydantic-ai/issues/13314
- Session: bf326360-1c79-4d5b-99d8-8508e7d44d45
2026-11-12 · adversarial verifier at cfa54a7a0ed3677bfa98759e4cb150c6a41e9d28 · iter -
- Decision: both claims HOLD (49-scenario matrix head vs main, field-by-field parity with GoogleModel.request; pytest not run by the verifier — test-runner agent unavailable in claude -p); outside findings: (A) real-socket retrying transport closes the body unread so RetryInfo retry_after is lost on every surface incl. chat, pre-existing, reported to manager; (B) = #13314; (C) GoogleEmbeddingModel.count_tokens also gains the arm, PR body updated; (D) legacy httpx v1 client errors leak on every surface, pre-existing
- Why: tree cc61a636519285ae9f974d73705988a922c52961; fresh opus via claude -p from detached head and merge-base checkouts; manager-requested for the count_tokens fold
- Source: https://github.com/pydantic/pydantic-ai/pull/13312
- Session: bf326360-1c79-4d5b-99d8-8508e7d44d45
2026-11-12 · defer: AsyncHTTPX2TenacityTransport closes a rejected response unread, losing Gemini RetryInfo · iter -
- Decision: Filed as #13315 instead of fixing here; no labels or assignee; legacy httpx v1 leak not filed (manager raising whether that surface is supported)
- Why: Out of scope for this PR — blast radius: root cause in the shared retrying transport, reproduces on main via GoogleModel.request (local-notes/repro_real_socket_retry_info.py)
- Source: https://github.com/pydantic/pydantic-ai/issues/13315
- Session: bf326360-1c79-4d5b-99d8-8508e7d44d45
2026-11-12 · skip: legacy httpx v1 client error leak · iter -
- Decision: Skip, no issue: with a legacy httpx.AsyncClient, httpx.HTTPStatusError and httpx.ConnectError leak raw on every Google surface, same on main
- Why: Google provider already warns the legacy client is deprecated (providers/google.py warn_if_legacy_httpx_client) and the surface goes away in v3; a fix would add a legacy arm to delete in v3. Manager relay for David
- Source: https://github.com/pydantic/pydantic-ai/pull/13312
- Session: bf326360-1c79-4d5b-99d8-8508e7d44d45
2026-11-12 · douwebot applied at cfa54a7a0ed3677bfa98759e4cb150c6a41e9d28 · iter -
- Decision: label applied after CI success and PR Review Checks success on the head (attempt 1)
- Why: Merge bar: size S, not trivial, needs a clean douwebot approval
- Source: https://github.com/pydantic/pydantic-ai/pull/13312
- Session: bf326360-1c79-4d5b-99d8-8508e7d44d45
2026-11-12 · douwebot run 1 failed at cfa54a7a0ed3677bfa98759e4cb150c6a41e9d28 · iter -
- Decision: Transient pipeline failure (OpenCode Go 429, GLM fallback review failed validation: missing); re-applied douwebot (attempt 2)
- Why: Manager rule: re-apply once per transient pipeline failure; hold and report after two in a row
- Source: https://github.com/pydantic/pydantic-ai/actions/runs/43327614105
- Session: bf326360-1c79-4d5b-99d8-8508e7d44d45
2026-11-12 · douwebot APPROVE at cfa54a7a0ed3677bfa98759e4cb150c6a41e9d28 · iter -
- Decision: Approved (run 43329120562, engine opencode-zai) with one Optional finding (extract the duplicated transport_status_error + google_retry_after arm): skipped, replied, reacted -1, resolved; no push
- Why: Finding reopens the settled fix shape (copy the arms, call _model_errors helpers directly; shared constructor refactor out of scope); manager rule: Optional-only APPROVE passes once threads are resolved (#13099)
- Source: https://github.com/pydantic/pydantic-ai/pull/13312#discussion_r4557773503
- Session: bf326360-1c79-4d5b-99d8-8508e7d44d45
2026-11-12 · final metadata check at cfa54a7a0ed3677bfa98759e4cb150c6a41e9d28 · iter -
- Decision: 2 corrections: added topic label httpx (kept: skill 'Keep labels current' topic rule); attribution-line replacement dismissed (body's first line equals aica-identity --kind pull-request output verbatim)
- Why: Light tier (haiku) via claude -p from detached policy-base at 052cca2c27af5d1f169d504639822587175060a0; title, body sections, 40-line limit, decisions mirror and anchors passed
- Source: https://github.com/pydantic/pydantic-ai/pull/13312
- Session: bf326360-1c79-4d5b-99d8-8508e7d44d45
2026-11-12 · step 7 second opinion: pydanty:review-lite · iter -
- Decision: David picked pydanty:review-lite (my recommendation was none)
- Why: It changes which error class users get, as #13269 did; manager relay for David
- Source: https://github.com/pydantic/pydantic-ai/pull/13312
- Session: bf326360-1c79-4d5b-99d8-8508e7d44d45
2026-11-13 · standing rule: GitHub comments need no approval · iter -
- Decision: "GitHub comments need no approval from David or me, on any PR or issue. Closing PRs and issues, merging and code changes keep their existing rules." When a finding touches more than one PR or issue, post the main comment where it belongs, then a short pointer linking it on each related PR and issue
- Why: Standing rule from David, relayed by the manager 2026-11-13
- Source: https://github.com/pydantic/pydantic-ai/pull/13312
- Session: bf326360-1c79-4d5b-99d8-8508e7d44d45
2026-11-13 · pydanty review-lite triage at cfa54a7a0ed3677bfa98759e4cb150c6a41e9d28 · iter -
- Decision: F3 compat warning: done (compatibility impact label + [!WARNING] in body, no new head). F1 batch _map_api_errors + F2 batch cleanup suppress guards: out of scope, tracked in #13313 (manager split; same on main; _google_batch.py untouched). F4 test placement: declined (settled tests decision; bug-fix test beside test_google_count_tokens_api_error_is_wrapped). F5 unwrap chain: refuted (hunk tuple is (errors.APIError, httpx2.TransportError, httpx2.HTTPStatusError) as in _generate_content; no _unwrap_exception_chain exists; tests assert raw cause; verifier ran the real retrying transport through count_tokens)
- Why: agent_docs/api-design.md: 'Treat previously valid user code that now needs modification as a compatibility impact'; root AGENTS.md scope rule binds review findings
- Source: https://github.com/pydantic/pydantic-ai/pull/13312#issuecomment-6839416995
- Session: bf326360-1c79-4d5b-99d8-8508e7d44d45
2026-11-13 · final metadata check (re-run after pydanty triage) at cfa54a7a0ed3677bfa98759e4cb150c6a41e9d28 · iter -
- Decision: 1 correction kept: 'What changes for existing users' merged into one sentence
- Why: Skill 'Write the title and body' item 5: 'State the effect in one sentence.'; re-run because the compat warning changed the body after the first check
- Source: https://github.com/pydantic/pydantic-ai/pull/13312
- Session: bf326360-1c79-4d5b-99d8-8508e7d44d45
Checklist
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
- PR title is fit for the release changelog.