Commit a946eb541 for llama.cpp

commit a946eb541a6d3051496bdcc451a0fa85772b8dfd
Author: Pascal <admin@serveurperso.com>
Date:   Mon Nov 16 06:55:56 2026 +0100

    server: answer reasoning_effort levels the template rejects with 400 (#32332)

    The reasoning effort probe already renders every candidate level, but
    it dropped the ones the template raises on, so a request with such a
    level (e.g. max on Qwen3.8) reached the template and came back as a
    500 on /v1/chat/completions, /v1/responses and /v1/messages.

    Keep the raising levels in reasoning_effort_rejected and reject them
    in oaicompat_chat_params_parse with an invalid_argument, which the
    server answers with a 400 listing the supported levels. Aliases and
    levels a template ignores are not rejected, so high on Qwen3.8
    still renders as xhigh.

    Also trim the output_config.effort test to a single level.

diff --git a/common/chat.cpp b/common/chat.cpp
index b3c930ec4..534a05d5c 100644
--- a/common/chat.cpp
+++ b/common/chat.cpp
@@ -1979,5 +1979,6 @@ common_json common_chat_templates_get_caps(const common_chat_templates * chat_te
                                                                       : chat_templates->template_default->caps;
     common_json result = caps.to_map();
     result["reasoning_effort_levels"] = caps.reasoning_effort_levels;
+    result["reasoning_effort_rejected"] = caps.reasoning_effort_rejected;
     return result;
 }
diff --git a/common/jinja/caps.cpp b/common/jinja/caps.cpp
index de7780bdf..cf14c71c5 100644
--- a/common/jinja/caps.cpp
+++ b/common/jinja/caps.cpp
@@ -627,9 +627,14 @@ caps caps_get(jinja::program & prog) {
         // levels sharing a render are aliases and the highest one is kept
         const auto unknown = render("REASONING_EFFORT_UNKNOWN");
         std::vector<std::string> renders;
+        std::vector<std::string> rejected;
         for (const auto & level : candidates) {
             const auto rendered = render(level);
-            if (!rendered || rendered == unknown) {
+            if (!rendered) {
+                rejected.push_back(level);
+                continue;
+            }
+            if (rendered == unknown) {
                 continue;
             }
             const auto it = std::find(renders.begin(), renders.end(), *rendered);
@@ -640,6 +645,11 @@ caps caps_get(jinja::program & prog) {
                 result.reasoning_effort_levels.push_back(level);
             }
         }
+
+        // a template that renders no level fails the probe for another reason, so its failures say nothing about the levels
+        if (!result.reasoning_effort_levels.empty()) {
+            result.reasoning_effort_rejected = rejected;
+        }
     }

     JJ_DEBUG("%s\n", result.to_string().c_str());
diff --git a/common/jinja/caps.h b/common/jinja/caps.h
index 108501e06..f231309e6 100644
--- a/common/jinja/caps.h
+++ b/common/jinja/caps.h
@@ -23,6 +23,9 @@ struct caps {
     // reasoning effort levels the template renders distinctly, from lowest to highest
     std::vector<std::string> reasoning_effort_levels;

+    // reasoning effort levels the template raises on
+    std::vector<std::string> reasoning_effort_rejected;
+
     // one of the 2 content capabilities must be true
     bool supports_string_content = true;
     bool supports_typed_content = false;
diff --git a/tests/test-chat.cpp b/tests/test-chat.cpp
index b34de6562..655ce0238 100644
--- a/tests/test-chat.cpp
+++ b/tests/test-chat.cpp
@@ -8614,6 +8614,17 @@ static void test_reasoning_effort_caps() {
     assert_effort_levels("models/templates/openai-gpt-oss-120b.jinja", all_levels);
     assert_effort_levels("models/templates/Cohere2MoE.jinja", {});
     assert_effort_levels("models/templates/Qwen-Qwen3-0.6B.jinja", {});
+
+    auto assert_effort_rejected = [](const std::string & path, const std::vector<std::string> & expected) {
+        auto tmpls = read_templates(path);
+        assert_equals(json(expected).dump(), common_chat_templates_get_caps(tmpls.get()).at("reasoning_effort_rejected").dump());
+    };
+
+    assert_effort_rejected("models/templates/IFM-K2-Horizon.jinja", { "minimal", "xhigh", "max" });
+    assert_effort_rejected("models/templates/tencent-Hy3.jinja", { "minimal", "medium", "xhigh", "max" });
+    assert_effort_rejected("models/templates/deepseek-ai-DeepSeek-V4.jinja", {});
+    assert_effort_rejected("models/templates/openai-gpt-oss-120b.jinja", {});
+    assert_effort_rejected("models/templates/Cohere2MoE.jinja", {});
 }

 static void test_msg_diffs_compute() {
diff --git a/tools/server/README.md b/tools/server/README.md
index 13d41d884..80f1cdf50 100644
--- a/tools/server/README.md
+++ b/tools/server/README.md
@@ -1031,7 +1031,7 @@ By default, it is read-only. To make POST request to change global properties, y
 - `voices` - the predefined voices of an audio generation model, to pass as `voice` to `/tts`. Only present when the model has some (e.g. LFM3-Audio, Pocket TTS without voice cloning)
 - `model_path` - the path to model file (same with `-m` argument)
 - `chat_template` - the model's original Jinja2 prompt template
-- `chat_template_caps` - capabilities of the chat template (see `common/jinja/caps.h` for more info), `reasoning_effort_levels` lists the `reasoning_effort` values the template renders distinctly, from lowest to highest
+- `chat_template_caps` - capabilities of the chat template (see `common/jinja/caps.h` for more info), `reasoning_effort_levels` lists the `reasoning_effort` values the template renders distinctly, from lowest to highest, and `reasoning_effort_rejected` the ones it raises on, which are answered with a 400
 - `modalities` - the list of supported modalities
 - `is_sleeping` - sleeping status, see [Sleeping on idle](#sleeping-on-idle)

diff --git a/tools/server/server-common.cpp b/tools/server/server-common.cpp
index ea34f75ac..82c48d2da 100644
--- a/tools/server/server-common.cpp
+++ b/tools/server/server-common.cpp
@@ -1646,6 +1646,11 @@ json oaicompat_chat_params_parse(
             inputs.enable_thinking = false;
             inputs.chat_template_kwargs.erase("reasoning_effort");
         } else if (!reasoning_effort.empty()) {
+            const auto rejected = caps.at("reasoning_effort_rejected").get<std::vector<std::string>>();
+            if (std::find(rejected.begin(), rejected.end(), reasoning_effort) != rejected.end()) {
+                const auto levels = caps.at("reasoning_effort_levels").get<std::vector<std::string>>();
+                throw std::invalid_argument("reasoning_effort \"" + reasoning_effort + "\" is not supported by the chat template, supported levels: " + string_join(levels, ", "));
+            }
             inputs.chat_template_kwargs["reasoning_effort"] = json(reasoning_effort).dump();
         }
     }
diff --git a/tools/server/tests/unit/test_compat_anthropic.py b/tools/server/tests/unit/test_compat_anthropic.py
index 2346de02e..0b65ad1ab 100644
--- a/tools/server/tests/unit/test_compat_anthropic.py
+++ b/tools/server/tests/unit/test_compat_anthropic.py
@@ -851,13 +851,13 @@ def test_anthropic_thinking():
     assert res.body["type"] == "message"


-@pytest.mark.parametrize("effort", ["low", "high"])
-def test_anthropic_output_config_effort(effort):
+def test_anthropic_output_config_effort():
     """output_config.effort must reach the chat template, same as chat_template_kwargs.reasoning_effort"""
     global server
     server.jinja = True
     server.chat_template_file = '../../../models/templates/openai-gpt-oss-120b.jinja'
     server.start()
+    effort = "low"

     def count_tokens(extra):
         res = server.make_request("POST", "/v1/messages/count_tokens", data={
diff --git a/tools/server/tests/unit/test_template.py b/tools/server/tests/unit/test_template.py
index 43a356020..8a7ea90ce 100644
--- a/tools/server/tests/unit/test_template.py
+++ b/tools/server/tests/unit/test_template.py
@@ -23,6 +23,26 @@ def create_server():
     server.n_slots = 1


+def test_reasoning_effort_rejected_by_template():
+    global server
+    server.jinja = True
+    # the template raises on max, and high is one of its levels
+    server.chat_template_file = '../../../models/templates/IFM-K2-Horizon.jinja'
+    server.start()
+
+    messages = [{"role": "user", "content": "Hello"}]
+    for path, data in [
+        ("/chat/completions/input_tokens", {"messages": messages, "reasoning_effort": "max"}),
+        ("/v1/responses/input_tokens",     {"input": "Hello", "reasoning": {"effort": "max"}}),
+        ("/v1/messages/count_tokens",      {"model": "test", "messages": messages, "output_config": {"effort": "max"}}),
+    ]:
+        res = server.make_request("POST", path, data=data)
+        assert res.status_code == 400, f"{path}: {res.body}"
+
+    res = server.make_request("POST", "/chat/completions/input_tokens", data={"messages": messages, "reasoning_effort": "high"})
+    assert res.status_code == 200, res.body
+
+
 @pytest.mark.parametrize("tools", [None, [], [TEST_TOOL]])
 @pytest.mark.parametrize("template_name,reasoning,expected_end", [
     ("deepseek-ai-DeepSeek-R1-Distill-Qwen-32B",  "on", "<think>\n"),