feat: add server-side fallbacks - #32
Merged
Merged
Conversation
Some models decline requests in certain policy areas. With `fallbacks:`
on `ClaudeLanguageModel`, the API retries a declined request on
substitute models within the same request, and the response continues
where the declining model stopped:
ClaudeLanguageModel(name: .opus5, auth: auth, fallbacks: [.opus4_8])
- `fallbacks` takes up to three models, or `.serverDefault` for the
model's default configuration. A list sends the
`server-side-fallback-2026-06-01` beta, and `.serverDefault` sends
`server-side-fallback-2026-07-01`, which it needs. The beta is added
after any `anthropic-beta` values a `.proxied` relay already sends.
- Each fallback gets the thinking and effort it accepts. An entry's
field replaces the request's whole field, so an entry sends complete
values: a fallback that doesn't think gets thinking turned off, and
its `output_config` keeps the request's schema. With `fixedEffort:`,
each fallback gets the closest level it accepts. Sampling, a schema,
and images can't differ per entry, so those need every model in the
chain.
- `fallback` blocks are recorded and replayed in place, because the
API rejects the thinking before a handover if the block is missing. A
request that replays one sends the beta even without `fallbacks`, so
a transcript from a fallback session stays usable.
- `ClaudeModelHandover`, `claudeHandovers`, `claudeHandover(for:)`, and
`claudeModelID` report handovers and the model that served each
response. After a handover the API routes the conversation straight
to the fallback model for a while; those responses carry no block,
and `claudeModelID` still names the model.
dtmeadows-ant
approved these changes
Sep 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds server-side fallbacks. Some models decline requests in certain policy areas, such as cybersecurity or biology. With
fallbacks:onClaudeLanguageModel, the API retries a declined request on substitute models within the same request, and the response continues where the declining model stopped:This follows the server-side
fallbackssupport in the TypeScript and Java SDKs, and it uses the same stream handling as their message accumulators.fallbackstakes up to three models, or.serverDefaultfor the model's default configuration.server-side-fallback-2026-06-01..serverDefaultsendsserver-side-fallback-2026-07-01, which it needs. Each form sends the narrowest value that admits it. The beta is added after anyanthropic-betavalues that a.proxiedrelay sends.output_configkeeps the request's schema. WithfixedEffort:, each fallback gets the closest level it accepts. A fallback entry can't change sampling, a schema, or images, so those need every model in the chain.fallbackblocks are recorded and replayed in place, because the API rejects the thinking before a handover if the block is missing. A request that replays one also sends the beta, even withoutfallbacks, so a transcript from a fallback session stays usable.claudeHandovers,claudeHandover(for:), andclaudeModelID. A handover is aClaudeModelHandover. After a handover, the API routes the conversation straight to the fallback model for a while, and those responses carry no handover block.claudeModelIDstill reports the model that served them.Out of scope: client-side fallbacks, like the TypeScript SDK's refusal middleware, for platforms without server-side support. This package talks to the Anthropic API directly.
Test plan
Live requests in the shapes this sends, on
claude-opus-5withclaude-opus-4-8as the fallback, underserver-side-fallback-2026-06-01:thinking: {"type": "disabled"},output_config: {}, and anoutput_configthat carries the schema, with or without an effort.fallbackblock returns 200 with the beta, and 400 without it.anthropic-betaheader returns 200."default"returns 400 under this beta, and 200 underserver-side-fallback-2026-07-01.I couldn't trigger a real handover with a benign prompt. The handover path is covered by unit tests, using a stream fixture in the shape the other SDKs handle.
The full test suite at 7ceb16a, via
xcodebuildon macOS 26.6 with Xcode 27.0 beta 6 (27A5252f) and the iOS 27.0 simulator:** TEST SUCCEEDED **, 240 tests, 237 passed, 0 failed, 3 skipped. The 3 skips are the keychain-backedAppAttestStoreTests, which skip by design under this runner.