fix(routing): auto-map respects explicit [[models]] entries#293
Merged
Destynova2 merged 1 commit intomainfrom Apr 26, 2026
Merged
fix(routing): auto-map respects explicit [[models]] entries#293Destynova2 merged 1 commit intomainfrom
Destynova2 merged 1 commit intomainfrom
Conversation
`Router::route` step 3 (auto-mapping) was rewriting `request.model` to
`router.default` for any name matching `auto_map_regex` — even when the
user had an explicit `[[models]]` virtual entry with that name. The
virtual entry's fallback chain was therefore bypassed entirely; in
practice the virtual name `default-model` then leaked to pass-through
providers downstream, producing errors like:
Provider API error: 400 - {"error":{"message":"default-model is not
a valid model ID","code":400},"user_id":"user_..."}
The error surfaces specifically when:
1. Client sends `claude-sonnet-4-6`.
2. Auto-map regex `^claude-` matches → `request.model` rewritten to
`default-model`.
3. The user's explicit `[[models]] name = "claude-sonnet-4-6"` with
fallbacks (anthropic native → OR `anthropic/claude-sonnet-4.6` →
`deepseek-v4-flash`) is never consulted.
4. `default-model` chain runs; on exhaustion `try_direct_provider_lookup`
forwards `model = "default-model"` to a pass-through provider
(OpenRouter), which 400s.
Fix: skip the auto-map rewrite when `config.models` already contains an
entry whose name equals `request.model`. The check is regex-agnostic, so
it covers every auto_map_regex the user might configure (claude, gpt,
gemini, mixed, etc.) — there is no provider-specific code path here.
Auto-map continues to rewrite the model when no explicit virtual exists,
preserving the original "catch-all rewrite for unconfigured models"
intent that `test_auto_map_claude_models` and `test_auto_map_custom_regex`
exercise.
Adds two regression tests:
- `test_auto_map_skips_explicit_virtual_model` — the new behaviour.
- `test_auto_map_still_rewrites_unmapped_claude` — counter-test confirming
the original catch-all path is intact.
Tests: 9/9 auto-map tests pass.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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
Router::routestep 3 (auto-mapping) was rewritingrequest.modeltorouter.defaultfor any name matchingauto_map_regex— even when the user had an explicit[[models]]virtual entry with that name. The virtual entry's fallback chain was therefore bypassed entirely.The bug surfaces in production with errors like:
Repro
claude-sonnet-4-6.^claude-matches →request.modelrewritten todefault-model.[[models]] name = "claude-sonnet-4-6"with fallbacks (anthropic native → ORanthropic/claude-sonnet-4.6→deepseek-v4-flash) is never consulted.default-modelchain runs; on exhaustiontry_direct_provider_lookupforwardsmodel = "default-model"to a pass-through provider (OpenRouter), which 400s.This is the failure mode I hit while debugging Anthropic 429 fallback chains: when the primary
anthropic/claude-sonnet-4-6mapping returned 429, the fallback toopenrouter/anthropic/claude-sonnet-4.6should have succeeded (and does succeed when called directly), but auto-map prevented it from ever being tried.Fix
The check is regex-agnostic: it covers every
auto_map_regexconfiguration (claude, gpt, gemini, mixed, custom) — no provider-specific code path needed.Auto-map continues to rewrite the model when no explicit virtual exists, preserving the original "catch-all rewrite for unconfigured models" intent.
Other routing steps audited
decision.model_name = ...directdecision.model_name = ...directrequest.model = ...then falls throughdecision.model_name = ...directdecision.model_name = ...directdecision.model_name = ...directAuto-map is the only step that mutates
request.modeland falls through; the other steps either return early or setdecision.model_namewithout touchingrequest.model. No other leak path exists.Tests
Adds two regression tests:
test_auto_map_skips_explicit_virtual_model— confirms the new behaviour.test_auto_map_still_rewrites_unmapped_claude— counter-test confirming the original catch-all path is intact.Existing tests still pass:
test_auto_map_claude_models,test_auto_map_custom_regex,test_no_auto_map_non_matching,test_auto_map_routes_to_default.Test plan
cargo nextest run -E 'test(auto_map)'— 9/9 passingcargo fmt --all -- --checkcleancargo clippy --all-features -- -D warningsclean🤖 Generated with Claude Code