From: Piotr Wilkin (ilintar) Date: Thu, 15 May 2025 06:40:58 +0000 (+0200) Subject: server : proper error handling for missing elements in messages array (OpenAI compati... X-Git-Url: https://git.djapps.eu/?a=commitdiff_plain;h=c753d7bed0dc2cc2d5c42dfa9806cba91748392e;p=pkg%2Fggml%2Fsources%2Fllama.cpp server : proper error handling for missing elements in messages array (OpenAI compatible backend) (#13540) --- diff --git a/tools/server/utils.hpp b/tools/server/utils.hpp index 45193c17..232eef19 100644 --- a/tools/server/utils.hpp +++ b/tools/server/utils.hpp @@ -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;