Raise an error status a retrying transport gave up on during a streamed Gemini long decode as `ModelHTTPError`

Merged
DS
22d ago
+35-11

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

Closes #13349

On a long decode, google-genai sends the continuation request from inside the stream iterator. GeminiStreamedResponse._get_event_iterator caught errors.APIError and httpx2.TransportError there, but not the httpx2.HTTPStatusError that a retrying transport raises for a status it gave up on, or that a response event hook calling raise_for_status raises. That error escaped run_stream raw. GoogleModel's other three request sites already catch it, and _map_api_error already maps it. This PR adds it to the stream's except. Reported, with a repro, by @v0ropaev on #13313.

[!WARNING]
Compatibility impact: A continuation request's error status in a streamed Gemini long decode, raised by a retrying transport or a response event hook, is now raised as ModelHTTPError with its error category, not as a raw httpx2.HTTPStatusError. Code that names httpx2.HTTPStatusError no longer matches it: except httpx2.HTTPStatusError around run_stream, and a FallbackModel or ModelFallback fallback_on that names it, which fell back only when the stream's first request had yielded no event. A Temporal activity now reports it as ModelHTTPError, so a retry policy whose non_retryable_error_types names HTTPStatusError no longer matches it, and the default policy stops retrying a 4xx and waits a 429's retry_after.

Why this can ship in a minor release: #11934 made the same change on this stream's first request, and the retries guide already documents the new behavior.

Migration: Catch ModelHTTPError or its category, such as ModelRateLimitError, or name one of them in fallback_on. The default fallback_on already falls back on them.

New public surface: none.

User-visible behavior: a long decode streamed through the retries guide's AsyncHTTPX2TenacityTransport, whose continuation request gets a 429 the transport gives up on:

 async with agent.run_stream('Count to 100,000.') as result:
     await result.get_output()
-# raises httpx2.HTTPStatusError
+# raises ModelRateLimitError, status_code 429, retry_after read from Gemini's RetryInfo

Verification: test_failed_continuation_request_raises gains transport-gave-up and event-hook cases, streamed and not, on httpx2.MockTransport. The two streamed cases fail on main with the raw httpx2.HTTPStatusError; all six pass with the fix. No recording: the transport raises the error itself. GoogleCloudProvider streams through the same GeminiStreamedResponse; a mock-transport probe reproduced the raw error there on main too, and the fix maps it.

What changes for existing users: only the code the warning names; a plain run_stream that raised the raw error now raises a ModelHTTPError, and the default fallback_on now falls back on it.

PR decisions

2026-11-13 · complexity: simple, implement directly · iter -

2026-11-13 · fix: add httpx2.HTTPStatusError to the stream iterator's except · iter -

  • Decision: Add httpx2.HTTPStatusError to GeminiStreamedResponse._get_event_iterator's except tuple; _map_api_error already maps it
  • Why: Reproduced on e65f5e9ac: a3 (429) and a5 (503) escape raw; the other three GoogleModel request sites already catch it
  • Source: https://github.com/pydantic/pydantic-ai/issues/13349
  • Session: 7500c01f-5bce-4d6b-b5a4-86dc1171e09d

2026-11-13 · behavior change accepted by precedent · iter -

  • Decision: Accept the change for b1 FallbackModel(fallback_on=(httpx2.HTTPStatusError,)) with a no-event first request, b6 ModelFallback with that fallback_on, b9 callable fallback_on checking HTTPStatusError, c1 user except httpx2.HTTPStatusError around run_stream, d1-d3 Temporal activity error type
  • Why: https://github.com/pydantic/pydantic-ai/pull/11934 (DouweM, compatibility impact) made the same change on the first streamed request; precedent_11934.py reproduces that flip; docs/retries.md Error handling already documents it
  • Source: https://github.com/pydantic/pydantic-ai/issues/13349
  • Session: 7500c01f-5bce-4d6b-b5a4-86dc1171e09d

2026-11-13 · compatibility impact label and WARNING section · iter -

  • Decision: Add compatibility impact + [!WARNING] naming except httpx2.HTTPStatusError, a fallback_on / ModelFallback naming httpx2.HTTPStatusError, and a Temporal non_retryable_error_types naming HTTPStatusError; migration: catch ModelHTTPError or a category; default fallback_on already covers it; link #11934
  • Why: Inputs that work on main change outcome
  • Source: https://github.com/pydantic/pydantic-ai/issues/13349
  • Session: 7500c01f-5bce-4d6b-b5a4-86dc1171e09d

2026-11-13 · tests: parametrize test_failed_continuation_request_raises · iter -

  • Decision: Add a transport-gave-up case (fails on main) and an httpx response event hook calling raise_for_status case as parameters of the one test; no new test function, no recording
  • Why: The transport raises the error itself, so MockTransport pins it; the event hook reaches the same except
  • Source: https://github.com/pydantic/pydantic-ai/issues/13349
  • Session: 7500c01f-5bce-4d6b-b5a4-86dc1171e09d

2026-11-13 · docs: none · iter -

2026-11-13 · credit v0ropaev · iter -

2026-11-13 · independent of #13327 · iter -

2026-11-13 · pre-push review 1 of 3 at 52b3bf1e5f7abd07db7b06ed7a82d54d04aee7dd · iter -

2026-11-13 · pre-push review 1 of 3 at 52b3bf1e5f7abd07db7b06ed7a82d54d04aee7dd · iter -

2026-11-13 · fix reaches both Google transports, defect reproduced on both · iter -

2026-11-13 · douwebot Optional on _map_api_errors: covered by #13327 · iter -

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

2026-11-13 · pydanty review-lite finding 1 (snapshot in shared body): declined · iter -

2026-11-13 · pydanty review-lite finding 2 (fallback/Temporal classifiers): declined · iter -

2026-11-13 · final metadata check · iter -

  • Decision: 2 corrections kept: added temporal and google-cloud topic labels
  • Why: pushing-commits-to-the-repo topic-label rule; WARNING carries a Temporal migration, fix reaches GoogleCloudProvider
  • Source: https://github.com/pydantic/pydantic-ai/pull/13356
  • Session: 7500c01f-5bce-4d6b-b5a4-86dc1171e09d

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_01HhQjZ8qqorhdn2GH3pRvvU