From 6cef047c54be8184c32177cf8b80e99673ba6dc7 Mon Sep 17 00:00:00 2001 From: Piotr Wilkin Date: Wed, 14 May 2025 15:12:23 +0200 Subject: [PATCH] fix: proper error handling for missing elements in messages array (OpenAI compatible backend) --- tools/server/utils.hpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tools/server/utils.hpp b/tools/server/utils.hpp index 45193c17cfd..232eef19543 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;