From bfc79a340eb432d629a708e6309907146a71cb61 Mon Sep 17 00:00:00 2001 From: Yi-Chun Kuo Date: Thu, 10 Jul 2025 18:45:15 +0000 Subject: [PATCH] Fix null content for chat-template. The nullptr cause `Test failed: basic_string: construction from null is not valid`. Change to use json j_null instead. Tests pass with `./scripts/tests.sh` --- include/minja/chat-template.hpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/include/minja/chat-template.hpp b/include/minja/chat-template.hpp index 3bc7c77..8f617db 100644 --- a/include/minja/chat-template.hpp +++ b/include/minja/chat-template.hpp @@ -165,11 +165,12 @@ class chat_template { auto out_empty = try_raw_render(json::array({dummy_user_msg, {{"role", "assistant"}, {"content", ""}}}), {}, false); auto out_null = try_raw_render(json::array({dummy_user_msg, {{"role", "assistant"}, {"content", nullptr}}}), {}, false); caps_.requires_non_null_content = contains(out_empty, user_needle) && !contains(out_null, user_needle); - + + json j_null; auto make_tool_calls_msg = [&](const json & tool_calls) { return json { {"role", "assistant"}, - {"content", caps_.requires_non_null_content? "" : nullptr}, + {"content", caps_.requires_non_null_content? "" : j_null}, {"tool_calls", tool_calls}, }; }; @@ -235,7 +236,7 @@ class chat_template { }; const json tool_call_msg { {"role", "assistant"}, - {"content", caps_.requires_non_null_content ? "" : nullptr}, + {"content", caps_.requires_non_null_content ? "" : j_null}, {"tool_calls", json::array({ { // TODO: detect if requires numerical id or fixed length == 6 like Nemo