Count a nested `FallbackModel`'s rejected responses when an outer fallback handles the error it raises
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.
| Case | Before | After |
|---|---|---|
Outer FallbackModel recovers | 45 tokens; 150-token and 0.015 limits don't trip | 165; both limits raise UsageLimitExceeded |
The outer FallbackModel then fails too (unhandled error or exhausted group) | 0 tokens | 120 |
SpendLimits on the nested model | charges the answer only | also charges the rejected response |
ModelFallback over a FallbackModel, or a RetryModelRequest hook around one | usage already 165; failed_attempts omits primary | lists primary as rejected, like the exhausted-group form |
| Streamed nested chain | failed_attempts omits the inner model's error | lists it |
Verification
Each new case fails on main with the behavior above and passes here:
test_nested_fallback_keeps_the_inner_attempts[unhandled]test_rejected_usage_on_an_error_another_fallback_recovers_from_counts_once(outerFallbackModel,ModelFallback,RetryModelRequest× within / tokens / cost limits; the within case fails if the usage is counted twice)test_rejected_usage_on_an_error_an_outer_fallback_recovers_from_survives_its_failuretest_nested_fallback_stream_keeps_the_inner_attemptsTestFallbackAttempts::test_a_nested_rejection_is_charged_when_an_outer_fallback_recovers_from_the_inner_error
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_attemptsis called only frommodels/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:rejectedattempt infailed_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 -
- Decision: Open our own PR; credit xxx-x0 in the body and with a Co-authored-by trailer on the fix commit; reuse their test shape (unhandled case, carried_usage_counts_once); then close https://github.com/pydantic/pydantic-ai/pull/12863 with a credit comment linking ours
- Why: David chose own PR; maintainer_can_modify is false; the fix line is theirs
- Source: https://github.com/pydantic/pydantic-ai/issues/12853
- Session: 0a5dbad6-274d-4d41-a3c8-1201f5f412aa
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 -
- Decision: Post a short pointer on https://github.com/pydantic/pydantic-ai/pull/11932 linking our PR once open
- Why: 11932 edits the
failed_attempt(...)line in AttemptLog.record; the two conflict - Source: https://github.com/pydantic/pydantic-ai/issues/12853
- Session: 0a5dbad6-274d-4d41-a3c8-1201f5f412aa
2026-11-13 · Durable wrapper carried-attribute loss out of scope · iter -
- Decision: Do not fold whether TemporalModel drops the carried attribute across its activity boundary; report only if reproduced
- Why: Unverified
- Source: https://github.com/pydantic/pydantic-ai/issues/12853
- Session: 0a5dbad6-274d-4d41-a3c8-1201f5f412aa
2026-11-13 · pre-push review 1 of 3 at e2c1135b62650344f7a672319d4f9761704f2657 · iter -
- Decision: reserved; dispatching (haiku, claude -p from policy-base checkout, read-only tools)
- Why: tree 01f3874e8136ef0dfd31ad73c249f3d3f1284bca; scope 6d334062f5959a6d34bf939153d4e5645ae4e816..e2c1135b62650344f7a672319d4f9761704f2657 (full branch); first review
- Source: https://github.com/pydantic/pydantic-ai/tree/nested-fallback-usage
- Session: 0a5dbad6-274d-4d41-a3c8-1201f5f412aa
2026-11-13 · pre-push review 1 of 3 at e2c1135b62650344f7a672319d4f9761704f2657 · iter -
- Decision: remediated: extended the AttemptLog.record comment to name the carried-error branch; skipped docs drift (docs/models/overview.md 'Failed attempts and usage' already promises the fixed behavior) and Literal test params (file types parametrize args as str; pyright doesn't check parametrize values)
- Why: tree 01f3874e8136ef0dfd31ad73c249f3d3f1284bca; scope 6d334062f..e2c1135b6 (full branch); first review
- Source: https://github.com/pydantic/pydantic-ai/tree/nested-fallback-usage
- Session: 0a5dbad6-274d-4d41-a3c8-1201f5f412aa
2026-11-13 · comment-only delta f9d2a1828 exempt from re-review · iter -
- Decision: push f9d2a1828 without a second pre-push review; chained from reviewed e2c1135b62650344f7a672319d4f9761704f2657
- Why: git diff e2c1135b6..f9d2a1828 has no non-comment line (exempt-delta table: comments only, no docs/** page)
- Source: https://github.com/pydantic/pydantic-ai/tree/nested-fallback-usage
- Session: 0a5dbad6-274d-4d41-a3c8-1201f5f412aa
2026-11-13 · step-7 second opinion: pydanty:review-lite · iter -
- Decision: David picked pydanty:review-lite as the second opinion for head f9d2a1828a72a23ee7ef994ea160ace112a1c09b
- Why: Narrow diff that changes accounting behavior in the shared AttemptLog.record; review-lite reports only and has no CI precondition
- Source: https://github.com/pydantic/pydantic-ai/pull/13325
- Session: 0a5dbad6-274d-4d41-a3c8-1201f5f412aa
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 -
- Decision: no change here; extended the pointer on 11932 with the semantic resolution (keep the carried_attempts else branch and pass kind=kind)
- Why: the conflict is resolved by whichever PR lands second
- Source: https://github.com/pydantic/pydantic-ai/pull/13325#issuecomment-6841248412
- Session: 0a5dbad6-274d-4d41-a3c8-1201f5f412aa
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.