Fall back to the next model in `ModelFallback` when a model refuses, and let `ContentFilterError` in `fallback_on` match refusals
Part of #9335. No closing keyword: there's no dedicated issue. Supersedes #12019, whose refusal docs are folded in and rewritten for the new default.
When a model refuses, ModelFallback now moves on to the next model. A refusal is a response the provider's content filter ended (finish_reason='content_filter'): Anthropic's refusal stop reason, OpenAI's refusal, Azure's content filter.
Until now, the default fallback_on=(ModelAPIError,) accepted a refusal. An empty or thinking-only refusal then ended the run with ContentFilterError. Passing fallback_on=(ContentFilterError,) was accepted, but it could never match, because that error is raised after fallback has already accepted the response.
ModelFallback shipped in v2.64.0, so changing its default is a compatibility impact; it has API-compat waivers against v2.64.0 (see the warning above).
[!WARNING]
Compatibility impact: ModelFallback (new in v2.64.0) now falls back when a model refuses (finish_reason='content_filter'). An empty refusal used to end the run with ContentFilterError, and one with text used to be the run's output; both now move on to the next model. Separately, FallbackModel code that already names ContentFilterError in fallback_on now falls back on refusals. Before, that entry silently never matched: it was the trap documented in #6565.
Why this can ship in a minor release: ModelFallback shipped days ago, and a capability like it gets to do the right thing by default. Anthropic's refusals guide says to discard a refused response's partial output and retry on a fallback model. The core Agent keeps its backward-compatible content-filter behavior. For FallbackModel, the ContentFilterError entry now does what it says.
Migration: pass fallback_on=(ModelAPIError,) to ModelFallback to keep the v2.64.0 behavior, and remove ContentFilterError from a FallbackModel's fallback_on to keep its old behavior.
Release note: ModelFallback now falls back to the next model when a model refuses; opt out with fallback_on=(ModelAPIError,).
What changed
ContentFilterErrorinfallback_onnow rejects any response withfinish_reason='content_filter', whether it's empty, thinking-only or has partial text. This lives in the shared predicate parsing, so it works forFallbackModeltoo. OnlyContentFilterErrorand its subclasses imply it; supertypes likeUnexpectedModelBehaviororExceptiondon't.ModelFallbackdefaults to(ModelAPIError, ContentFilterError), in Python and in agent specs. A spec'sfallback_oncan now nameContentFilterError.- Opting out with
fallback_on=(ModelAPIError,)restores today's behavior. An empty refusal raisesContentFilterError, and a refusal with text is the run's output, which is the default established in #5221.RaiseContentFilterError(#6167) still makes those text refusals raise. FallbackModel's default is unchanged at(ModelAPIError,).- One observable change on a released surface:
FallbackModelcode that already namesContentFilterErrorinfallback_on, e.g.FallbackModel(a, b, fallback_on=(ModelAPIError, ContentFilterError)). That entry was a documented no-op. With this PR, a refusal moves on tobinstead of ending the run (empty refusal) or becoming the output (refusal with text). That's most likely what such code meant. Code that doesn't nameContentFilterErroris unaffected. - Streamed requests are unchanged. A streamed refusal is only known once it has been streamed, so it's accepted, as today. This implicit check is not a response handler, so it doesn't add the "response handlers are not applied to streamed requests"
UserWarningto every streamed request. sticky: a refusal that fell back records a rejected attempt, so the conversation sticks to the model that answered. There's a test for this.
Why partial-text refusals fall back too
- Anthropic's refusals guide says to "treat any partial output as incomplete and discard it". It also says "re-sending a refused request to the same model usually earns another refusal. Point the retry at the fallback model", and to "keep using the fallback model for subsequent turns", which is what
sticky=Truedoes. - google-genai's chat helper drops blocked turns from history.
- Bedrock guardrails re-block blocked input on every turn.
Docs
model-fallback.md:- "When to fall back" states the new default.
- The "Falling back when a model refuses" section from #12019 is rewritten. Refusals fall back by default, there's an opt-out, and
FallbackModelneedsfallback_on=(ModelAPIError, ContentFilterError). It includes the safety point (the backup model applies its own provider's safety policy), the streaming caveat, theContentFilterErrormessage andprovider_details['refusal']notes, and Anthropic'sreasoning_extractionrefusals, which come from the prompt. The "ContentFilterErrorinfallback_ondoesn't work" bullet is dropped. - Migration: a "Behavior differences" list, which has one entry: refusals fall back by default.
models/overview.md: theFallbackModelnote claimedfallback_on=(ContentFilterError,)can never match. That's corrected; see #6565 for the earlier docs bug about whatFallbackModelcatches.models/anthropic.md"Refusals on repeated prompts" told users to "configure a fallback model", which wasn't enough until now. It now points toModelFallback.troubleshooting.md(from #12019) and the skill reference are updated.
Tests
- A real adapter refusal falling back under the default: Anthropic (
stop_reason='refusal') and OpenAI chat (refusal). - An OpenAI streamed refusal: no warning, and the run ends with
ContentFilterError. - Empty and text refusals falling back by default.
- The opt-out: an empty refusal raises, and a text refusal is the output.
- Supertypes don't imply refusals.
- All models refusing raises
FallbackExceptionGroup. FallbackModelwith and withoutContentFilterError.- The spec default, the opt-out, and
ContentFilterErrorby name. sticky.
Checklist
- No breaking changes in accordance with the version policy. The
ModelFallbackdefault change is a permitted compatibility impact (see the warning);FallbackModel's default is unchanged. - 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.
🤖 Generated with Claude Code