-
Notifications
You must be signed in to change notification settings - Fork 261
Accept empty content array - transform it to null #4359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/llm/io_processing/input_processors/empty_content_array_normalization_processor.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"]; | ||
| if (content.is_array() && content.size() == 0) { | ||
| chatHistory[i]["content"] = ov::genai::JsonContainer(nullptr); | ||
| } | ||
| } | ||
| return absl::OkStatus(); | ||
| } | ||
|
|
||
| } // namespace ovms | ||
31 changes: 31 additions & 0 deletions
31
src/llm/io_processing/input_processors/empty_content_array_normalization_processor.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/test/llm/input_processing/empty_content_array_normalization_processor_test.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.