From fe92e30d1e75fa1171faa112018ba24614209407 Mon Sep 17 00:00:00 2001 From: hksdpc255 <43977088+hksdpc255@users.noreply.github.com> Date: Mon, 16 Mar 2026 23:59:19 +1100 Subject: [PATCH] server : preserve anthropic thinking blocks in conversion (#1441) --- examples/server/server-common.cpp | 49 +++++++++++++------------------ 1 file changed, 20 insertions(+), 29 deletions(-) diff --git a/examples/server/server-common.cpp b/examples/server/server-common.cpp index 8876641c..4d496c9f 100644 --- a/examples/server/server-common.cpp +++ b/examples/server/server-common.cpp @@ -1250,6 +1250,7 @@ json anthropic_params_from_json( json tool_calls = json::array(); json converted_content = json::array(); json tool_results = json::array(); + std::string reasoning_content; bool has_tool_calls = false; for (auto& block : content) { @@ -1258,6 +1259,9 @@ json anthropic_params_from_json( if (type == "text") { converted_content.push_back(block); } + else if (type == "thinking") { + reasoning_content += json_value(block, "thinking", std::string()); + } else if (type == "image") { json source = json_value(block, "source", json::object()); std::string source_type = json_value(source, "type", std::string()); @@ -1318,38 +1322,25 @@ json anthropic_params_from_json( } } - if (!tool_results.empty()) { - if (!converted_content.empty() || has_tool_calls) { - json new_msg = { {"role", role} }; - if (!converted_content.empty()) { - new_msg["content"] = converted_content; - } - else if (has_tool_calls) { - new_msg["content"] = ""; - } - if (!tool_calls.empty()) { - new_msg["tool_calls"] = tool_calls; - } - oai_messages.push_back(new_msg); + if (!converted_content.empty() || has_tool_calls || !reasoning_content.empty()) { + json new_msg = {{"role", role}}; + if (!converted_content.empty()) { + new_msg["content"] = converted_content; } - for (const auto& tool_msg : tool_results) { - oai_messages.push_back(tool_msg); + else if (has_tool_calls || !reasoning_content.empty()) { + new_msg["content"] = ""; } + if (!tool_calls.empty()) { + new_msg["tool_calls"] = tool_calls; + } + if (!reasoning_content.empty()) { + new_msg["reasoning_content"] = reasoning_content; + } + oai_messages.push_back(new_msg); } - else { - if (!converted_content.empty() || has_tool_calls) { - json new_msg = { {"role", role} }; - if (!converted_content.empty()) { - new_msg["content"] = converted_content; - } - else if (has_tool_calls) { - new_msg["content"] = ""; - } - if (!tool_calls.empty()) { - new_msg["tool_calls"] = tool_calls; - } - oai_messages.push_back(new_msg); - } + + for (const auto& tool_msg : tool_results) { + oai_messages.push_back(tool_msg); } }