fix(router): use alias modelMeta for vision and context checks

GET /v1/models resolves an alias's vision and contextTokens through the
alias's own modelMeta entry. The smart router read only the target model,
so an alias marked vision: false still got image requests. The router now
resolves a candidate through the alias when the tier target is one.
This commit is contained in:
2026-12-02 02:24:53 +00:00
parent 4086f7f712
commit 7c1219a2bf
4 changed files with 36 additions and 5 deletions
+1 -1
View File
@@ -1606,7 +1606,7 @@ The gateway picks the highest tier below the first attempted tier, and goes lowe
- **Estimate**: the prompt estimate counts one token per four bytes of these inputs: message text, the system prompt, the `tools` schema, OpenAI `tool_calls` arguments, and Anthropic `tool_use` input. Each image adds 1000 tokens. Very large structured payloads count as a lower bound.
- **Learned ratio**: after 50 served requests with provider-reported usage, the gateway learns bytes per token for each upstream model. The ratio stays between 1.5 and 6.0 and lives in memory on each instance.
- **No fit**: when the check would skip every target, the gateway uses the model with the largest known window instead of returning `404`.
- **Vision**: a request with an image skips models whose resolved vision flag is `false`: from `modelMeta`, else from discovery.
- **Vision**: a request with an image skips models whose resolved vision flag is `false`: from `modelMeta`, else from discovery. A target that is a model alias uses the alias's own `modelMeta` entry first, as `GET /v1/models` does. This also applies to its `contextTokens`.
- **Advertised window**: `GET /v1/models` lists a router when the group may use its name and at least one target. With `contextEscalation` on, the entry advertises the largest known window of all targets in `context_window`, `context_length`, `max_model_len`, and `max_context_length`. With `contextEscalation: false`, it advertises the smallest known window. Targets with an unknown window do not count. When no target has a known window, the fields are omitted.
- **Override**: a `modelMeta` entry keyed by the router name replaces the value. It needs `contextTokens` above zero. The gateway logs a one-time warning when the override exceeds the largest known target window.
+9
View File
@@ -2489,6 +2489,15 @@ func (m *modelRegistry) resolveMetaForAliasName(alias string) resolvedModelMeta
return m.resolveMetaForAlias(alias, providerName, bareTarget)
}
// candidateMeta resolves a router candidate's metadata as /v1/models shows
// the target: through the alias when target is one.
func (m *modelRegistry) candidateMeta(target, providerName, model string) resolvedModelMeta {
if _, isAlias := m.aliases[target]; isAlias {
return m.resolveMetaForAlias(target, providerName, model)
}
return m.resolveMetaFor(providerName, model)
}
// resolveMetaForAlias resolves alias's metadata (feature v0.23) via
// resolveAliasModelMeta: target-inherited unless the alias itself has
// its own modelMeta entry — see that function's own doc comment.
+22
View File
@@ -245,6 +245,28 @@ func TestRouter_TargetSkipping(t *testing.T) {
},
content: "hi", wantStatus: 200, wantModel: "alpha/a1", wantA1Hit: 1,
},
{
name: "alias vision false is skipped for image requests",
mut: func(c *Config) {
c.ModelAliases = map[string]string{"a1-text": "alpha/a1"}
c.ModelMeta = map[string]*ModelMetaConfig{"a1-text": {Vision: &no}}
twoTiers(c, "a1-text", "b1")
},
content: []any{
map[string]any{"type": "text", "text": "hi"},
map[string]any{"type": "image_url", "image_url": map[string]any{"url": "data:image/png;base64,AAAA"}},
},
wantStatus: 200, wantModel: "beta/b1",
},
{
name: "alias small context window skipped when escalation on",
mut: func(c *Config) {
c.ModelAliases = map[string]string{"a1-small": "alpha/a1"}
c.ModelMeta = map[string]*ModelMetaConfig{"a1-small": {ContextTokens: 50}}
twoTiers(c, "a1-small", "b1")
},
content: longPrompt, wantStatus: 200, wantModel: "beta/b1",
},
{
name: "small context window skipped when escalation on",
mut: func(c *Config) {
+4 -4
View File
@@ -327,7 +327,7 @@ func (g *Gateway) routerTargetCandidates(rt *smartRouter, tier, target string, f
ordered := g.orderedFailoverCandidates(primary, extra)
usable := make([]resolveCandidate, 0, len(ordered))
for _, cand := range ordered {
if g.routerCandidateSkipped(cand, tier, rt, facts, skips) {
if g.routerCandidateSkipped(cand, target, tier, rt, facts, skips) {
continue
}
usable = append(usable, cand)
@@ -336,8 +336,9 @@ func (g *Gateway) routerTargetCandidates(rt *smartRouter, tier, target string, f
return capFailoverCandidates(usable, g.failover.maxAttempts)
}
func (g *Gateway) routerCandidateSkipped(cand resolveCandidate, tier string, rt *smartRouter, facts routerRequestFacts, skips *routerSkips) bool {
if facts.hasImage && !visionCapable(g.registry.resolveMetaFor(cand.providerName, cand.upstreamModel).Vision) {
func (g *Gateway) routerCandidateSkipped(cand resolveCandidate, target, tier string, rt *smartRouter, facts routerRequestFacts, skips *routerSkips) bool {
meta := g.registry.candidateMeta(target, cand.providerName, cand.upstreamModel)
if facts.hasImage && !visionCapable(meta.Vision) {
skips.vision++
return true
}
@@ -347,7 +348,6 @@ func (g *Gateway) routerCandidateSkipped(cand resolveCandidate, tier string, rt
}
// Checked last so the fallback candidate already passed vision and cost.
if rt.contextEscalation {
meta := g.registry.resolveMetaFor(cand.providerName, cand.upstreamModel)
if meta.ContextKnown && !contextFits(facts, g.tokenCalib.promptTokens(cand.providerName, cand.upstreamModel, facts.promptBytes), int64(meta.ContextTokens)) {
skips.context++
if skips.largest == nil || meta.ContextTokens > skips.largestCtx {