server : preserve anthropic thinking blocks in conversion (#20120)

* server : preserve anthropic thinking blocks in conversion (#20090)

* server : add tests for anthropic thinking block conversion

---------

Co-authored-by: root <root@llamacpp.home>
This commit is contained in:
Tom Vaucourt 2026-03-06 17:41:12 +01:00 committed by GitHub
parent ba2fd11cdf
commit e68f2fb894
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 141 additions and 2 deletions

View file

@ -1463,6 +1463,7 @@ json convert_anthropic_to_oai(const json & body) {
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 (const auto & block : content) {
@ -1470,6 +1471,8 @@ json convert_anthropic_to_oai(const json & body) {
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());
@ -1528,16 +1531,19 @@ json convert_anthropic_to_oai(const json & body) {
}
}
if (!converted_content.empty() || has_tool_calls) {
if (!converted_content.empty() || has_tool_calls || !reasoning_content.empty()) {
json new_msg = {{"role", role}};
if (!converted_content.empty()) {
new_msg["content"] = converted_content;
} else if (has_tool_calls) {
} 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);
}