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: 1 addition & 1 deletion src/llm/io_processing/gptoss/harmony.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ ToolCalls_t Harmony::getToolCalls() {
static const std::string tool_prefix = "to=functions.";
ToolCalls_t toolCalls;
for (const auto& msg : messages) {
if (startsWith(msg.getChannel(), "commentary")) {
if (startsWith(msg.getChannel(), "commentary") || startsWith(msg.getChannel(), "analysis")) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a comment. For GenAI/OV testing purposes it could be useful to be able to turn off such workarounds, to check accuracy fixes, otherwise we may just mask those issues

size_t marker = msg.getChannel().find(tool_prefix);
if (marker != std::string::npos) {
marker += tool_prefix.length();
Expand Down
8 changes: 5 additions & 3 deletions src/llm/io_processing/gptoss/tool_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ std::optional<rapidjson::Document> GptOssToolParser::parseChunk(const std::strin
std::string chunk = newChunk;
std::optional<rapidjson::Document> result;

if (chunk.find(getParsingStartTags()[0]) != std::string::npos) {
toolCallIndex++; // starting with -1, first call will be 0
return std::nullopt;
for (const auto& parsingStartTag : getParsingStartTags()) {
if (chunk.find(parsingStartTag) != std::string::npos) {
toolCallIndex++; // starting with -1, first call will be 0
return std::nullopt;
}
}
Comment on lines +89 to 94
Copy link

Copilot AI Jan 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The loop performs multiple string searches on the same chunk. If the chunk matches an early tag, subsequent searches are unnecessary. Since the function returns immediately on match, this is already optimized, but consider adding a comment explaining that the order of tags in getParsingStartTags() may affect performance if one pattern is more common than others.

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

both start tags differ from each other significantly, new tags are not planned


// This should only happen during channel read if model does not produce garbage
Expand Down
5 changes: 4 additions & 1 deletion src/llm/io_processing/gptoss/tool_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ class GptOssToolParser : public BaseOutputParser {
std::optional<rapidjson::Document> parseChunk(const std::string& chunk, ov::genai::GenerationFinishReason finishReason) override;

const std::vector<std::string>& getParsingStartTags() const override {
static const std::vector<std::string> parsingStartTags{parsingStartTag};
static const std::vector<std::string> parsingStartTags{
parsingStartTag,
"<|channel|>analysis to=", // Workaround: allow tool calls emitted from the analysis channel (non-standard behavior observed in some model outputs).
};
return parsingStartTags;
}

Expand Down
101 changes: 101 additions & 0 deletions src/test/llm/output_parsers/gptoss_output_parser_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,36 @@ TEST_F(GptOssOutputUnaryParserTest, SingleToolCallWithConstrain) {
}
}

// This does not conform to OpenAI Harmony format, however tests show that sometimes model produces tools in analysis channel
TEST_F(GptOssOutputUnaryParserTest, SingleToolCallWithConstrainInAnalysisChannel) {
for (auto closureToken : std::vector<Harmony::TokenID>{
Harmony::TokenID::RETURN, // ending with <|return|>
Harmony::TokenID::END, // ending with <|end|>
Harmony::TokenID::CALL}) { // ending with <|call|>
for (auto functionDeclaration : std::vector<std::string>{
"analysis to=functions.hello", // valid channel with to=
"analysis to=functions.hello ",
"analysis to=functions.hello",
"analysis ANYTHING IN BETWEEN to=functions.hello",
}) { // spaces after hello
builder
.clear()
.add(Harmony::TokenID::CHANNEL)
.add(functionDeclaration)
.add(Harmony::TokenID::MESSAGE)
.add(R"({"Hello": "world!"})")
.add(closureToken);
Harmony harmony(*gptOssTokenizer, builder.build());
ASSERT_TRUE(harmony.parse()) << "Failed for closure token: " << static_cast<int64_t>(closureToken) << " function declaration: " << functionDeclaration;
ASSERT_EQ(harmony.getContent(), "") << "Failed for closure token: " << static_cast<int64_t>(closureToken) << " function declaration: " << functionDeclaration;
ASSERT_EQ(harmony.getReasoning(), "") << "Failed for closure token: " << static_cast<int64_t>(closureToken) << " function declaration: " << functionDeclaration;
ASSERT_EQ(harmony.getToolCalls().size(), 1) << "Failed for closure token: " << static_cast<int64_t>(closureToken) << " function declaration: " << functionDeclaration;
ASSERT_EQ(harmony.getToolCalls()[0].name, "hello") << "Failed for closure token: " << static_cast<int64_t>(closureToken) << " function declaration: " << functionDeclaration;
ASSERT_EQ(harmony.getToolCalls()[0].arguments, R"({"Hello": "world!"})") << "Failed for closure token: " << static_cast<int64_t>(closureToken) << " function declaration: " << functionDeclaration;
}
}
}

TEST_F(GptOssOutputUnaryParserTest, InvalidSingleToolCallWithConstrain) {
for (auto closureToken : std::vector<Harmony::TokenID>{
Harmony::TokenID::RETURN, // ending with <|return|>
Expand Down Expand Up @@ -637,3 +667,74 @@ TEST_F(GptOssOutputStreamParserTest, ConstrainAndCallGluedWithToolCall) {
};
test(chunkToDeltaVec);
}

// This does not conform to OpenAI Harmony format, however tests show that sometimes model produces tools in analysis channel
TEST_F(GptOssOutputStreamParserTest, HolisticStreamingToolsInAnalysisChannel) {
std::vector<std::tuple<std::string, ov::genai::GenerationFinishReason, std::optional<std::string>>> chunkToDeltaVec{
// Reasoning
{"<|channel|>", ov::genai::GenerationFinishReason::NONE, std::nullopt},
{"analysis", ov::genai::GenerationFinishReason::NONE, std::nullopt},
{"<|message|>", ov::genai::GenerationFinishReason::NONE, std::nullopt},
{"I", ov::genai::GenerationFinishReason::NONE, {R"({"delta":{"reasoning_content":"I"}})"}},
{" will", ov::genai::GenerationFinishReason::NONE, {R"({"delta":{"reasoning_content":" will"}})"}},
{" call", ov::genai::GenerationFinishReason::NONE, {R"({"delta":{"reasoning_content":" call"}})"}},
{" fun", ov::genai::GenerationFinishReason::NONE, {R"({"delta":{"reasoning_content":" fun"}})"}},
{"ction.", ov::genai::GenerationFinishReason::NONE, {R"({"delta":{"reasoning_content":"ction."}})"}},
{"<|end|>", ov::genai::GenerationFinishReason::NONE, std::nullopt},
// Preamble
{"<|channel|>", ov::genai::GenerationFinishReason::NONE, std::nullopt},
{"commentary", ov::genai::GenerationFinishReason::NONE, std::nullopt},
{"<|message|>", ov::genai::GenerationFinishReason::NONE, std::nullopt},
{"I", ov::genai::GenerationFinishReason::NONE, {R"({"delta":{"content":"I"}})"}},
{" have", ov::genai::GenerationFinishReason::NONE, {R"({"delta":{"content":" have"}})"}},
{" to", ov::genai::GenerationFinishReason::NONE, {R"({"delta":{"content":" to"}})"}},
{" call", ov::genai::GenerationFinishReason::NONE, {R"({"delta":{"content":" call"}})"}},
{" fun", ov::genai::GenerationFinishReason::NONE, {R"({"delta":{"content":" fun"}})"}},
{"ction.", ov::genai::GenerationFinishReason::NONE, {R"({"delta":{"content":"ction."}})"}},
{"<|end|>", ov::genai::GenerationFinishReason::NONE, std::nullopt},
// Tool 1 (with ignored constrain)
{"<|channel|>", ov::genai::GenerationFinishReason::NONE, std::nullopt},
{"analysis", ov::genai::GenerationFinishReason::NONE, std::nullopt},
{" to=", ov::genai::GenerationFinishReason::NONE, std::nullopt},
{"fun", ov::genai::GenerationFinishReason::NONE, std::nullopt},
{"ctions", ov::genai::GenerationFinishReason::NONE, std::nullopt},
{".hello ", ov::genai::GenerationFinishReason::NONE, std::nullopt},
{"<|message|>", ov::genai::GenerationFinishReason::NONE, "{\"delta\":{\"tool_calls\":[{\"id\":\"XXXXXXXXX\",\"type\":\"function\",\"index\":0,\"function\":{\"name\":\"hello\"}}]}}"},
{" {\"", ov::genai::GenerationFinishReason::NONE, R"({"delta":{"tool_calls":[{"index":0,"function":{"arguments":" {\""}}]}})"},
{"location", ov::genai::GenerationFinishReason::NONE, R"({"delta":{"tool_calls":[{"index":0,"function":{"arguments":"location"}}]}})"},
{"\":", ov::genai::GenerationFinishReason::NONE, R"({"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\":"}}]}})"},
{" \"", ov::genai::GenerationFinishReason::NONE, R"({"delta":{"tool_calls":[{"index":0,"function":{"arguments":" \""}}]}})"},
{"Paris", ov::genai::GenerationFinishReason::NONE, R"({"delta":{"tool_calls":[{"index":0,"function":{"arguments":"Paris"}}]}})"},
{"\"}", ov::genai::GenerationFinishReason::NONE, R"({"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\"}"}}]}})"},
{"<|call|>", ov::genai::GenerationFinishReason::NONE, std::nullopt},
// Tool 2 (with constrain)
{"<|channel|>", ov::genai::GenerationFinishReason::NONE, std::nullopt},
{"analysis", ov::genai::GenerationFinishReason::NONE, std::nullopt},
{" to=", ov::genai::GenerationFinishReason::NONE, std::nullopt},
{"fun", ov::genai::GenerationFinishReason::NONE, std::nullopt},
{"ctions", ov::genai::GenerationFinishReason::NONE, std::nullopt},
{".world ", ov::genai::GenerationFinishReason::NONE, std::nullopt},
{"<|constrain|>", ov::genai::GenerationFinishReason::NONE, "{\"delta\":{\"tool_calls\":[{\"id\":\"XXXXXXXXX\",\"type\":\"function\",\"index\":1,\"function\":{\"name\":\"world\"}}]}}"},
{"json", ov::genai::GenerationFinishReason::NONE, std::nullopt},
{"<|message|>", ov::genai::GenerationFinishReason::NONE, std::nullopt},
{" {\"", ov::genai::GenerationFinishReason::NONE, R"({"delta":{"tool_calls":[{"index":1,"function":{"arguments":" {\""}}]}})"},
{"location", ov::genai::GenerationFinishReason::NONE, R"({"delta":{"tool_calls":[{"index":1,"function":{"arguments":"location"}}]}})"},
{"\":", ov::genai::GenerationFinishReason::NONE, R"({"delta":{"tool_calls":[{"index":1,"function":{"arguments":"\":"}}]}})"},
{" \"", ov::genai::GenerationFinishReason::NONE, R"({"delta":{"tool_calls":[{"index":1,"function":{"arguments":" \""}}]}})"},
{"Warsaw", ov::genai::GenerationFinishReason::NONE, R"({"delta":{"tool_calls":[{"index":1,"function":{"arguments":"Warsaw"}}]}})"},
{"\"}", ov::genai::GenerationFinishReason::NONE, R"({"delta":{"tool_calls":[{"index":1,"function":{"arguments":"\"}"}}]}})"},
{"<|call|>", ov::genai::GenerationFinishReason::NONE, std::nullopt},
// Tool 3 (glued with call token)
{"<|channel|>", ov::genai::GenerationFinishReason::NONE, std::nullopt},
{"analysis", ov::genai::GenerationFinishReason::NONE, std::nullopt},
{" to=", ov::genai::GenerationFinishReason::NONE, std::nullopt},
{"fun", ov::genai::GenerationFinishReason::NONE, std::nullopt},
{"ctions", ov::genai::GenerationFinishReason::NONE, std::nullopt},
{".world ", ov::genai::GenerationFinishReason::NONE, std::nullopt},
{"<|constrain|>", ov::genai::GenerationFinishReason::NONE, "{\"delta\":{\"tool_calls\":[{\"id\":\"XXXXXXXXX\",\"type\":\"function\",\"index\":2,\"function\":{\"name\":\"world\"}}]}}"},
{"json", ov::genai::GenerationFinishReason::NONE, std::nullopt},
{"<|message|>", ov::genai::GenerationFinishReason::NONE, std::nullopt},
{"{}<|call|>", ov::genai::GenerationFinishReason::NONE, R"({"delta":{"tool_calls":[{"index":2,"function":{"arguments":"{}"}}]}})"},
};
test(chunkToDeltaVec);
}