]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/commitdiff
server : proper error handling for missing elements in messages array (OpenAI compati...
authorPiotr Wilkin (ilintar) <redacted>
Thu, 15 May 2025 06:40:58 +0000 (08:40 +0200)
committerGitHub <redacted>
Thu, 15 May 2025 06:40:58 +0000 (08:40 +0200)
tools/server/utils.hpp

index 45193c17cfd98dc5a3ddf03d1e9697b2bcd331e6..232eef195437f4f9d4968e48e2565c632c63fbd6 100644 (file)
@@ -643,6 +643,18 @@ static json oaicompat_completion_params_parse(
         throw std::runtime_error("Expected 'messages' to be an array");
     }
     for (auto & msg : messages) {
+        std::string role = json_value(msg, "role", std::string());
+        if (role != "assistant" && !msg.contains("content")) {
+            throw std::runtime_error("All non-assistant messages must contain 'content'");
+        }
+        if (role == "assistant") {
+            if (!msg.contains("content") && !msg.contains("tool_calls")) {
+                throw std::runtime_error("Assistant message must contain either 'content' or 'tool_calls'!");
+            }
+            if (!msg.contains("content")) {
+                continue; // avoid errors with no content
+            }
+        }
         json & content = msg.at("content");
         if (content.is_string() || content.is_null()) {
             continue;