Raise a Gemini API error from `GoogleImageGenerationModel.generate` as its error category, like `ModelRateLimitError`

Merged
DS
22d ago
+86-9

This pull request was posted by Claude Code using claude-opus-5-5 on behalf of David.

Closes #13314

Why we make these changes

GoogleImageGenerationModel.generate raised a plain ModelHTTPError for a Gemini API error (google.genai.errors.APIError), while GoogleEmbeddingModel and GoogleModel raise it with its error category. So except ModelRateLimitError caught a 429 RESOURCE_EXHAUSTED from embeddings and chat, but not from image generation, and provider_error_code was None.

The >= 400 arm now builds the error the way embeddings/google.py::_map_api_errors does. It leaves out the chat model's suggested_model_id, which suggests a chat model id on NOT_FOUND, as #10804 did for embeddings. The < 400 arm still raises ModelAPIError.

[!WARNING]
Compatibility impact: Existing code that matches a Gemini API error from GoogleImageGenerationModel.generate by exact type or class name may behave differently. The error is now a subclass of ModelHTTPError, so type(e) is ModelHTTPError no longer matches it, and the class name in tracebacks, repr() and OTel exception.type is the category's (e.g. ModelRateLimitError). provider_error_code is now the Gemini status (e.g. 'RESOURCE_EXHAUSTED') instead of None.

Why this can ship in a minor release: Every such error is still a ModelHTTPError with the same status code, body, headers and retry_after, so except ModelHTTPError and isinstance checks that matched before still match. This is the same impact #10804 shipped for embedding models.

Migration: Match errors with except or isinstance (e.g. except ModelRateLimitError) rather than exact-type checks or class-name strings.

New public surface

None.

User-visible behavior

Gemini errorBeforeAfter (provider_error_code)
429 RESOURCE_EXHAUSTEDModelHTTPErrorModelRateLimitError ('RESOURCE_EXHAUSTED')
503 UNAVAILABLE, overloadedModelHTTPErrorModelOverloadedError ('UNAVAILABLE')
400 INVALID_ARGUMENTModelHTTPErrorModelRequestRejectedError ('INVALID_ARGUMENT')
400 API_KEY_INVALIDModelHTTPErrorModelAuthenticationError ('INVALID_ARGUMENT')
404 NOT_FOUNDModelHTTPErrorModelNotFoundError ('NOT_FOUND')

provider_error_code was None in every case before. retry_after is unchanged.

Verification

test_google_image_generation_status_error covers the five rows above, plus a status google_status_category doesn't map (504 DEADLINE_EXCEEDED → ModelServerError from the HTTP status) and a body without status (provider_error_code stays None), through google.genai with an httpx2.MockTransport. Each case fails on main and passes here. test_google_image_generation_maps_non_http_api_error still pins the < 400 arm as ModelAPIError.

What changes for existing users

A Gemini API error from Google image generation can now be caught by its category, and carries the Gemini status as provider_error_code.

PR decisions

2026-11-13 · fix shape: inline for_category in the >= 400 arm · iter -

  • Decision: Replace only the >= 400 ModelHTTPError construction in GoogleImageGenerationModel.generate with ModelHTTPError.for_category(google_status_category(status, e.message, body) or http_status_category(status_code, e.message), ..., provider_error_code=status), then google_retry_after as today
  • Why: Copies the errors.APIError arm of embeddings/google.py::_map_api_errors
  • Source: https://github.com/pydantic/pydantic-ai/issues/13314
  • Session: a54f460c-274e-41db-8043-42b9e502270d

2026-11-13 · don't reuse the embeddings context manager whole · iter -

  • Decision: Do not wrap generate in embeddings' _map_api_errors
  • Why: For code < 400 it re-raises the raw APIError; the image model raises ModelAPIError there, pinned by tests/test_images.py::test_google_image_generation_maps_non_http_api_error
  • Source: https://github.com/pydantic/pydantic-ai/issues/13314
  • Session: a54f460c-274e-41db-8043-42b9e502270d

2026-11-13 · no suggested_model_id on NOT_FOUND · iter -

  • Decision: Do not reuse chat _map_api_error / _http_error
  • Why: On NOT_FOUND they suggest a chat model id (gemini-3-pro-image-preview -> google:gemini-3.1-pro-preview per suggest.py); #10804 set the same precedent for embeddings
  • Source: https://github.com/pydantic/pydantic-ai/issues/13314
  • Session: a54f460c-274e-41db-8043-42b9e502270d

2026-11-13 · compatibility impact · iter -

  • Decision: Add the compatibility impact label and a [!WARNING] section, as #10804 and #13312 did
  • Why: Categories subclass ModelHTTPError so except ModelHTTPError still matches; type(e) is ModelHTTPError, the class name in repr/tracebacks/OTel exception.type, and provider_error_code (None -> Gemini status) change
  • Source: https://github.com/pydantic/pydantic-ai/issues/13314
  • Session: a54f460c-274e-41db-8043-42b9e502270d

2026-11-13 · tests: mock transport, five cases · iter -

  • Decision: Parametrize next to test_google_image_generation_status_error over 429, 503, 400, API_KEY_INVALID, 404, asserting category class and provider_error_code; httpx2.MockTransport, no recording; each fails on main
  • Why: google.genai builds ClientError/ServerError itself from the mocked response
  • Source: https://github.com/pydantic/pydantic-ai/issues/13314
  • Session: a54f460c-274e-41db-8043-42b9e502270d

2026-11-13 · docs scope · iter -

2026-11-13 · realtime handshake errors split out · iter -

  • Decision: Gemini Live websockets.InvalidStatus arm (reproduced: 401/429/503 plain ModelHTTPError) and OpenAI realtime handshakes (code reading only) go to a follow-up issue via /file-followup-issue, no labels, no assignee
  • Why: Different error source on a different surface
  • Source: https://github.com/pydantic/pydantic-ai/issues/13314
  • Session: a54f460c-274e-41db-8043-42b9e502270d

2026-11-13 · pre-push review 1 of 3 at 59b1342ef91fe6382b2463a86a4f7477eae56ede · iter -

2026-11-13 · pre-push review 1 of 3 at 59b1342ef91fe6382b2463a86a4f7477eae56ede · iter -

  • Decision: remediated: test pins the exact for_category class instead of comparing class names (reviewer's suggested type(e) is can never hold, the raised class is a private subclass); dismissed: extra ModelServerError/Unavailable/QuotaExceeded cases (settled five-case scope, helpers covered by embeddings/chat tests); inline copy kept per settled fix shape
  • Why: reviewed tree bc768e6b3930c146e067b1deed090dbc7b12469d; full branch scope 75ef1a9bd..59b1342ef; headless claude -p haiku in detached policy-base 75ef1a9bd, read-only tools, no fan-out; remediation is test-code-only, an exempt delta chaining from 59b1342ef
  • Source: https://github.com/pydantic/pydantic-ai/tree/google-image-error-category
  • Session: a54f460c-274e-41db-8043-42b9e502270d

2026-11-13 · exempt delta at efca9c4e20b3e43ec60ff00e2e306883f7db81db · iter -

2026-11-13 · defer: realtime connection errors with an HTTP status are uncategorized · iter -

  • Decision: Filed as #13336 instead of fixing here; no labels, no assignee (the manager routes it)
  • Why: Out of scope for this PR — different error source on a different surface (Gemini Live reproduced; OpenAI realtime from code reading only)
  • Source: https://github.com/pydantic/pydantic-ai/issues/13336
  • Session: a54f460c-274e-41db-8043-42b9e502270d

2026-11-13 · PR Review Checks finding: HTTP-status fallback untested · iter -

  • Decision: fixed: added 504 DEADLINE_EXCEEDED (google_status_category -> None, http_status_category -> ModelServerError) and a status-less 502 (provider_error_code None) cases
  • Why: the changed line's fallback operand had no case; same expectation the chat suite pins as google-deadline-exceeded; exempt delta (test-code-only coverage) chaining from reviewed 59b1342ef, budget still 1 of 3
  • Source: https://github.com/pydantic/pydantic-ai/pull/13335#discussion_r4558963512
  • Session: a54f460c-274e-41db-8043-42b9e502270d

2026-11-13 · pre-push review 2 of 3 at 7dac2b37ebd766e890f959f8a2fd57ec15b73261 · iter -

2026-11-13 · pre-push review 2 of 3 at 7dac2b37ebd766e890f959f8a2fd57ec15b73261 · iter -

2026-11-13 · douwebot REQUEST_CHANGES on 6c77270: docs category wording · iter -

  • Decision: fixed: docs/image-generation.md Error Handling now says Google and xAI image errors are also instances of an error category, linking models/overview.md#handling-model-api-errors, as docs/embeddings.md does
  • Why: settled docs scope allows category wording scoped to Google and xAI; xAI image goes through models/xai.py::_map_api_errors (for_category); OpenAI uncategorized until #13329
  • Source: https://github.com/pydantic/pydantic-ai/pull/13335#pullrequestreview-5885423507
  • Session: a54f460c-274e-41db-8043-42b9e502270d

2026-11-13 · merge bar met at 7dac2b37ebd766e890f959f8a2fd57ec15b73261 · iter -

  • Decision: CI green, PR Review Checks success, clean douwebot APPROVE (run 43359529787, no findings), 0 unresolved threads; holding for the manager's step-7 second-opinion choice, final metadata check after it
  • Why: dispatch step 7: report PR stats and recommendation, then hold
  • Source: https://github.com/pydantic/pydantic-ai/pull/13335#pullrequestreview-5885651606
  • Session: a54f460c-274e-41db-8043-42b9e502270d

2026-11-13 · step-7 second opinion: pydanty:review-lite · iter -

2026-11-13 · pydanty review-lite optional finding: OpenAI image 429 unclassified · iter -

2026-11-13 · pydanty review-lite run df_run_f0c3d7b66163405dab67: incomplete · iter -

  • Decision: Not approved: 3 of 18 checks partial (review-precedent, review-spec-conformance, review-runtime-behavior hit the run deadline); its review-gate CHANGES_REQUESTED does not block (mergeable_state clean); one optional finding triaged as skip; re-run held for the manager
  • Why: pydanty asks for a re-run before relying on it; whether to re-apply review-lite is not settled in the dispatch
  • Source: https://github.com/pydantic/pydantic-ai/pull/13335#issuecomment-6843225537
  • Session: a54f460c-274e-41db-8043-42b9e502270d

2026-11-13 · final metadata check at 7dac2b37ebd766e890f959f8a2fd57ec15b73261 · iter -

  • Decision: current: fresh haiku subagent in detached policy-base db141af6d returned current; no corrections
  • Why: visible body 26 lines; title, labels and compatibility-impact warning checked against pushing-commits-to-the-repo, root AGENTS.md and the PR template
  • Source: https://github.com/pydantic/pydantic-ai/pull/13335
  • Session: a54f460c-274e-41db-8043-42b9e502270d

2026-11-13 · pydanty review-lite re-run, once · iter -

  • Decision: Re-apply pydanty:review-lite on 7dac2b37 one time; if complete with no required findings, dismiss both CHANGES_REQUESTED via the tracked helper and report for enqueue; if incomplete again, do not re-apply and report the partial lanes
  • Why: run df_run_f0c3d7b66163405dab67 ended with precedent, spec-conformance and runtime-behavior partial at its budget deadline, so the second opinion David picked was not delivered
  • Source: https://github.com/pydantic/pydantic-ai/pull/13335#issuecomment-6843225537
  • Session: a54f460c-274e-41db-8043-42b9e502270d

2026-11-13 · accept a partial review-lite run 2 · iter -

  • Decision: If review-lite run 2 also ends partial, accept it without reporting back: once it posts and pydanty:is-working comes off, triage it, dismiss the CHANGES_REQUESTED reviews through the tracked helper if no required finding holds, and report the gate state for the manager's enqueue
  • Why: Every review-lite run so far has ended partial at pydanty's budget deadline (#13312, #13325, #13326, #13327, #13332); #13312 and #13325 merged after one
  • Source: https://github.com/pydantic/pydantic-ai/pull/13335
  • Session: a54f460c-274e-41db-8043-42b9e502270d
  • Supersedes: pydanty review-lite re-run, once

2026-11-13 · pydanty review-lite run 2 df_run_7f72837d11624343b2d1: partial, accepted · iter -

  • Decision: Accepted per the manager's correction: 4 of 19 lanes partial (runtime-behavior, spec-conformance, compat-impact-equipped, sibling-consistency), no required finding; optional OpenAI 429 note already tracked by #13329 (reacted, no new reply); dismissed pydanty CHANGES_REQUESTED 5885965190 and 5886471198 via the tracked helper
  • Why: partial lanes report no defect, and they ran out of budget on probes the status-error test already pins
  • Source: https://github.com/pydantic/pydantic-ai/pull/13335#issuecomment-6843946016
  • Session: a54f460c-274e-41db-8043-42b9e502270d

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.

https://claude.ai/code/session_01HhQjZCxX1J1hcE7jkkN2iD