Commit 2924a0e26 for llama.cpp

commit 2924a0e26b10f75e3f970443d5fca204cea4cae1
Author: patricksamuelevans <66663476+patricksamuelevans@users.noreply.github.com>
Date:   Mon Nov 30 18:03:08 2026 +0700

    common : fix Qwen3-ASR transcription context placement (#33075) (#33135)

    Route the transcription prompt into the system turn so the user turn stays audio only. Add regression coverage for the message layout and language handling.

    Assisted-by: OpenCode Hy4 flash Free

diff --git a/common/chat.cpp b/common/chat.cpp
index fba4b8226..7ec670f30 100644
--- a/common/chat.cpp
+++ b/common/chat.cpp
@@ -890,6 +890,11 @@ static bool is_vibevoice_asr_template(const std::string & src) {
     return src.find("transcribes audio input into text output in JSON format") != std::string::npos;
 }

+// Qwen3-ASR: the template marks the transcript boundary with <asr_text>
+static bool is_qwen3_asr_template(const std::string & src) {
+    return src.find("<asr_text>") != std::string::npos;
+}
+
 // LFM3-Audio: language code -> name
 static std::string lfm3_language_name(const std::string & language) {
     static const std::map<std::string, std::string> names = {
@@ -936,6 +941,13 @@ common_chat_prompt_preset common_chat_get_asr_prompt(const common_chat_templates
             }
             asr_preset.media_first        = true;
             asr_preset.needs_audio_length = true;
+        } else if (is_qwen3_asr_template(src)) {
+            // Context belongs in the system turn and the user turn is audio only.
+            // Language is detected from the audio, so do not append it to the user text.
+            asr_preset.system       = context;
+            asr_preset.user         = "";
+            asr_preset.has_context  = true;
+            asr_preset.has_language = true;
         }
     }

diff --git a/common/chat.h b/common/chat.h
index f80ef4ed2..fae6b62ef 100644
--- a/common/chat.h
+++ b/common/chat.h
@@ -520,8 +520,8 @@ std::optional<common_chat_params> common_chat_try_specialized_template(
 struct common_chat_prompt_preset {
     std::string system;
     std::string user;
-    bool has_language       = false; // the preset already names the requested language
-    bool has_context        = false; // the user text already holds the context
+    bool has_language       = false; // the preset already handles the language, do not append it
+    bool has_context        = false; // the preset already handles the context, use preset.user
     bool media_first        = false; // the media goes before the user text
     bool needs_audio_length = false; // the user text depends on the audio length, call again with it
 };
diff --git a/tests/test-chat.cpp b/tests/test-chat.cpp
index cb075a75e..7726473d0 100644
--- a/tests/test-chat.cpp
+++ b/tests/test-chat.cpp
@@ -2213,6 +2213,68 @@ static void test_convert_responses_to_chatcmpl() {
     }
 }

+// Qwen3-ASR expects conditioning text in the system turn, not next to the audio
+static void test_convert_transcriptions_to_chatcmpl() {
+    LOG_DBG("%s\n", __func__);
+
+    auto tmpls = read_templates("models/templates/Qwen3-ASR.jinja");
+
+    json input = json::parse(R"({
+        "model": "qwen3-asr",
+        "prompt": "previous segment transcript"
+    })");
+
+    std::map<std::string, uploaded_file> in_files;
+    uploaded_file file;
+    file.data = raw_buffer{ 1, 2, 3 };
+    in_files["file"] = file;
+
+    std::vector<raw_buffer> out_files;
+    json result = convert_transcriptions_to_chatcmpl(input, tmpls.get(), in_files, out_files);
+
+    const auto & messages = result.at("messages");
+    assert_equals((size_t)2, messages.size());
+
+    assert_equals(std::string("system"), messages[0].at("role").get<std::string>());
+    assert_equals(std::string("previous segment transcript"), messages[0].at("content").get<std::string>());
+
+    assert_equals(std::string("user"), messages[1].at("role").get<std::string>());
+    const auto & user_content = messages[1].at("content").get<std::string>();
+    assert_not_contains(user_content, "previous segment transcript");
+    // the audio marker must still be there so the server can splice the media in
+    assert_equals(std::string(get_media_marker()), user_content);
+
+    assert_equals((size_t)1, out_files.size());
+
+    // a supplied language must not be appended back into the user turn
+    {
+        json input_lang = json::parse(R"({
+            "model": "qwen3-asr",
+            "prompt": "previous segment transcript",
+            "language": "ja"
+        })");
+        std::vector<raw_buffer> out_files_lang;
+        json result_lang = convert_transcriptions_to_chatcmpl(input_lang, tmpls.get(), in_files, out_files_lang);
+        const auto & messages_lang = result_lang.at("messages");
+        assert_equals((size_t)2, messages_lang.size());
+        assert_equals(std::string("previous segment transcript"), messages_lang[0].at("content").get<std::string>());
+        const auto & user_lang = messages_lang[1].at("content").get<std::string>();
+        assert_not_contains(user_lang, "previous segment transcript");
+        assert_not_contains(user_lang, "language");
+    }
+
+    // no prompt means no system message and an audio-only user turn
+    {
+        json input_empty = json::parse(R"({ "model": "qwen3-asr" })");
+        std::vector<raw_buffer> out_files_empty;
+        json result_empty = convert_transcriptions_to_chatcmpl(input_empty, tmpls.get(), in_files, out_files_empty);
+        const auto & messages_empty = result_empty.at("messages");
+        assert_equals((size_t)1, messages_empty.size());
+        assert_equals(std::string("user"), messages_empty[0].at("role").get<std::string>());
+        assert_equals(std::string(get_media_marker()), messages_empty[0].at("content").get<std::string>());
+    }
+}
+
 // Shared LFM2 parser cases - all variants use one output format and parser
 static void test_lfm2_parser(const std::string & template_path, bool detailed_debug) {
     auto tst = peg_tester(template_path, detailed_debug);
@@ -9531,6 +9593,7 @@ int main(int argc, char ** argv) {
         test_msg_token_delimiters_split();
         test_tools_oaicompat_json_conversion();
         test_convert_responses_to_chatcmpl();
+        test_convert_transcriptions_to_chatcmpl();
         test_developer_role_to_system_workaround();
         test_translate_gemma();
         test_deepseek_v4_thinking_retention();