Raise an error status a retrying transport gave up on during a streamed Gemini long decode as `ModelHTTPError`
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 -
- Decision: Implement directly; no plan-only PR
- Why: Manager's dispatch settled Step 4: one-tuple fix with a mock-transport test
- Source: https://github.com/pydantic/pydantic-ai/issues/13349
- Session: 7500c01f-5bce-4d6b-b5a4-86dc1171e09d
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 -
- Decision: No docs change
- Why: docs/retries.md Error handling already describes the fixed behavior
- Source: https://github.com/pydantic/pydantic-ai/issues/13349
- Session: 7500c01f-5bce-4d6b-b5a4-86dc1171e09d
2026-11-13 · credit v0ropaev · iter -
- Decision: Credit v0ropaev in the PR body for the report and repro
- Why: Reported on #13313
- Source: https://github.com/pydantic/pydantic-ai/issues/13313#issuecomment-6843505850
- Session: 7500c01f-5bce-4d6b-b5a4-86dc1171e09d
2026-11-13 · independent of #13327 · iter -
- Decision: No dependency on #13327; _map_api_errors (batch/files) stays out of scope
- Why: No shared helper, no conflict; #13327 owns the other gave-up sites
- Source: https://github.com/pydantic/pydantic-ai/issues/13349
- Session: 7500c01f-5bce-4d6b-b5a4-86dc1171e09d
2026-11-13 · pre-push review 1 of 3 at 52b3bf1e5f7abd07db7b06ed7a82d54d04aee7dd · iter -
- Decision: reserved; dispatching (haiku, claude -p from detached policy-base checkout, read-only tools, no fan-out)
- Why: tree 84eb1a695902404ce5a6caac8077d97fb256a819; scope e65f5e9ac8ca01ffd1ad8c91d6f5a006d10d250a..52b3bf1e5f7abd07db7b06ed7a82d54d04aee7dd (full branch); policy-base af851a6675d4f703a691adac5b7ba1a089abb9fe; first review
- Source: https://github.com/pydantic/pydantic-ai/tree/gemini-stream-continuation-gave-up
- Session: 7500c01f-5bce-4d6b-b5a4-86dc1171e09d
2026-11-13 · pre-push review 1 of 3 at 52b3bf1e5f7abd07db7b06ed7a82d54d04aee7dd · iter -
- Decision: 1 finding (new cases cover GoogleProvider only, GeminiStreamedResponse is shared with Vertex): dismissed with evidence, no code change; PR body states the Vertex probe
- Why: reviewed tree 84eb1a695902404ce5a6caac8077d97fb256a819; full branch e65f5e9ac..52b3bf1e5; local-notes/repro/vertex_probe.py: Vertex continues and leaks raw HTTPStatusError on e65f5e9ac (gave-up and event-hook), maps to ModelOverloadedError with the fix; both transports reach the same except, so a vertex test dimension adds no code-path coverage
- Source: https://github.com/pydantic/pydantic-ai/tree/gemini-stream-continuation-gave-up
- Session: 7500c01f-5bce-4d6b-b5a4-86dc1171e09d
2026-11-13 · fix reaches both Google transports, defect reproduced on both · iter -
- Decision: Keep the fix in GeminiStreamedResponse, which GoogleProvider and GoogleCloudProvider share
- Why: vertex_probe.py reproduces the raw HTTPStatusError on both transports on e65f5e9ac
- Source: https://github.com/pydantic/pydantic-ai/issues/13349
- Session: 7500c01f-5bce-4d6b-b5a4-86dc1171e09d
2026-11-13 · douwebot Optional on _map_api_errors: covered by #13327 · iter -
- Decision: Replied, +1, resolved; no change here
- Why: #13327 head 4ce9517f1 adds httpx2.HTTPStatusError to map_api_errors (batch* methods)
- Source: https://github.com/pydantic/pydantic-ai/pull/13356#discussion_r4562233110
- Session: 7500c01f-5bce-4d6b-b5a4-86dc1171e09d
2026-11-13 · step 7 second opinion: pydanty:review-lite · iter -
- Decision: David picked pydanty:review-lite on 52b3bf1e5
- Why: It changes the error class users get, following the precedent of #13312 and #13335
- Source: https://github.com/pydantic/pydantic-ai/pull/13356
- Session: 7500c01f-5bce-4d6b-b5a4-86dc1171e09d
2026-11-13 · pydanty review-lite finding 1 (snapshot in shared body): declined · iter -
- Decision: No change; the tests/AGENTS.md per-case snapshot rule covers heterogeneous expectations, and the three failure cases share one expectation
- Why: The if-stream snapshot predates this PR (e65f5e9ac); splitting would triplicate one expectation
- Source: https://github.com/pydantic/pydantic-ai/pull/13356#issuecomment-6850952507
- Session: 7500c01f-5bce-4d6b-b5a4-86dc1171e09d
2026-11-13 · pydanty review-lite finding 2 (fallback/Temporal classifiers): declined · iter -
- Decision: No change; it is the accepted b1/b6/b9/c1/d1-d3 compatibility impact, covered by the PR's WARNING; default fallback_on falling back (b2/b7) raised raw on main
- Why: Precedent https://github.com/pydantic/pydantic-ai/pull/11934; repro_matrix main vs head
- Source: https://github.com/pydantic/pydantic-ai/pull/13356#issuecomment-6850952507
- Session: 7500c01f-5bce-4d6b-b5a4-86dc1171e09d
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.