diff --git a/src/common/AutoModel/modeling_gemma4_12b.cpp b/src/common/AutoModel/modeling_gemma4_12b.cpp index 92928121e..24525b47c 100644 --- a/src/common/AutoModel/modeling_gemma4_12b.cpp +++ b/src/common/AutoModel/modeling_gemma4_12b.cpp @@ -86,25 +86,12 @@ std::string Gemma4_12B::apply_chat_template(nlohmann::ordered_json& messages, nl opt.polyfill_tool_responses = false; opt.polyfill_object_arguments = false; - // The template raises if tool_calls[].function.arguments is a string, but OpenAI-style - // clients send it serialized. Deserialize it here (what polyfill_object_arguments would do). - nlohmann::ordered_json normalized = messages; - for (auto& message : normalized) { - if (!message.is_object() || !message.contains("tool_calls")) continue; - for (auto& tool_call : message["tool_calls"]) { - if (!tool_call.is_object() || !tool_call.contains("function")) continue; - auto& function = tool_call["function"]; - if (!function.is_object() || !function.contains("arguments")) continue; - auto& arguments = function["arguments"]; - if (!arguments.is_string()) continue; - auto raw = arguments.get(); - if (raw.empty()) { - arguments = nlohmann::ordered_json::object(); - continue; - } - auto parsed = nlohmann::ordered_json::parse(raw, nullptr, /* allow_exceptions= */ false); - if (parsed.is_object()) arguments = parsed; - } + // OpenAI-style clients send tool call arguments serialized; the template renders only an object + nlohmann::ordered_json normalized; + try { + normalized = gemma4_template_messages(messages); + } catch (const std::invalid_argument& e) { + throw ModelRequestError(400, false, e.what(), "invalid_request_error"); } minja::chat_template_inputs inputs; diff --git a/src/common/AutoModel/modeling_gemma4e.cpp b/src/common/AutoModel/modeling_gemma4e.cpp index 2ae272921..3af8d03f1 100644 --- a/src/common/AutoModel/modeling_gemma4e.cpp +++ b/src/common/AutoModel/modeling_gemma4e.cpp @@ -317,25 +317,12 @@ std::string Gemma4e::apply_chat_template(nlohmann::ordered_json& messages, nlohm opt.polyfill_tool_responses = false; opt.polyfill_object_arguments = false; - // The template raises if tool_calls[].function.arguments is a string, but OpenAI-style clients send it - // serialized. Deserialize it here (what polyfill_object_arguments would do). - nlohmann::ordered_json normalized = messages; - for (auto& message : normalized) { - if (!message.is_object() || !message.contains("tool_calls")) continue; - for (auto& tool_call : message["tool_calls"]) { - if (!tool_call.is_object() || !tool_call.contains("function")) continue; - auto& function = tool_call["function"]; - if (!function.is_object() || !function.contains("arguments")) continue; - auto& arguments = function["arguments"]; - if (!arguments.is_string()) continue; - auto raw = arguments.get(); - if (raw.empty()) { - arguments = nlohmann::ordered_json::object(); - continue; - } - auto parsed = nlohmann::ordered_json::parse(raw, nullptr, /* allow_exceptions= */ false); - if (parsed.is_object()) arguments = parsed; - } + // OpenAI-style clients send tool call arguments serialized; the template renders only an object + nlohmann::ordered_json normalized; + try { + normalized = gemma4_template_messages(messages); + } catch (const std::invalid_argument& e) { + throw ModelRequestError(400, false, e.what(), "invalid_request_error"); } minja::chat_template_inputs inputs; diff --git a/src/common/chat/chat_gemma4_tools.cpp b/src/common/chat/chat_gemma4_tools.cpp index 52fa6ee5a..52734e672 100644 --- a/src/common/chat/chat_gemma4_tools.cpp +++ b/src/common/chat/chat_gemma4_tools.cpp @@ -1,6 +1,8 @@ /// \file chat_gemma4_tools.cpp /// \brief Prepare a request's tools for the Gemma4 chat template #include "chat/chat_gemma4_tools.hpp" +#include +#include namespace { /// \brief Rewrite the nullable types in a JSON schema and in the schemas it holds @@ -40,3 +42,27 @@ nlohmann::ordered_json gemma4_template_tools(nlohmann::ordered_json tools) { } return tools; } + +nlohmann::ordered_json gemma4_template_messages(nlohmann::ordered_json messages) { + for (auto& message : messages) { + if (!message.is_object() || !message.contains("tool_calls") || !message["tool_calls"].is_array()) continue; + for (auto& tool_call : message["tool_calls"]) { + if (!tool_call.is_object() || !tool_call.contains("function")) continue; + auto& function = tool_call["function"]; + if (!function.is_object() || !function.contains("arguments")) continue; + auto& arguments = function["arguments"]; + if (arguments.is_string()) { + const auto raw = arguments.get(); + arguments = raw.empty() ? nlohmann::ordered_json::object() + : nlohmann::ordered_json::parse(raw, nullptr, /* allow_exceptions= */ false); + } + if (!arguments.is_object() && !arguments.is_null()) { + const auto name = function.value("name", nlohmann::ordered_json()).is_string() + ? function["name"].get() : std::string("?"); + throw std::invalid_argument("tool_calls[].function.arguments of the call to \"" + name + + "\" must be a JSON object"); + } + } + } + return messages; +} diff --git a/src/include/chat/chat_gemma4_tools.hpp b/src/include/chat/chat_gemma4_tools.hpp index 0eff4ecdd..86a64a522 100644 --- a/src/include/chat/chat_gemma4_tools.hpp +++ b/src/include/chat/chat_gemma4_tools.hpp @@ -10,3 +10,9 @@ /// a single nullable parameter would fail the whole request. A type that still lists /// more than one type after dropping "null" is left as it is. nlohmann::ordered_json gemma4_template_tools(nlohmann::ordered_json tools); + +/// \brief The messages with every tool call's arguments as the JSON object the template renders +/// \note OpenAI-style clients send arguments serialized; an empty string is no arguments ({}). +/// The template refuses any arguments that are not an object or null, so those throw +/// std::invalid_argument, naming the call, instead of failing in the template. +nlohmann::ordered_json gemma4_template_messages(nlohmann::ordered_json messages); diff --git a/src/model_info.json b/src/model_info.json index ad13c60c0..8c5b81144 100644 --- a/src/model_info.json +++ b/src/model_info.json @@ -296,13 +296,13 @@ }, { "type": "file", - "oid": "c19999a347da729cf62806a8ddb7eb8e315223b5", - "size": 17336, + "oid": "9d494178d6f050c786d4b6fa70a0e1a24fac4ae6", + "size": 18454, "path": "chat_template.jinja" }, { "type": "file", - "oid": "51a20dbc7844107d06280c51882dbad273f1a9c4", + "oid": "a2222087e803782a01c982c2419f96b5754a2cf9", "size": 7528, "path": "config.json" }, @@ -320,14 +320,14 @@ }, { "type": "file", - "oid": "1ff9f3e3439a939b971f9919e821bf87e835a503", - "size": 32169626, + "oid": "423b777a1620a1e582e0843d01b86d1d1c698965", + "size": 32155733, "lfs": { - "oid": "cc8d3a0ce36466ccc1278bf987df5f71db1719b9ca6b4118264f45cb627bfe0f", - "size": 32169626, + "oid": "ab26b2add729e6f645bd869f228f14190a06615538f3be6f7786f33d13d78ad2", + "size": 32155733, "pointerSize": 133 }, - "xetHash": "c62336ad134cad6f154d84eb0e5a5fa9ca17cd665ef3ba5ac4fd02b1486760b4", + "xetHash": "d3e2cbf0094f678aa49dbe05422fc1e70302e85a138fe38163a6a8683c169265", "path": "tokenizer.json" }, { @@ -370,13 +370,13 @@ }, { "type": "file", - "oid": "c19999a347da729cf62806a8ddb7eb8e315223b5", - "size": 17336, + "oid": "9d494178d6f050c786d4b6fa70a0e1a24fac4ae6", + "size": 18454, "path": "chat_template.jinja" }, { "type": "file", - "oid": "07a6fb2f7c407853d83725b513ab1fc2d165e6a5", + "oid": "e504aebcb59eae9e583cc95b419d6d7ccdd5a90a", "size": 7229, "path": "config.json" }, @@ -394,14 +394,14 @@ }, { "type": "file", - "oid": "1ff9f3e3439a939b971f9919e821bf87e835a503", - "size": 32169626, + "oid": "0e3703ae5fe4459202ad171ccb127b4fcfb7f275", + "size": 32156428, "lfs": { - "oid": "cc8d3a0ce36466ccc1278bf987df5f71db1719b9ca6b4118264f45cb627bfe0f", - "size": 32169626, + "oid": "91d9cf86906f78e197021fda88214d448e5c120665d0243a8e776e046986c6bc", + "size": 32156428, "pointerSize": 133 }, - "xetHash": "c62336ad134cad6f154d84eb0e5a5fa9ca17cd665ef3ba5ac4fd02b1486760b4", + "xetHash": "521d9f0390742dc5890fc5f92931dae9bdda70191dd92fd0f551fae33d8ded1c", "path": "tokenizer.json" }, { @@ -444,13 +444,13 @@ }, { "type": "file", - "oid": "c19999a347da729cf62806a8ddb7eb8e315223b5", - "size": 17336, + "oid": "9d494178d6f050c786d4b6fa70a0e1a24fac4ae6", + "size": 18454, "path": "chat_template.jinja" }, { "type": "file", - "oid": "51a20dbc7844107d06280c51882dbad273f1a9c4", + "oid": "a2222087e803782a01c982c2419f96b5754a2cf9", "size": 7528, "path": "config.json" }, @@ -468,14 +468,14 @@ }, { "type": "file", - "oid": "1ff9f3e3439a939b971f9919e821bf87e835a503", - "size": 32169626, + "oid": "423b777a1620a1e582e0843d01b86d1d1c698965", + "size": 32155733, "lfs": { - "oid": "cc8d3a0ce36466ccc1278bf987df5f71db1719b9ca6b4118264f45cb627bfe0f", - "size": 32169626, + "oid": "ab26b2add729e6f645bd869f228f14190a06615538f3be6f7786f33d13d78ad2", + "size": 32155733, "pointerSize": 133 }, - "xetHash": "c62336ad134cad6f154d84eb0e5a5fa9ca17cd665ef3ba5ac4fd02b1486760b4", + "xetHash": "d3e2cbf0094f678aa49dbe05422fc1e70302e85a138fe38163a6a8683c169265", "path": "tokenizer.json" }, { @@ -518,13 +518,13 @@ }, { "type": "file", - "oid": "c19999a347da729cf62806a8ddb7eb8e315223b5", - "size": 17336, + "oid": "9d494178d6f050c786d4b6fa70a0e1a24fac4ae6", + "size": 18454, "path": "chat_template.jinja" }, { "type": "file", - "oid": "07a6fb2f7c407853d83725b513ab1fc2d165e6a5", + "oid": "e504aebcb59eae9e583cc95b419d6d7ccdd5a90a", "size": 7229, "path": "config.json" }, @@ -542,14 +542,14 @@ }, { "type": "file", - "oid": "1ff9f3e3439a939b971f9919e821bf87e835a503", - "size": 32169626, + "oid": "0e3703ae5fe4459202ad171ccb127b4fcfb7f275", + "size": 32156428, "lfs": { - "oid": "cc8d3a0ce36466ccc1278bf987df5f71db1719b9ca6b4118264f45cb627bfe0f", - "size": 32169626, + "oid": "91d9cf86906f78e197021fda88214d448e5c120665d0243a8e776e046986c6bc", + "size": 32156428, "pointerSize": 133 }, - "xetHash": "c62336ad134cad6f154d84eb0e5a5fa9ca17cd665ef3ba5ac4fd02b1486760b4", + "xetHash": "521d9f0390742dc5890fc5f92931dae9bdda70191dd92fd0f551fae33d8ded1c", "path": "tokenizer.json" }, { diff --git a/src/model_list.json b/src/model_list.json index 235b1dca7..3c1a86bcd 100644 --- a/src/model_list.json +++ b/src/model_list.json @@ -360,11 +360,11 @@ "tools": true, "supported_platforms": ["aie2p"], "name": "Gemma4-E2B-IT-NPU2", - "url": "https://huggingface.co/FastFlowLM/Gemma4-E2B-IT-NPU2", - "file_url": "https://huggingface.co/api/models/FastFlowLM/Gemma4-E2B-IT-NPU2/tree/main", - "ms_url": "https://modelscope.cn/models/amd/Gemma4-E2B-IT-NPU2", + "url": "https://huggingface.co/FastFlowLM/Gemma4-E2B-IT-NPU2/resolve/flm_1.0.15", + "file_url": "https://huggingface.co/api/models/FastFlowLM/Gemma4-E2B-IT-NPU2/tree/flm_1.0.15", + "ms_url": "https://modelscope.cn/models/amd/Gemma4-E2B-IT-NPU2/resolve/flm_1.0.15", "size": 2000000000, - "flm_min_version": "0.9.43", + "flm_min_version": "1.0.15", "files": [ "config.json", "model.q4nx", @@ -399,11 +399,11 @@ "tools": true, "supported_platforms": ["aie2p"], "name": "Gemma4-E4B-IT-NPU2", - "url": "https://huggingface.co/FastFlowLM/Gemma4-E4B-IT-NPU2", - "file_url": "https://huggingface.co/api/models/FastFlowLM/Gemma4-E4B-IT-NPU2/tree/main", - "ms_url": "https://modelscope.cn/models/amd/Gemma4-E4B-IT-NPU2", + "url": "https://huggingface.co/FastFlowLM/Gemma4-E4B-IT-NPU2/resolve/flm_1.0.15", + "file_url": "https://huggingface.co/api/models/FastFlowLM/Gemma4-E4B-IT-NPU2/tree/flm_1.0.15", + "ms_url": "https://modelscope.cn/models/amd/Gemma4-E4B-IT-NPU2/resolve/flm_1.0.15", "size": 4000000000, - "flm_min_version": "0.9.43", + "flm_min_version": "1.0.15", "files": [ "config.json", "model.q4nx", @@ -591,11 +591,11 @@ "e2b": { "supported_platforms": ["aie2p"], "name": "Gemma4-E2B-IT-NPU2", - "url": "https://huggingface.co/FastFlowLM/Gemma4-E2B-IT-NPU2", - "file_url": "https://huggingface.co/api/models/FastFlowLM/Gemma4-E2B-IT-NPU2/tree/main", - "ms_url": "https://modelscope.cn/models/amd/Gemma4-E2B-IT-NPU2", + "url": "https://huggingface.co/FastFlowLM/Gemma4-E2B-IT-NPU2/resolve/flm_1.0.15", + "file_url": "https://huggingface.co/api/models/FastFlowLM/Gemma4-E2B-IT-NPU2/tree/flm_1.0.15", + "ms_url": "https://modelscope.cn/models/amd/Gemma4-E2B-IT-NPU2/resolve/flm_1.0.15", "size": 2000000000, - "flm_min_version": "0.9.43", + "flm_min_version": "1.0.15", "files": [ "config.json", "model.q4nx", @@ -629,11 +629,11 @@ "e4b": { "supported_platforms": ["aie2p"], "name": "Gemma4-E4B-IT-NPU2", - "url": "https://huggingface.co/FastFlowLM/Gemma4-E4B-IT-NPU2", - "file_url": "https://huggingface.co/api/models/FastFlowLM/Gemma4-E4B-IT-NPU2/tree/main", - "ms_url": "https://modelscope.cn/models/amd/Gemma4-E4B-IT-NPU2", + "url": "https://huggingface.co/FastFlowLM/Gemma4-E4B-IT-NPU2/resolve/flm_1.0.15", + "file_url": "https://huggingface.co/api/models/FastFlowLM/Gemma4-E4B-IT-NPU2/tree/flm_1.0.15", + "ms_url": "https://modelscope.cn/models/amd/Gemma4-E4B-IT-NPU2/resolve/flm_1.0.15", "size": 4000000000, - "flm_min_version": "0.9.43", + "flm_min_version": "1.0.15", "files": [ "config.json", "model.q4nx", diff --git a/src/test/chat_tools/test_chat_tools.cpp b/src/test/chat_tools/test_chat_tools.cpp index 678d1a275..5874be10e 100644 --- a/src/test/chat_tools/test_chat_tools.cpp +++ b/src/test/chat_tools/test_chat_tools.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -2035,6 +2036,35 @@ void test_penalty_free_spans() { CHECK(!token_ban_constraint(64, {7}).penalty_free()); } +// Tool call arguments for the Gemma4 templates, which render only an object (or null) +void test_gemma4_template_messages() { + std::printf("gemma4 template gets tool call arguments as an object, or a reason it cannot\n"); + auto call = [](json arguments) { + return json::array({{{"role", "user"}, {"content", "hi"}}, + {{"role", "assistant"}, {"content", ""}, + {"tool_calls", json::array({{{"type", "function"}, + {"function", {{"name", "f"}, {"arguments", arguments}}}}})}}}); + }; + auto arguments = [](const json& messages) { return messages[1]["tool_calls"][0]["function"]["arguments"]; }; + CHECK(arguments(gemma4_template_messages(call("{\"a\":null,\"b\":{\"c\":1}}"))) == + (json{{"a", nullptr}, {"b", {{"c", 1}}}})); + CHECK(arguments(gemma4_template_messages(call(""))) == json::object()); + CHECK(arguments(gemma4_template_messages(call(json{{"a", 1}}))) == (json{{"a", 1}})); + CHECK(arguments(gemma4_template_messages(call(nullptr))).is_null()); + // messages without calls are left as they are + const json plain = json::array({{{"role", "user"}, {"content", "hi"}}}); + CHECK(gemma4_template_messages(plain) == plain); + for (const json bad : {json("oops"), json("[1]"), json("5"), json("{\"a\":1} trailing"), json::array({1}), json(5)}) { + bool threw = false; + try { + gemma4_template_messages(call(bad)); + } catch (const std::invalid_argument& e) { + threw = std::string(e.what()).find("\"f\"") != std::string::npos; + } + CHECK(threw); + } +} + } // namespace int main() { @@ -2073,6 +2103,7 @@ int main() { test_minicpm_template(); test_inkling(); test_penalty_free_spans(); + test_gemma4_template_messages(); if (failures) { std::printf("%d check(s) failed\n", failures); return 1; diff --git a/src/test/model_extension_gemma4e/gemma4_open_frontend.cpp b/src/test/model_extension_gemma4e/gemma4_open_frontend.cpp index a5193a3fa..f0c5f938e 100644 --- a/src/test/model_extension_gemma4e/gemma4_open_frontend.cpp +++ b/src/test/model_extension_gemma4e/gemma4_open_frontend.cpp @@ -442,6 +442,11 @@ class Frontend { !call["function"].contains("arguments")) continue; auto& arguments = call["function"]["arguments"]; if (!arguments.is_string()) continue; + // an empty string is a call without arguments, as in Gemma4e + if (arguments.get().empty()) { + arguments = json::object(); + continue; + } const auto parsed = json::parse( arguments.get(), nullptr, false); if (parsed.is_object()) arguments = parsed; diff --git a/src/test/model_extension_gemma4e/model-list.json b/src/test/model_extension_gemma4e/model-list.json index 3cf95799f..2772b01a2 100644 --- a/src/test/model_extension_gemma4e/model-list.json +++ b/src/test/model_extension_gemma4e/model-list.json @@ -5,12 +5,12 @@ "e2b": { "supported_platforms": ["aie2p"], "name": "Gemma4-Open-E2B-IT-NPU2", - "url": "https://huggingface.co/FastFlowLM/Gemma4-E2B-IT-NPU2", - "file_url": "https://huggingface.co/api/models/FastFlowLM/Gemma4-E2B-IT-NPU2/tree/main", + "url": "https://huggingface.co/FastFlowLM/Gemma4-E2B-IT-NPU2/resolve/flm_1.0.15", + "file_url": "https://huggingface.co/api/models/FastFlowLM/Gemma4-E2B-IT-NPU2/tree/flm_1.0.15", "model_info_key": "gemma4-it:e2b", - "ms_url": "https://modelscope.cn/models/amd/Gemma4-E2B-IT-NPU2", + "ms_url": "https://modelscope.cn/models/amd/Gemma4-E2B-IT-NPU2/resolve/flm_1.0.15", "size": 2000000000, - "flm_min_version": "0.9.43", + "flm_min_version": "1.0.15", "files": [ "config.json", "model.q4nx",