Count a nested `FallbackModel`'s rejected responses when an outer fallback handles the error it raises

Merged
DS
23d ago
+140-7

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

Closes #12853

Why we make these changes

An inner FallbackModel that rejects a billed response, then raises an error it doesn't fall back on, puts the rejected attempt on that error (carry_attempts). AttemptLog.record read a nested chain's attempts only from a ModelResponse or a FallbackExceptionGroup, so an outer fallback moving on from that error dropped them. The run underreported tokens and cost, and token and cost limits didn't trip. AttemptLog.record now also records the attempts an error carries. The source change is @xxx-x0's, from #12863; they are co-author of the fix commit.

New public surface

None.

User-visible behavior

The issue's chain: the inner model rejects a 120-token / 0.012 response, then raises a 503 that only the outer fallback handles, and the outer backup answers with 45 / 0.0045.

CaseBeforeAfter
Outer FallbackModel recovers45 tokens; 150-token and 0.015 limits don't trip165; both limits raise UsageLimitExceeded
The outer FallbackModel then fails too (unhandled error or exhausted group)0 tokens120
SpendLimits on the nested modelcharges the answer onlyalso charges the rejected response
ModelFallback over a FallbackModel, or a RetryModelRequest hook around oneusage already 165; failed_attempts omits primarylists primary as rejected, like the exhausted-group form
Streamed nested chainfailed_attempts omits the inner model's errorlists it

Verification

Each new case fails on main with the behavior above and passes here:

Unchanged: a flat chain whose error ends the run (120), an inner exhausted group the outer handles (165), and the outer rejecting the inner's response (210).

What changes for existing users

Runs where something recovers from, or ends on, an error a FallbackModel raised after rejecting a billed response now count that response's usage and list its attempt; nothing else changes.

PR decisions

2026-11-13 · Fix shape: one else branch in AttemptLog.record · iter -

  • Decision: Add else: self.attempts.extend(carried_attempts(failure)) next to the ModelResponse and FallbackExceptionGroup branches in _fallback.py::AttemptLog.record
  • Why: Only exceptions that carry attempts change output; carry_attempts is called only from models/fallback.py
  • Source: https://github.com/pydantic/pydantic-ai/issues/12853
  • Session: 0a5dbad6-274d-4d41-a3c8-1201f5f412aa

2026-11-13 · Records gap in cases 6 and 11 ships with the same line · iter -

  • Decision: ModelFallback over a FallbackModel and a RetryModelRequest hook around one now list the carried primary:rejected attempt in failed_attempts; pinned in a test and named in the PR body's behavior section
  • Why: Same missing-carried-attempts defect; makes those records match the exhausted-group form
  • Source: https://github.com/pydantic/pydantic-ai/issues/12853
  • Session: 0a5dbad6-274d-4d41-a3c8-1201f5f412aa

2026-11-13 · Own PR over contributor PR 12863 · iter -

2026-11-13 · Tests beyond PR 12863 · iter -

  • Decision: Add outer-then-fails full-loss (unhandled + exhausted), SpendLimits, streamed records, and case 6/11 records tests; each must fail on main
  • Why: Validator reproduced these on upstream/main 6d334062f; 12863 lacks them
  • Source: https://github.com/pydantic/pydantic-ai/issues/12853
  • Session: 0a5dbad6-274d-4d41-a3c8-1201f5f412aa

2026-11-13 · Sequencing with DouweM PR 11932 · iter -

2026-11-13 · Durable wrapper carried-attribute loss out of scope · iter -

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

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

2026-11-13 · comment-only delta f9d2a1828 exempt from re-review · iter -

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

2026-11-13 · pydanty review-lite findings 1-2: Temporal codec drops carried attempts · iter -

  • Decision: skip in this PR: pre-existing on main and documented; reported to the manager, not folded
  • Why: _model_errors._encode drops every pydantic_ai* attribute, so carried_attempts reads [] in the workflow on main and here; docs/models/overview.md durable note states the limit; settled decision keeps it out of scope
  • Source: https://github.com/pydantic/pydantic-ai/pull/13325#issuecomment-6841248412
  • Session: 0a5dbad6-274d-4d41-a3c8-1201f5f412aa

2026-11-13 · pydanty review-lite finding 3: overlap with PR 11932 · iter -

2026-11-13 · pydanty review-lite finding 4: untested table claims · iter -

  • Decision: narrowed the PR body table to what the tests pin; dropped the three-level and wrapper parenthetical
  • Why: those cases were verified only by offline repros; carry_attempts composition and the wrappers' re-raise are unchanged by this PR
  • Source: https://github.com/pydantic/pydantic-ai/pull/13325#issuecomment-6841248412
  • Session: 0a5dbad6-274d-4d41-a3c8-1201f5f412aa

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_01HhQjZ8CLKqgJcxXspubkwb