Skip to content
Merged
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
2 changes: 2 additions & 0 deletions src/llm/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,14 @@ ovms_cc_library(
"io_processing/input_processors/chat_template_processor.hpp",
"io_processing/input_processors/chat_template_adapter.hpp",
"io_processing/chat_template/caps.hpp",
"io_processing/input_processors/empty_content_array_normalization_processor.hpp",
"io_processing/input_processors/raw_prompt_extractor.hpp",
"io_processing/input_processors/text_content_normalization_processor.hpp",
"io_processing/input_processors/tokenization_processor.hpp"],
srcs = ["io_processing/input_processors/image_decoding_processor.cpp",
"io_processing/input_processors/chat_template_processor.cpp",
"io_processing/input_processors/chat_template_adapter.cpp",
"io_processing/input_processors/empty_content_array_normalization_processor.cpp",
"io_processing/input_processors/text_content_normalization_processor.cpp",
"io_processing/input_processors/tokenization_processor.cpp"],
deps = [
Expand Down
7 changes: 3 additions & 4 deletions src/llm/apis/openai_completions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,9 @@ absl::Status OpenAIChatCompletionsHandler::parseMessages(std::optional<std::stri
continue;
}
if (memberName == "content" && member->value.IsArray()) {
// Validate the content array and check whether it contains images.
if (member->value.GetArray().Size() == 0) {
return absl::InvalidArgumentError("Invalid message structure - content array is empty");
}
// Empty content arrays are accepted and preserved as-is. The
// EmptyContentArrayNormalizationProcessor converts them to null before
// downstream processing.
for (const auto& v : member->value.GetArray()) {
if (!v.IsObject()) {
return absl::InvalidArgumentError("Invalid message structure - content array should contain objects");
Expand Down
28 changes: 16 additions & 12 deletions src/llm/io_processing/input_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "../../config.hpp"
#include "../../logging.hpp"
#include "input_processors/chat_template_processor.hpp"
#include "input_processors/empty_content_array_normalization_processor.hpp"
#include "input_processors/image_decoding_processor.hpp"
#include "input_processors/chat_template_adapter.hpp"
#include "input_processors/raw_prompt_extractor.hpp"
Expand All @@ -37,22 +38,25 @@ InputProcessor::InputProcessor(InputProcessorContext& context,
// Chat template already adds special tokens; completions path needs them added by the tokenizer.
const bool addSpecialTokens = !isChatPath;

if (context.config.isVLM && isChatPath) {
const auto& settings = Config::instance().getServerSettings();
processors.emplace_back(std::make_unique<ImageDecodingProcessor>(
settings.allowedLocalMediaPath,
settings.allowedMediaDomains));
}
if (isChatPath) {
// Normalize empty content arrays to null before any content-aware processor runs.
processors.emplace_back(std::make_unique<EmptyContentArrayNormalizationProcessor>());

if (!context.config.isVLM && isChatPath) {
// Flatten text-only content arrays for both LM and VLM. Arrays that contain
// images (or other modalities) are left untouched for ImageDecodingProcessor.
processors.emplace_back(std::make_unique<TextContentNormalizationProcessor>());
}

if (isChatPath && context.chatTemplateCaps.needsWorkarounds()) {
processors.emplace_back(std::make_unique<ChatTemplateAdapter>(context.chatTemplateCaps));
}
if (context.config.isVLM) {
const auto& settings = Config::instance().getServerSettings();
processors.emplace_back(std::make_unique<ImageDecodingProcessor>(
settings.allowedLocalMediaPath,
settings.allowedMediaDomains));
}

if (context.chatTemplateCaps.needsWorkarounds()) {
processors.emplace_back(std::make_unique<ChatTemplateAdapter>(context.chatTemplateCaps));
}

if (isChatPath) {
#if (PYTHON_DISABLE == 0)
// Select the path at construction time. If !useMinja but templateProcessor is null
// (shouldn't happen on a properly initialized servable), fall back to the native path.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//*****************************************************************************
// Copyright 2026 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*****************************************************************************

#include "empty_content_array_normalization_processor.hpp"

#include <variant>

namespace ovms {

absl::Status EmptyContentArrayNormalizationProcessor::process(InputRequest& req) {
if (!std::holds_alternative<ov::genai::ChatHistory>(req.input)) {
return absl::Status(absl::StatusCode::kInternal,
"EmptyContentArrayNormalizationProcessor received input that is not a ChatHistory");
}
ov::genai::ChatHistory& chatHistory = std::get<ov::genai::ChatHistory>(req.input);
for (size_t i = 0; i < chatHistory.size(); i++) {
const auto content = chatHistory[i]["content"];
Comment thread
mzegla marked this conversation as resolved.
if (content.is_array() && content.size() == 0) {
chatHistory[i]["content"] = ov::genai::JsonContainer(nullptr);
}
}
return absl::OkStatus();
}

} // namespace ovms
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//*****************************************************************************
// Copyright 2026 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*****************************************************************************
#pragma once

#include "../base_input_processor.hpp"

namespace ovms {

// Replaces empty content arrays ("content": []) in ChatHistory messages with null.
// Runs for all chat paths (LM and VLM) and must execute before ImageDecodingProcessor
// and TextContentNormalizationProcessor so downstream processors and chat templates
// see a null content instead of an empty array.
class EmptyContentArrayNormalizationProcessor : public BaseInputProcessor {
public:
absl::Status process(InputRequest& req) override;
};

} // namespace ovms
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,26 @@ absl::Status TextContentNormalizationProcessor::process(InputRequest& req) {
if (!content.is_array()) {
continue;
}
// Only flatten arrays that contain exclusively text parts. Arrays with
// images (or other modalities) are left untouched for ImageDecodingProcessor.
// Single pass: build the combined string while scanning, and bail out on the
// first non-text part without touching the message.
std::string combined;
bool allText = true;
for (size_t j = 0; j < content.size(); j++) {
const auto part = content[j];
if (part["type"].as_string().value_or("") != "text") {
continue;
allText = false;
break;
}
if (!combined.empty()) {
combined += "\n";
}
combined += part["text"].as_string().value_or("");
}
if (!allText) {
continue;
}
chatHistory[i]["content"] = combined;
}
return absl::OkStatus();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@

namespace ovms {

// Normalizes text-only content arrays in ChatHistory messages to plain strings.
// Parts are joined with "\n" for backward compatibility with LM chat templates.
// Active when: !config.isVLM && input is ChatHistory variant.
// Flattens text-only content arrays in ChatHistory messages to plain strings.
// Parts are joined with "\n" for backward compatibility with chat templates.
// Runs for both LM and VLM chat paths: arrays that contain images (or other
// non-text modalities) are left untouched for ImageDecodingProcessor.
// Must run before ChatTemplateProcessor.
class TextContentNormalizationProcessor : public BaseInputProcessor {
public:
Expand Down
11 changes: 9 additions & 2 deletions src/test/http_openai_handler_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2325,7 +2325,7 @@ TEST_F(HttpOpenAIHandlerParsingTest, ParsingMessagesWithInvalidContentTypeFails)
EXPECT_EQ(apiHandler->parseMessages(), absl::InvalidArgumentError("Unsupported content type"));
}

TEST_F(HttpOpenAIHandlerParsingTest, ParsingMessagesEmptyContentArrayFails) {
TEST_F(HttpOpenAIHandlerParsingTest, ParsingMessagesEmptyContentArrayPreservesArray) {
std::string json = R"({
"model": "llama",
"messages": [
Expand All @@ -2338,7 +2338,14 @@ TEST_F(HttpOpenAIHandlerParsingTest, ParsingMessagesEmptyContentArrayFails) {
doc.Parse(json.c_str());
ASSERT_FALSE(doc.HasParseError());
std::shared_ptr<ovms::OpenAIChatCompletionsHandler> apiHandler = std::make_shared<ovms::OpenAIChatCompletionsHandler>(doc, ovms::Endpoint::CHAT_COMPLETIONS, std::chrono::system_clock::now(), *tokenizer);
EXPECT_EQ(apiHandler->parseMessages(), absl::InvalidArgumentError("Invalid message structure - content array is empty"));
// Empty content arrays are accepted and preserved as-is. The
// EmptyContentArrayNormalizationProcessor converts them to null downstream.
ASSERT_EQ(apiHandler->parseMessages(), absl::OkStatus());
auto& chatHistory = apiHandler->getChatHistory();
ASSERT_EQ(chatHistory.size(), 1u);
auto content = chatHistory[0]["content"];
EXPECT_TRUE(content.is_array());
EXPECT_EQ(content.size(), 0u);
}

TEST_F(HttpOpenAIHandlerParsingTest, ParsingMessagesWithNoContentFieldAddsEmptyStringToChatHistory) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//*****************************************************************************
// Copyright 2026 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*****************************************************************************
#include <string>

#include <gtest/gtest.h>
#include <openvino/genai/chat_history.hpp>

#include "../../../llm/io_processing/input_processors/empty_content_array_normalization_processor.hpp"
#include "../../../llm/io_processing/input_request.hpp"

using namespace ovms;

// Helpers ----------------------------------------------------------------

static InputRequest makeChatRequest(ov::genai::ChatHistory chatHistory) {
InputRequest req;
req.input = std::move(chatHistory);
return req;
}

// Tests ------------------------------------------------------------------

TEST(EmptyContentArrayNormalizationProcessorTest, EmptyArrayConvertedToNull) {
ov::genai::ChatHistory history;
ov::AnyMap msg = {{"role", std::string("user")}};
msg["content"] = ov::genai::JsonContainer::from_json_string("[]");
history.push_back(msg);

InputRequest req = makeChatRequest(history);
EmptyContentArrayNormalizationProcessor processor;
const auto status = processor.process(req);

EXPECT_TRUE(status.ok());
const auto& result = std::get<ov::genai::ChatHistory>(req.input);
EXPECT_FALSE(result[0]["content"].is_array());
EXPECT_TRUE(result[0]["content"].is_null());
}

TEST(EmptyContentArrayNormalizationProcessorTest, NonEmptyArrayPreserved) {
ov::genai::ChatHistory history;
ov::AnyMap msg = {{"role", std::string("user")}};
msg["content"] = ov::genai::JsonContainer::from_json_string(
R"([{"type":"text","text":"hello"}])");
history.push_back(msg);

InputRequest req = makeChatRequest(history);
EmptyContentArrayNormalizationProcessor processor;
const auto status = processor.process(req);

EXPECT_TRUE(status.ok());
const auto& result = std::get<ov::genai::ChatHistory>(req.input);
ASSERT_TRUE(result[0]["content"].is_array());
EXPECT_EQ(result[0]["content"].size(), 1u);
}

TEST(EmptyContentArrayNormalizationProcessorTest, StringContentPassedThrough) {
ov::genai::ChatHistory history;
history.push_back({{"role", "user"}, {"content", "Hello, world!"}});

InputRequest req = makeChatRequest(history);
EmptyContentArrayNormalizationProcessor processor;
const auto status = processor.process(req);

EXPECT_TRUE(status.ok());
const auto& result = std::get<ov::genai::ChatHistory>(req.input);
EXPECT_EQ(result[0]["content"].as_string().value_or(""), "Hello, world!");
}

TEST(EmptyContentArrayNormalizationProcessorTest, RawPromptInputRejected) {
InputRequest req;
req.input = std::string("raw prompt");
EmptyContentArrayNormalizationProcessor processor;
const auto status = processor.process(req);

EXPECT_EQ(status.code(), absl::StatusCode::kInternal);
}
Original file line number Diff line number Diff line change
Expand Up @@ -342,20 +342,23 @@ TEST_P(InputProcessingIntegrationTest, GenerationConfigFields_SurviveFullPipelin
}

TEST_P(InputProcessingIntegrationTest, TextArrayContent_FlattenedByNormalizationProcessor) {
// Non-VLM path: content array with two text parts must be flattened by
// TextContentNormalizationProcessor so both strings appear in the final prompt.
auto result = runPipeline(textArrayJson(), /*isVLM=*/false);

ASSERT_TRUE(result.parseStatus.ok()) << result.parseStatus.message();
ASSERT_TRUE(result.processStatus.ok()) << result.processStatus.message();

// A text-only content array must be flattened by TextContentNormalizationProcessor
// so both strings appear in the final prompt. This holds on both the LM path and the
// VLM path used without images (VLM is a superset of LM).
// TextContentNormalizationProcessor joins the two parts with \n before the template runs.
const std::string expected =
std::string(SMOL_DEFAULT_SYSTEM) +
"<|im_start|>user\nFirst part.\nSecond part.<|im_end|>\n"
"<|im_start|>assistant\n";
EXPECT_EQ(result.req.promptText, expected);
EXPECT_TRUE(result.req.inputImages.empty());

for (bool isVLM : {false, true}) {
auto result = runPipeline(textArrayJson(), isVLM);

ASSERT_TRUE(result.parseStatus.ok()) << "isVLM=" << isVLM << ": " << result.parseStatus.message();
ASSERT_TRUE(result.processStatus.ok()) << "isVLM=" << isVLM << ": " << result.processStatus.message();
EXPECT_EQ(result.req.promptText, expected) << "isVLM=" << isVLM;
EXPECT_TRUE(result.req.inputImages.empty()) << "isVLM=" << isVLM;
}
}

// ---------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ TEST(TextContentNormalizationProcessorTest, MultipleTextPartsJoinedWithNewline)
EXPECT_EQ(result[0]["content"].as_string().value_or(""), "first\nsecond");
}

TEST(TextContentNormalizationProcessorTest, NonTextPartsIgnored) {
// image_url entries alongside text: only text parts should contribute to combined string.
TEST(TextContentNormalizationProcessorTest, MixedContentArrayLeftUntouched) {
// image_url entries alongside text: the array is NOT text-only, so it must be
// left untouched for ImageDecodingProcessor to handle downstream.
ov::genai::ChatHistory history;
ov::AnyMap msg = {{"role", std::string("user")}};
ov::genai::JsonContainer parts = ov::genai::JsonContainer::from_json_string(
Expand All @@ -95,5 +96,6 @@ TEST(TextContentNormalizationProcessorTest, NonTextPartsIgnored) {

EXPECT_TRUE(status.ok());
const auto& result = std::get<ov::genai::ChatHistory>(req.input);
EXPECT_EQ(result[0]["content"].as_string().value_or(""), "describe this");
ASSERT_TRUE(result[0]["content"].is_array());
EXPECT_EQ(result[0]["content"].size(), 2u);
}