Raise a gave-up transport status from OpenAI and Cohere embeddings, OpenAI image generation and `GoogleModel` batch methods as `ModelHTTPError`

Merged
DS
22d ago
+196-12

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

Closes #13313

Why we make these changes

A retrying transport re-raises HTTPStatusError for the status it gave up on, and openai, cohere and google.genai let it through unwrapped. The chat model classes map it since #11934, and #13312 does the same for Google embeddings, image generation and count_tokens. Four more call sites let the raw exception escape, which contradicts docs/embeddings.md and docs/image-generation.md:

Call siteBeforeAfter
OpenAIEmbeddingModel.embedraw httpx2.HTTPStatusErrorcategorized ModelHTTPError
OpenAIImageGenerationModel.generate (images.generate and images.edit)raw httpx2.HTTPStatusErrorcategorized ModelHTTPError
CohereEmbeddingModel.embed / count_tokensraw httpx.HTTPStatusErrorcategorized ModelHTTPError
GoogleModel.batch_create / batch_status / batch_results / batch_cancelraw httpx2.HTTPStatusErrorcategorized ModelHTTPError, with Gemini's RetryInfo wait

Each arm copies an existing one: models/openai/_errors.py::map_api_errors for OpenAI, models/cohere.py::_map_api_errors for Cohere (cohere takes a legacy httpx client), and models/google.py::_map_api_error's branch for the batch methods.

The batch input-file deletes are best effort, but a gave-up status on one escaped: on Google and OpenAI it aborted batch_results for a batch that succeeded, and on a refused batch_create it replaced the refusal. The deletes now suppress it, as they already suppress connection errors.

[!WARNING]
Compatibility impact: Existing code that catches a raw httpx2.HTTPStatusError (or, from Cohere embeddings, httpx.HTTPStatusError) from any of the call sites above will no longer catch it. It is now raised as a ModelHTTPError with its error category, with the httpx/httpx2 exception as its __cause__. A gave-up status on a batch input-file delete no longer raises at all.

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 chat model 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, or ModelAPIError for all of them.

New public surface

None.

User-visible behavior

model = OpenAIEmbeddingModel('text-embedding-3-small', provider=provider)
await model.embed(['hi'], input_type='query')  # the transport gave up on a 429 with `Retry-After: 7`
# before: httpx2.HTTPStatusError
# after:  ModelRateLimitError (a ModelHTTPError) with status_code=429, retry_after=7.0

[!NOTE]
Deliberate scope: with a validate_response that rejects every 4xx, an OpenAI image moderation_blocked 400 raises ModelRequestRejectedError, not the ContentFilterError the SDK-seen path raises. The transport closes the rejected response unread (#13315), so the error code isn't readable here. Tracked in #13330.

Verification

Each test below fails on main and passes here; for the same statuses seen by the SDK, and for each provider's chat model, outputs are byte-identical to main. No recording: the transport raises HTTPStatusError itself, so there is no response to record.

What changes for existing users

A status a retrying transport gave up on now raises the documented ModelHTTPError category from these call sites, and no longer aborts a batch's best-effort input-file delete; every other input behaves as before.

Follow-ups, out of scope here: #13328 (batch_create leaves its input file after a gave-up 4xx), #13329 (OpenAI image generation's SDK-seen status error is uncategorized, the counterpart of #13314), #13330 (the moderation gap above), #13349 (a gave-up long-decode continuation request in GoogleModel.request_stream, reported by @v0ropaev).

PR decisions

2026-11-13 · fix shape: copy the existing gave-up arms · iter -

  • Decision: OpenAI embeddings/images and Cohere use _model_errors.transport_status_error as models/openai/_errors.py::map_api_errors and models/cohere.py::_map_api_errors do; Google adds httpx2.HTTPStatusError to models/google.py::_map_api_errors, reaching _map_api_error's branch with google_retry_after
  • Why: Manager-settled; validator applied exactly these arms to a copy: every leak became the categorized error, controls and SDK-seen outputs byte-identical to main
  • Source: https://github.com/pydantic/pydantic-ai/issues/13313
  • Session: f11a33e0-9f19-4f84-a466-194d8c19ab2e

2026-11-13 · fold: best-effort input-file delete tolerates a gave-up status (D2) · iter -

  • Decision: Add httpx2.HTTPStatusError to both _google_batch.py delete suppress tuples (batch_results, batch_create), failing test first next to test_gemini_batch_results_survive_failing_to_delete_the_input_file
  • Why: A gave-up status on the delete aborts batch_results for a job that succeeded (validator repro B)
  • Source: https://github.com/pydantic/pydantic-ai/issues/13313
  • Session: f11a33e0-9f19-4f84-a466-194d8c19ab2e

2026-11-13 · fold: OpenAI batch delete gap reproduced · iter -

  • Decision: models/openai/_batch.py suppress(APIError) misses a gave-up httpx2.HTTPStatusError; reproduced (local-notes/readiness/repro_openai_batch_delete.py: delete gives up with 503 -> batch_results raises ModelUnavailableError; connect error on the delete -> results returned), so it folds
  • Why: Manager rule: reproduce first, fold only if it reproduces
  • Source: https://github.com/pydantic/pydantic-ai/issues/13313
  • Session: f11a33e0-9f19-4f84-a466-194d8c19ab2e

2026-11-13 · split: batch_create input file after gave-up 4xx + OpenAI image SDK-seen arm uncategorized · iter -

  • Decision: File two follow-ups via /file-followup-issue after the PR opens, no routing labels, no assignee: (1) batch_create leaves the uploaded file after a gave-up 4xx (repro C), deleting is a design question; (2) OpenAI image APIStatusError arm builds an uncategorized ModelHTTPError, OpenAI counterpart of #13314
  • Why: Manager-settled split; manager routes them
  • Source: https://github.com/pydantic/pydantic-ai/issues/13313
  • Session: f11a33e0-9f19-4f84-a466-194d8c19ab2e

2026-11-13 · sequencing with #13312 · iter -

2026-11-13 · tests: placement and no recording · iter -

  • Decision: Embeddings as Case rows in tests/test_embeddings_error_categories.py; OpenAI images next to tests/test_images.py::test_openai_image_edit_status_error; batch in tests/models/test_google_batch.py; docstrings say no recording because the transport raises, so there is no response to record
  • Why: Manager-settled
  • Source: https://github.com/pydantic/pydantic-ai/issues/13313
  • Session: f11a33e0-9f19-4f84-a466-194d8c19ab2e

2026-11-13 · D1 deliberator: deployed agent unavailable · iter -

  • Decision: The manager-only deliberator agent is not deployed in this worktree; dispatched a fresh opus general-purpose agent instructed to follow /home/agent/pydantic/ai/manager/.claude/agents/deliberator.md verbatim
  • Why: Closest faithful substitute; reported to the manager
  • Source: https://github.com/pydantic/pydantic-ai/issues/13313
  • Session: f11a33e0-9f19-4f84-a466-194d8c19ab2e

2026-11-13 · D1 verdict: OpenAI image gave-up arm copies the arm verbatim (Option A) · iter -

  • Decision: images/openai.py gets the plain transport_status_error arm; a gave-up moderation_blocked 400 raises ModelRequestRejectedError, not ContentFilterError; no test pins moderation_blocked -> ModelRequestRejectedError
  • Why: Deliberator DECIDE: AsyncHTTPX2TenacityTransport closes the rejected response unread (issue 13315), so over a real socket the body is None and a moderation_blocked match could never fire; all 7 sibling gave-up arms categorize by status only, incl. Azure content_filter in _chat_model.py; PR 11934 set that shape
  • Source: https://github.com/pydantic/pydantic-ai/pull/11934 https://github.com/pydantic/pydantic-ai/issues/13315
  • Session: f11a33e0-9f19-4f84-a466-194d8c19ab2e

2026-11-13 · D1 accepted; third follow-up approved · iter -

  • Decision: Manager accepted D1 Option A. After the PR opens, file three follow-ups via /file-followup-issue with no assignee and no labels (manager routes all three): repro C cleanup, OpenAI image SDK-seen arm uncategorized, OpenAI image moderation_blocked behind a 4xx-rejecting transport (blocked by 13315). PR body keeps one deliberate-scope line on the moderation gap
  • Why: Manager reply to the D1 report (local-notes/dispatch-20261113T003144Z.md)
  • Source: https://github.com/pydantic/pydantic-ai/issues/13313
  • Session: f11a33e0-9f19-4f84-a466-194d8c19ab2e

2026-11-13 · repro C wording: transport's own retry · iter -

  • Decision: The follow-up says the second batchGenerateContent send is the retrying transport's own retry, which the SDK's retry_options attempts=1 can't reach; OpenAI batch_create's with_options(max_retries=0) has the same shape by code reading only
  • Why: Repro C sets stop_after_attempt(2) on the transport; manager agreed with the pushback
  • Source: https://github.com/pydantic/pydantic-ai/issues/13313
  • Session: f11a33e0-9f19-4f84-a466-194d8c19ab2e

2026-11-13 · pre-push review 1 of 3 at 4ce9517f14a9d63c6ec7dfef257fb806ad96600f · iter -

2026-11-13 · compatibility impact label and warning · iter -

2026-11-13 · pre-push review 1 of 3 at 4ce9517f14a9d63c6ec7dfef257fb806ad96600f · iter -

2026-11-13 · defer: batch_create input file after a gave-up 4xx · iter -

2026-11-13 · defer: OpenAI image SDK-seen status error uncategorized · iter -

2026-11-13 · defer: OpenAI image moderation_blocked behind a 4xx-rejecting transport · iter -

2026-11-13 · douwebot applied at 4ce9517f14a9d63c6ec7dfef257fb806ad96600f · iter -

  • Decision: Applied the douwebot label after CI (run 43345966808) and PR Review Checks succeeded on the head
  • Why: Merge bar for size: M needs a clean douwebot approval; diff touches no guarded path
  • Source: https://github.com/pydantic/pydantic-ai/pull/13327
  • Session: f11a33e0-9f19-4f84-a466-194d8c19ab2e

2026-11-13 · douwebot optional body finding: defer to #13328 · iter -

2026-11-13 · gate gap filed: douwebot body finding blocks the clean-approval gate · iter -

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

2026-11-13 · pydanty review-lite triage at 4ce9517f14a9d63c6ec7dfef257fb806ad96600f · iter -

  • Decision: Both required actions declined, no push: (1) 3xx APIStatusError branch in images/openai.py is pre-existing (b84a7c9149, lax no cover like embeddings/openai.py), outside 13313 scope; (2) uname -p ResourceWarning in test_openai_batch.py reproduces with main's versions of all changed files, tracked in 12444 (_batch_group reads client.default_headers). Reply posted, 👎 reacted, pointer on 12444, pydanty CHANGES_REQUESTED 5885549534 dismissed via the tracked helper
  • Why: Root AGENTS.md scope rule; pushing-commits step 2: a flake not caused by this PR is cited by its issue, fixed here only with a claim and in scope; CI run 43345966808 passed on the head
  • Source: https://github.com/pydantic/pydantic-ai/pull/13327#issuecomment-6843025987
  • Session: f11a33e0-9f19-4f84-a466-194d8c19ab2e

2026-11-13 · final metadata check at 4ce9517f14a9d63c6ec7dfef257fb806ad96600f · iter -

  • Decision: current: fresh haiku checker (policy base db141af6d) confirmed title, labels, attribution, closing line, template sections and decisions mirror; its 4 corrections (bare #N to full URLs) dropped
  • Why: Each cited only a local session memory, not a rule in pushing-commits-to-the-repo, root AGENTS.md or the PR template (skill 'Before handing the PR back' step 5); GitHub links #N in PR bodies
  • Source: https://github.com/pydantic/pydantic-ai/pull/13327
  • Session: f11a33e0-9f19-4f84-a466-194d8c19ab2e

2026-11-13 · fold: 5th gave-up site, GeminiStreamedResponse._get_event_iterator · iter -

  • Decision: Add httpx2.HTTPStatusError to the except tuple in models/google.py::GeminiStreamedResponse._get_event_iterator so a long-decode continuation hop the retrying transport gave up on maps through _map_api_error; streaming test first; credit v0ropaev for report and repro
  • Why: Manager fold under the pr-orchestrator fold bar: same root cause, same fix shape, changes output only for inputs that already fail, PR not at the merge bar
  • Source: https://github.com/pydantic/pydantic-ai/issues/13313#issuecomment-6843505850
  • Session: f11a33e0-9f19-4f84-a466-194d8c19ab2e

2026-11-13 · adversarial verification of the fold at f6882ab75 · iter -

  • Decision: Fresh opus agent following the adversarial-verifier definition (no such agent type in this worktree): 1 counterexample. FallbackModel or ModelFallback with fallback_on naming httpx2.HTTPStatusError, and a first long-decode request with no events, falls back on main and fails with ModelRateLimitError on f6882ab75 (reproduced). Control: a first streamed request that gives up already maps and doesn't fall back on main (reproduced). Push held for the manager
  • Why: Contradicts part of the fold bar's premise 'changes output only for inputs that already fail'; same class as the PR's declared compatibility impact
  • Source: https://github.com/pydantic/pydantic-ai/issues/13313#issuecomment-6843505850
  • Session: f11a33e0-9f19-4f84-a466-194d8c19ab2e

2026-11-13 · split, not fold: 5th gave-up site · iter -

  • Decision: Drop f6882ab75 (never pushed; head stays 4ce9517f1) and file GeminiStreamedResponse._get_event_iterator as its own follow-up, no labels, no assignee; reply once to v0ropaev; pointer on #13327
  • Why: Manager reversal: the verifier's fallback_on=(httpx2.HTTPStatusError,) counterexample fails the fold bar's third condition, 'changes output only for inputs that already fail on main', so the fold rule says split
  • Source: https://github.com/pydantic/pydantic-ai/issues/13313#issuecomment-6843505850
  • Session: f11a33e0-9f19-4f84-a466-194d8c19ab2e
  • Supersedes: fold: 5th gave-up site, GeminiStreamedResponse._get_event_iterator

2026-11-13 · defer: long-decode stream continuation site · iter -

2026-11-13 · gate: merge main and re-run douwebot, no waiver · iter -

  • Decision: David picked (b) merge main and re-run douwebot, no waiver, on 2026-11-13
  • Why: Standing rule from David: never waive the douwebot gate; when it fails, push the fix and re-run douwebot
  • Source: https://github.com/pydantic/pydantic-ai/pull/13327
  • Session: f11a33e0-9f19-4f84-a466-194d8c19ab2e

2026-11-13 · exempt delta: main merge at 29748823f54afb2b3328555bfe13ee8cea044eb7 · iter -

  • Decision: No pre-push review dispatch (budget 1 of 3 used): target-branch merge of upstream/main 357ca4ec4 with no conflict resolution, chained from reviewed SHA 4ce9517f14a9d63c6ec7dfef257fb806ad96600f
  • Why: git diff main...4ce9517f1 and git diff main...HEAD carry identical added/removed lines (only blob index hashes and one hunk offset in tests/test_images.py differ, from #13356/#13335 touching the same files elsewhere); targeted tests over the touched files plus test_long_decode.py pass; pyright 0 errors; no pyproject/uv.lock change
  • Source: https://github.com/pydantic/pydantic-ai/pull/13327
  • Session: f11a33e0-9f19-4f84-a466-194d8c19ab2e

2026-11-13 · douwebot applied at 29748823f54afb2b3328555bfe13ee8cea044eb7 · iter -

  • Decision: Applied the douwebot label after CI (run 43485048384) and PR Review Checks succeeded on the merged head
  • Why: David's pick (b): merge main and re-run douwebot, no waiver
  • Source: https://github.com/pydantic/pydantic-ai/pull/13327
  • Session: f11a33e0-9f19-4f84-a466-194d8c19ab2e

2026-11-13 · douwebot approval at 29748823f54afb2b3328555bfe13ee8cea044eb7 · iter -

  • Decision: douwebot APPROVED (review 5896545218, run 43488463303) with a clean body and one inline Optional finding on test_gemini_refused_job_raises_its_refusal_when_deleting_its_input_file_fails; skipped as net-neutral, replied with evidence, 👎, thread resolved; no push
  • Why: test_gemini_refused_job_deletes_its_input_file[refused] already asserts the refused job's delete runs; the flagged test fails on main; a push restarts CI/PRC/douwebot for no added coverage (manager: don't push just to clear such a nit)
  • Source: https://github.com/pydantic/pydantic-ai/pull/13327#discussion_r4567465274
  • Session: f11a33e0-9f19-4f84-a466-194d8c19ab2e

2026-11-13 · merge bar met at 29748823f54afb2b3328555bfe13ee8cea044eb7 · iter -

  • Decision: CI run 43485048384 success, PR Review Checks success, Gate: clean douwebot approval success (19:09:11Z, after a PR body edit re-triggered PR Review Gates per #13322), 0 unresolved threads, mergeStateStatus CLEAN; enqueue is the manager's
  • Why: pushing-commits-to-the-repo Merge bar for size: M
  • Source: https://github.com/pydantic/pydantic-ai/pull/13327
  • Session: f11a33e0-9f19-4f84-a466-194d8c19ab2e

2026-11-13 · manager enqueued #13327 · iter -

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_01HhQjZAdM2jha9DyEFcrtq3