8000 server : proper error handling for missing elements in messages array… · ggml-org/llama.cpp@c753d7b · GitHub
[go: up one dir, main page]

Skip to content

Commit c753d7b

Browse files
authored
server : proper error handling for missing elements in messages array (OpenAI compatible backend) (#13540)
1 parent b283804 commit c753d7b

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

tools/server/utils.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,18 @@ static json oaicompat_completion_params_parse(
643643
throw std::runtime_error("Expected 'messages' to be an array");
644644
}
645645
for (auto & msg : messages) {
646+
std::string role = json_value(msg, "role", std::string());
647+
if (role != "assistant" && !msg.contains("content")) {
648+
throw std::runtime_error("All non-assistant messages must contain 'content'");
649+
}
650+
if (role == "assistant") {
651+
if (!msg.contains("content") && !msg.contains("tool_calls")) {
652+
throw std::runtime_error("Assistant message must contain either 'content' or 'tool_calls'!");
653+
}
654+
if (!msg.contains("content")) {
655+
continue; // avoid errors with no content
656+
}
657+
}
646658
json & content = msg.at("content");
647659
if (content.is_string() || content.is_null()) {
648660
continue;

0 commit comments

Comments
 (0)
0