Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions tests/test-chat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1898,6 +1898,41 @@ static void test_convert_responses_to_chatcmpl() {
assert_equals(100, result.at("max_tokens").get<int>());
}

// Test structured output conversion
{
json input = json::parse(R"({
"input": "Hello",
"model": "test-model",
"text": {
"format": {
"type": "json_schema",
"name": "TestOutput",
"schema": {
"type": "object",
"properties": {
"message": {"type": "string"}
},
"required": ["message"],
"additionalProperties": false
},
"strict": true
}
}
})");

json result = server_chat_convert_responses_to_chatcmpl(input);

assert_equals(false, result.contains("text"));
assert_equals(true, result.contains("response_format"));
const auto & response_format = result.at("response_format");
assert_equals(std::string("json_schema"), response_format.at("type").get<std::string>());
const auto & json_schema = response_format.at("json_schema");
assert_equals(std::string("TestOutput"), json_schema.at("name").get<std::string>());
assert_equals(true, json_schema.at("strict").get<bool>());
assert_equals(std::string("object"), json_schema.at("schema").at("type").get<std::string>());
assert_equals(false, json_schema.contains("type"));
}

// Test mixed Responses tools: convert only function tools
{
json input = json::parse(R"({
Expand Down
56 changes: 46 additions & 10 deletions tools/server/server-chat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,26 +217,34 @@ json server_chat_convert_responses_to_chatcmpl(const json & response_body) {
} else if (exists_and_is_array(item, "summary") &&
exists_and_is_string(item, "type") &&
item.at("type") == "reasoning") {
// #responses_create-input-input_item_list-item-reasoning

if (!exists_and_is_array(item, "content")) {
throw std::invalid_argument("item['content'] is not an array");
}
if (item.at("content").empty()) {
throw std::invalid_argument("item['content'] is empty");
std::string reasoning_text;

if (exists_and_is_array(item, "content") && !item.at("content").empty()) {
if (!exists_and_is_string(item.at("content")[0], "text")) {
throw std::invalid_argument("item['content']['text'] is not a string");
}
reasoning_text = item.at("content")[0].at("text").get<std::string>();
} else if (!item.at("summary").empty()) {
for (const auto & summary_item : item.at("summary")) {
if (exists_and_is_string(summary_item, "text")) {
reasoning_text += summary_item.at("text").get<std::string>();
}
}
}
if (!exists_and_is_string(item.at("content")[0], "text")) {
throw std::invalid_argument("item['content']['text'] is not a string");

if (reasoning_text.empty()) {
continue;
}

if (merge_prev) {
auto & prev_msg = chatcmpl_messages.back();
prev_msg["reasoning_content"] = item.at("content")[0].at("text");
prev_msg["reasoning_content"] = reasoning_text;
} else {
chatcmpl_messages.push_back(json {
{"role", "assistant"},
{"content", json::array()},
{"reasoning_content", item.at("content")[0].at("text")},
{"reasoning_content", reasoning_text},
});
}
} else {
Expand Down Expand Up @@ -283,6 +291,34 @@ json server_chat_convert_responses_to_chatcmpl(const json & response_body) {
chatcmpl_body["max_tokens"] = response_body["max_output_tokens"];
}

if (response_body.contains("text")) {
const json & text = response_body.at("text");
if (!text.is_object()) {
throw std::invalid_argument("'text' must be an object");
}

chatcmpl_body.erase("text");
if (text.contains("format")) {
const json & format = text.at("format");
if (!format.is_object()) {
throw std::invalid_argument("'text.format' must be an object");
}

const std::string type = json_value(format, "type", std::string());
if (type == "json_schema") {
chatcmpl_body["response_format"] = {
{"type", "json_schema"},
{"json_schema", format},
};
chatcmpl_body["response_format"]["json_schema"].erase("type");
} else if (type == "json_object" || type == "text") {
chatcmpl_body["response_format"] = format;
} else if (!type.empty()) {
throw std::invalid_argument("'text.format.type' must be one of 'text', 'json_object', or 'json_schema'");
}
}
}

if (response_body.contains("reasoning")) {
// Only "effort" is handled so far
const json & reasoning = response_body.at("reasoning");
Expand Down
Loading