fix(models): stop advertising graded thinking levels the wire format drops (#3552)

* fix(models): stop advertising graded thinking levels the wire format drops

A generic-provider row (relays, self-hosted runtimes — the default for
uncatalogued models) resolves an empty ThinkingLevelMap, whose missing-key
semantics report every graded rung as supported. Capabilities() then hands
minimal/low/medium/high to the frontend, which renders a selector whose
choice applyThinking silently discards: the chat-template-kwargs branch
sends only the enable_thinking boolean and no reasoning_effort is computed.
Same defect class as the two NVIDIA NIM entries fixed in #3489, but there
is no catalog entry to repair here.

Collapse Capabilities() to the levels the wire can actually express for a
boolean-only switch (off/auto) unless the settings opt into
supports_reasoning_effort. The vendor map itself is untouched, so vendors
that do carry effort keep their ladders.

* fix(models): carry reasoning_effort through the chat-template switch when opted in

The Capabilities() guard kept a vendor's ladder when it opts into
supports_reasoning_effort, but applyThinking's chat-template-kwargs branch
wrote only the enable_thinking boolean and dropped the already-computed
effort — so the opt-in combination still sent byte-identical bodies for
every graded rung (#3551's defect, alive in exactly the shape the review
flagged). Mirror the enable_thinking dialect: write reasoning_effort on
top of the switch when one was computed. A golden case pins the opt-in
combination; the no-opt-in guard case keeps asserting the level is
dropped.

* style(models): wrap long Resolve call in thinking-levels test

---------

Co-authored-by: wizardchen <wizardchen@tencent.com>
Co-authored-by: lyingbug <xiayula2014@gmail.com>
main
DerrickLinus 2026-09-25 22:23:08 +08:00 committed by GitHub
parent c7601b2d2a
commit a9d71038b6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 114 additions and 0 deletions

View File

@ -225,6 +225,36 @@ func TestGolden(t *testing.T) {
opts: &api.Options{Thinking: ptrBool(true)},
want: map[string]any{"chat_template_kwargs": map[string]any{"enable_thinking": true}},
},
{
// Without the effort opt-in the graded level the caller picked is
// not expressible in this dialect: the branch carries only the
// boolean and no reasoning_effort may leak onto the wire
// (capabilities must not advertise rungs for the same reason).
name: "vllm: graded effort request still carries only the boolean",
mutate: func(c *Config) {
c.Settings.ThinkingFormat = api.ThinkingFormatChatTemplateKwargs
},
opts: &api.Options{Thinking: ptrBool(true), ReasoningEffort: api.ReasoningHigh},
want: map[string]any{
"chat_template_kwargs": map[string]any{"enable_thinking": true},
"reasoning_effort": nil,
},
},
{
// With the effort opt-in the graded level rides on top of the
// switch, exactly like the enable_thinking dialect (#3552
// review): capabilities keep the ladder for the same reason.
name: "nim-style opt-in: chat_template_kwargs plus reasoning_effort",
mutate: func(c *Config) {
c.Settings.ThinkingFormat = api.ThinkingFormatChatTemplateKwargs
c.Settings.SupportsReasoningEffort = true
},
opts: &api.Options{Thinking: ptrBool(true), ReasoningEffort: api.ReasoningHigh},
want: map[string]any{
"chat_template_kwargs": map[string]any{"enable_thinking": true},
"reasoning_effort": "high",
},
},
{
name: "openrouter: reasoning effort object and enabled=false",
mutate: func(c *Config) {

View File

@ -392,6 +392,9 @@ func (c *Client) applyThinking(body map[string]any, opts *api.Options, stream bo
}
case api.ThinkingFormatChatTemplateKwargs:
body["chat_template_kwargs"] = map[string]any{"enable_thinking": enabled}
if effort != "" {
body[effortField] = effort
}
case api.ThinkingFormatOpenRouter:
if enabled {
reasoning := map[string]any{}

View File

@ -65,6 +65,31 @@ func registerTestVendors(t *testing.T) {
},
ModelTypes: []types.ModelType{types.ModelTypeKnowledgeQA},
})
// acme-nim mirrors the NVIDIA NIM entries before #3489: a chat-template
// switch that carries only the enable_thinking boolean, while the models
// carry ladders the branch cannot encode.
modelruntime.Register(&providers.Definition{
ID: "acme-nim", Name: "Acme NIM", API: api.APIOpenAICompletions,
ModelTypes: []types.ModelType{types.ModelTypeKnowledgeQA},
Compat: providers.VendorCompat{OpenAICompletions: api.OpenAICompletionsCompat{
MaxTokensField: api.Ptr("max_tokens"),
ThinkingFormat: api.Ptr(api.ThinkingFormatChatTemplateKwargs),
}},
}, []models.ModelSpec{
{
ID: "acme-nim-glm", Reasoning: true,
ThinkingLevels: api.ThinkingLevelMap{
api.ReasoningOff: nil,
api.ReasoningMinimal: api.StringPtr("low"),
api.ReasoningHigh: api.StringPtr("high"),
},
},
{
ID: "acme-nim-effort", Reasoning: true,
Compat: json.RawMessage(`{"supports_reasoning_effort": true}`),
ThinkingLevels: api.ThinkingLevelMap{api.ReasoningHigh: api.StringPtr("high")},
},
}...)
}
func TestFindModel_ExactAliasAndGlob(t *testing.T) {
@ -273,6 +298,45 @@ func TestResolve_AlwaysOnModelCannotBeSwitchedOff(t *testing.T) {
assert.NotContains(t, r.Capabilities().ThinkingLevels, api.ReasoningOff)
}
// A chat-template switch carries only the enable_thinking boolean, so every
// graded rung is silently dropped on the wire — and a relay backend that does
// not know enable_thinking drops the boolean too. Capabilities must not
// advertise levels the wire format cannot express, or the UI renders a
// selector whose choice does nothing. #3489 fixed the two catalogued NIM
// entries with this defect; this pins the generic fallback and the
// boolean-only vendor shape in general.
func TestResolve_BooleanThinkingSwitchHidesGradedLevels(t *testing.T) {
registerTestVendors(t)
// Uncatalogued model on the generic vendor — a relay row. The empty level
// map's missing-key pass-through used to advertise six rungs here.
r, err := modelruntime.Resolve(modelruntime.Ref{
Provider: "generic", Model: "glm-5.3", BaseURL: "https://relay.example.com/v1",
})
require.NoError(t, err)
assert.False(t, r.Cataloged)
assert.Equal(t, []api.ReasoningEffort{api.ReasoningOff, api.ReasoningAuto},
r.Capabilities().ThinkingLevels, "only the boolean the switch can carry")
// Catalogued model whose ladder the chat-template branch cannot encode:
// the graded rungs stay hidden from the selector.
r, err = modelruntime.Resolve(modelruntime.Ref{Provider: "acme-nim", Model: "acme-nim-glm"})
require.NoError(t, err)
caps := r.Capabilities()
assert.Equal(t, []api.ReasoningEffort{api.ReasoningAuto}, caps.ThinkingLevels,
"always-on entry: off was already hidden, graded rungs must not appear")
assert.NotContains(t, caps.ThinkingLevels, api.ReasoningMinimal)
assert.NotContains(t, caps.ThinkingLevels, api.ReasoningHigh)
// A vendor that opts into a top-level effort field alongside the switch
// keeps its ladder (the future combo #3489's entries moved to).
r, err = modelruntime.Resolve(modelruntime.Ref{Provider: "acme-nim", Model: "acme-nim-effort"})
require.NoError(t, err)
caps = r.Capabilities()
assert.Contains(t, caps.ThinkingLevels, api.ReasoningMinimal, "pass-through rung kept")
assert.Contains(t, caps.ThinkingLevels, api.ReasoningHigh, "explicit ladder rung kept")
}
func TestResolve_RejectsUnknownCompatKeys(t *testing.T) {
registerTestVendors(t)
_, err := modelruntime.Resolve(

View File

@ -324,6 +324,23 @@ func (r *Resolved) Capabilities() Capabilities {
caps.MaxTokensField = "num_predict"
}
caps.ThinkingLevels = r.ThinkingLevels.SupportedLevels()
// A chat-template switch carries only the enable_thinking boolean: every
// graded rung is silently dropped on the wire (and a relay backend that
// does not know enable_thinking drops the boolean too), so the selector
// must not offer rungs the request cannot express. Vendors that opt into
// a top-level effort field keep their ladders (#3489 moved its NIM
// entries to that shape).
if r.API == api.APIOpenAICompletions &&
r.OpenAICompletions.ThinkingFormat == api.ThinkingFormatChatTemplateKwargs &&
!r.OpenAICompletions.SupportsReasoningEffort {
keep := make([]api.ReasoningEffort, 0, 2)
for _, level := range caps.ThinkingLevels {
if !level.Graded() {
keep = append(keep, level)
}
}
caps.ThinkingLevels = keep
}
return caps
}