Skip to content

Commit dee40cf

Browse files
authored
Fix Qwen3 content extraction breaking code formatting (#661)
Problem: - qwen3::extract_content_during_parsing() used aggressive regex to collapse multiple newlines - This broke proper code formatting (e.g., PEP 8's 2 empty lines between functions) - Affected non-tool-call streaming output where formatting is critical Solution: - Replace aggressive std::regex_replace(R"(\n\s*\n)", "\n") with gentle string_strip() - Follow original llama.cpp patterns: only trim leading/trailing whitespace - Preserve internal formatting including multiple newlines - Add proper include for common.h to access string_strip function Changes: - examples/server/parsers/qwen3_parser.hpp: Replace whitespace cleanup with string_strip() - tests/test-function-calls.cpp: Add test_qwen3_whitespace_preservation() to prevent regression Testing: - ✅ PEP 8 compliance: 2 empty lines between functions preserved - ✅ Tool call parsing: All Qwen3 tests continue to pass - ✅ No regressions: Existing functionality maintained - ✅ Follows original llama.cpp whitespace handling patterns
1 parent e484944 commit dee40cf

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

examples/server/parsers/qwen3_parser.hpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "json.hpp"
4+
#include "../../common/common.h"
45
#include <string>
56
#include <regex>
67

@@ -102,12 +103,8 @@ static std::string extract_content_during_parsing(const std::string& text, bool
102103
}
103104
}
104105

105-
// Clean up extra whitespace
106-
content = std::regex_replace(content, std::regex(R"(\n\s*\n)"), "\n");
107-
108-
// Trim leading/trailing whitespace
109-
content.erase(0, content.find_first_not_of(" \t\n\r"));
110-
content.erase(content.find_last_not_of(" \t\n\r") + 1);
106+
// Only trim leading/trailing whitespace, preserve internal formatting
107+
content = string_strip(content);
111108

112109
} catch (const std::exception&) {
113110
// Return original text on regex errors

tests/test-function-calls.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2237,6 +2237,40 @@ void test_xml_tool_call_parsing() {
22372237
std::cout << " ✅ XML tool call parsing works correctly!" << std::endl;
22382238
}
22392239

2240+
// Test whitespace preservation in qwen3 content extraction
2241+
void test_qwen3_whitespace_preservation() {
2242+
std::cout << "\n🧹 Testing Qwen3 Whitespace Preservation Fix:" << std::endl;
2243+
2244+
// Test case with PEP 8 style: 2 empty lines between functions
2245+
const std::string pep8_content = R"(def celsius_to_fahrenheit(celsius):
2246+
return celsius * 9/5 + 32
2247+
2248+
2249+
def fahrenheit_to_celsius(fahrenheit):
2250+
return (fahrenheit - 32) * 5/9)";
2251+
2252+
std::cout << "🎯 Testing PEP 8 compliance (2 empty lines between functions)..." << std::endl;
2253+
std::cout << "Original content has: 2 empty lines between functions" << std::endl;
2254+
2255+
// Test the qwen3 content extraction directly
2256+
std::string result = qwen3::extract_content_during_parsing(pep8_content, false);
2257+
2258+
// Check if the double newlines are preserved (should have \n\n\n for 2 empty lines)
2259+
bool has_double_empty_lines = result.find("\n\n\n") != std::string::npos;
2260+
2261+
std::cout << "Result content: '" << result << "'" << std::endl;
2262+
std::cout << "Has 2 empty lines preserved: " << (has_double_empty_lines ? "YES" : "NO") << std::endl;
2263+
2264+
test_assert(has_double_empty_lines, "Qwen3: PEP 8 double empty lines preserved");
2265+
2266+
// Additional test: ensure no excessive trimming
2267+
test_assert(!result.empty(), "Qwen3: Content not empty after processing");
2268+
test_assert(result.find("celsius_to_fahrenheit") != std::string::npos, "Qwen3: Function content preserved");
2269+
test_assert(result.find("fahrenheit_to_celsius") != std::string::npos, "Qwen3: Second function preserved");
2270+
2271+
std::cout << " ✅ Qwen3 whitespace preservation working correctly!" << std::endl;
2272+
}
2273+
22402274
// Test the streaming tool calls fix implementation
22412275
void test_streaming_tool_calls_fix() {
22422276
std::cout << "\n=== Streaming Tool Calls Fix Validation ===" << std::endl;
@@ -2797,6 +2831,7 @@ int main() {
27972831
test_content_cleaning();
27982832
test_contamination_reproduction(); // Added this test
27992833
test_mixed_formats();
2834+
test_qwen3_whitespace_preservation(); // Test whitespace fix
28002835

28012836
std::cout << "\n🌍 Unicode & International Tests:" << std::endl;
28022837
test_unicode_support();

0 commit comments

Comments
 (0)