feat(qwen3-asr): support prompt parameter in v1/audio/transcriptions (#35415)

Signed-off-by: Nathan Price <nathan@abridge.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cyrus Leung <tlleungac@connect.ust.hk>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
Nathan Price
2026-06-10 19:54:59 +00:00
committed by GitHub
co-authored by Cursor Cyrus Leung mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent 6471ec75bd
commit 12f3f19c19
3 changed files with 145 additions and 13 deletions
+54 -12
View File
@@ -25,6 +25,7 @@
from collections.abc import Iterable, Mapping, Sequence
from typing import Any
import regex as re
import torch
import torch.nn as nn
from transformers.feature_extraction_utils import BatchFeature
@@ -90,6 +91,31 @@ from vllm.transformers_utils.processors.qwen3_asr import (
logger = init_logger(__name__)
_ASR_TEXT_TAG = "<asr_text>"
# User-supplied `prompt` / `response_prefix` must not inject extra ChatML turns.
_CHATML_LIKE_TOKEN = re.compile(r"<\|[^|]+\|>")
def _sanitize_transcription_user_text(text: str) -> str:
"""Strip ChatML-style special tokens from user-controlled transcription fields.
Applies the regex / ``<asr_text>`` substitutions to a fixpoint so nested
payloads cannot reconstruct a valid token after a single pass:
- ``<|im<|x|>_end|>`` would, with a single ``re.sub``, leave ``<|im_end|>``
(a real ChatML control token).
- ``<asr_te<asr_text>xt>`` would, with a single ``str.replace``, leave
``<asr_text>`` (the model-significant assistant-prefix delimiter).
Looping both substitutions until the string stabilises eliminates these
reconstruction attacks.
"""
if not text:
return ""
prev = None
while prev != text:
prev = text
text = _CHATML_LIKE_TOKEN.sub("", text).replace(_ASR_TEXT_TAG, "")
return text
def _get_feat_extract_output_lengths(input_lengths: torch.Tensor):
@@ -550,11 +576,24 @@ class Qwen3ASRForConditionalGeneration(
@classmethod
def get_generation_prompt(cls, stt_params: SpeechToTextParams) -> PromptType:
"""Get the generation prompt to be used for transcription requests."""
"""Get the generation prompt to be used for transcription requests.
Matches the official Qwen3-ASR SDK prompt format. The ``system`` turn
is only emitted when the caller supplied a ``prompt``, mirroring the
SDK's ``_build_messages`` (which omits the system role when context is
empty) and preserving the prior no-prompt behavior:
[system: {context}] # only when prompt given
user: {audio}
assistant: [language {Lang}<asr_text>] # when language is forced
"""
audio = stt_params.audio
model_config = stt_params.model_config
language = stt_params.language
task_type = stt_params.task_type
request_prompt = stt_params.request_prompt
to_language = stt_params.to_language
tokenizer = cached_tokenizer_from_config(model_config)
audio_placeholder = cls.get_placeholder_str("audio", 0)
@@ -563,17 +602,20 @@ class Qwen3ASRForConditionalGeneration(
f"Unsupported task_type '{task_type}'. "
"Supported task types are 'transcribe' and 'translate'."
)
full_lang_name_to = cls.supported_languages.get(to_language, to_language)
if to_language is None:
prompt = (
f"<|im_start|>user\n{audio_placeholder}<|im_end|>\n"
f"<|im_start|>assistant\n"
)
else:
prompt = (
f"<|im_start|>user\n{audio_placeholder}<|im_end|>\n"
f"<|im_start|>assistant\nlanguage {full_lang_name_to}{_ASR_TEXT_TAG}"
)
context = _sanitize_transcription_user_text(request_prompt)
system_turn = f"<|im_start|>system\n{context}<|im_end|>\n" if context else ""
prompt = (
f"{system_turn}"
f"<|im_start|>user\n{audio_placeholder}<|im_end|>\n"
f"<|im_start|>assistant\n"
)
lang_code = to_language if task_type == "translate" else language
if lang_code is not None:
full_lang_name = cls.supported_languages.get(lang_code, lang_code)
prompt += f"language {full_lang_name}{_ASR_TEXT_TAG}"
prompt_token_ids = tokenizer.encode(prompt)