From 14243f05855a190f336d2765e6f71a71f9103d17 Mon Sep 17 00:00:00 2001 From: Park Woorak Date: Sun, 20 Apr 2025 20:49:43 +0900 Subject: [PATCH 1/2] fix: require one of 'content' or 'tool_calls' in messages --- include/minja/chat-template.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/minja/chat-template.hpp b/include/minja/chat-template.hpp index 142a58e..5d395f7 100644 --- a/include/minja/chat-template.hpp +++ b/include/minja/chat-template.hpp @@ -395,8 +395,8 @@ class chat_template { for (const auto & message_ : adjusted_messages) { auto message = message_; - if (!message.contains("role") || !message.contains("content")) { - throw std::runtime_error("message must have 'role' and 'content' fields: " + message.dump()); + if (!message.contains("role") || (!message.contains("content") && !message.contains("tool_calls"))) { + throw std::runtime_error("message must have 'role' and one of 'content' or 'tool_calls' fields: " + message.dump()); } std::string role = message.at("role"); From df77a18f7a297517c7de039da77888d523771f95 Mon Sep 17 00:00:00 2001 From: Park Woorak Date: Sun, 20 Apr 2025 21:10:51 +0900 Subject: [PATCH 2/2] fix: check content is given during polyfill tool_calls --- include/minja/chat-template.hpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/include/minja/chat-template.hpp b/include/minja/chat-template.hpp index 5d395f7..ab5b521 100644 --- a/include/minja/chat-template.hpp +++ b/include/minja/chat-template.hpp @@ -417,7 +417,6 @@ class chat_template { } } if (polyfill_tool_calls) { - auto content = message.at("content"); auto tool_calls = json::array(); for (const auto & tool_call : message.at("tool_calls")) { if (tool_call.at("type") != "function") { @@ -436,8 +435,11 @@ class chat_template { auto obj = json { {"tool_calls", tool_calls}, }; - if (!content.is_null() && !content.empty()) { - obj["content"] = content; + if (message.contains("content")) { + auto content = message.at("content"); + if (!content.is_null() && !content.empty()) { + obj["content"] = content; + } } message["content"] = obj.dump(2); message.erase("tool_calls");