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 renders; + std::vector 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 reasoning_effort_levels; + // reasoning effort levels the template raises on + std::vector 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 & 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>(); + if (std::find(rejected.begin(), rejected.end(), reasoning_effort) != rejected.end()) { + const auto levels = caps.at("reasoning_effort_levels").get>(); + 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", "\n"),