Skip to content

Open-source speech recognition toolkit for training, inference, streaming ASR, VAD, punctuation, speaker diarization pipelines, and OpenAI-compatible/MCP serving.

fix: reject incomplete token timestamp alignment

zhifu gao12916bfa96b7
diff --git a/funasr/utils/postprocess_utils.py b/funasr/utils/postprocess_utils.py
index 9774300ac..6a3cd2336 100644
--- a/funasr/utils/postprocess_utils.py
+++ b/funasr/utils/postprocess_utils.py
@@ -189,8 +189,11 @@ def sentence_postprocess(words: List[Any], time_stamp: List[List] = None):
middle_lists.append(word)
kept_indices.append(index)
- # time_stamp has one entry per token, including the ones washed out above
- if time_stamp is not None and max(kept_indices, default=-1) < len(time_stamp):
+ # Timestamps use original token indices. Only trailing filtered tokens
+ # may be missing timestamps; guessing alignment would silently shift words.
+ if time_stamp is not None:
+ if max(kept_indices, default=-1) >= len(time_stamp):
+ raise ValueError("time_stamp is missing a retained token timestamp")
time_stamp = [time_stamp[index] for index in kept_indices]
# all chinese characters
diff --git a/runtime/python/libtorch/funasr_torch/utils/postprocess_utils.py b/runtime/python/libtorch/funasr_torch/utils/postprocess_utils.py
index 0390c5da8..3f8d9999e 100644
--- a/runtime/python/libtorch/funasr_torch/utils/postprocess_utils.py
+++ b/runtime/python/libtorch/funasr_torch/utils/postprocess_utils.py
@@ -155,8 +155,11 @@ def sentence_postprocess(words: List[Any], time_stamp: List[List] = None):
middle_lists.append(word)
kept_indices.append(index)
- # time_stamp has one entry per token, including the ones washed out above
- if time_stamp is not None and max(kept_indices, default=-1) < len(time_stamp):
+ # Timestamps use original token indices. Only trailing filtered tokens
+ # may be missing timestamps; guessing alignment would silently shift words.
+ if time_stamp is not None:
+ if max(kept_indices, default=-1) >= len(time_stamp):
+ raise ValueError("time_stamp is missing a retained token timestamp")
time_stamp = [time_stamp[index] for index in kept_indices]
# all chinese characters
diff --git a/runtime/python/onnxruntime/funasr_onnx/utils/postprocess_utils.py b/runtime/python/onnxruntime/funasr_onnx/utils/postprocess_utils.py
index 7158aba60..343ed383a 100644
--- a/runtime/python/onnxruntime/funasr_onnx/utils/postprocess_utils.py
+++ b/runtime/python/onnxruntime/funasr_onnx/utils/postprocess_utils.py
@@ -157,8 +157,11 @@ def sentence_postprocess(words: List[Any], time_stamp: List[List] = None):
middle_lists.append(word)
kept_indices.append(index)
- # time_stamp has one entry per token, including the ones washed out above
- if time_stamp is not None and max(kept_indices, default=-1) < len(time_stamp):
+ # Timestamps use original token indices. Only trailing filtered tokens
+ # may be missing timestamps; guessing alignment would silently shift words.
+ if time_stamp is not None:
+ if max(kept_indices, default=-1) >= len(time_stamp):
+ raise ValueError("time_stamp is missing a retained token timestamp")
time_stamp = [time_stamp[index] for index in kept_indices]
# all chinese characters
diff --git a/tests/test_sentence_postprocess_timestamps.py b/tests/test_sentence_postprocess_timestamps.py
index 5ad8d775c..0aa6965c7 100644
--- a/tests/test_sentence_postprocess_timestamps.py
+++ b/tests/test_sentence_postprocess_timestamps.py
@@ -60,7 +60,7 @@ class SentencePostprocessTimestampTest(unittest.TestCase):
self.assertEqual(words, ["你", "好"])
self.assertEqual(timestamps, [[0, 100], [100, 200]])
- def test_too_few_timestamps_do_not_crash_when_a_token_is_filtered(self):
+ def test_missing_retained_token_timestamp_is_rejected(self):
cases = (
(["你", "<unk>", "好", "吗"], ["你", "好", "吗"]),
(["hello", "<unk>", "there", "big", "world"], ["hello", "there", "big", "world"]),
@@ -70,11 +70,31 @@ class SentencePostprocessTimestampTest(unittest.TestCase):
for tokens, expected_words in cases:
with self.subTest(path=path, tokens=tokens):
timestamps = [[i * 100, (i + 1) * 100] for i in range(len(tokens) - 1)]
- _, result_timestamps, words = _load_sentence_postprocess(path)(
- tokens, timestamps
- )
- self.assertEqual(words, expected_words)
- self.assertEqual(len(result_timestamps), len(expected_words))
+ with self.assertRaisesRegex(ValueError, "missing a retained token"):
+ _load_sentence_postprocess(path)(tokens, timestamps)
+
+ def test_missing_timestamp_without_filtered_tokens_is_rejected(self):
+ for path in POSTPROCESS_UTILS:
+ for tokens, timestamps in ((["你"], []), (["你", "好"], [[0, 100]])):
+ with self.subTest(path=path, tokens=tokens):
+ with self.assertRaisesRegex(ValueError, "missing a retained token"):
+ _load_sentence_postprocess(path)(tokens, timestamps)
+
+ def test_filtered_only_tokens_need_no_timestamps(self):
+ for path in POSTPROCESS_UTILS:
+ with self.subTest(path=path):
+ self.assertEqual(
+ _load_sentence_postprocess(path)(["<s>", "<unk>", "</s>"], []),
+ ("", [], []),
+ )
+
+ def test_untimed_transcription_still_accepts_filtered_tokens(self):
+ for path in POSTPROCESS_UTILS:
+ with self.subTest(path=path):
+ self.assertEqual(
+ _load_sentence_postprocess(path)(["你", "<unk>", "好"]),
+ ("你好", ["你", "好"]),
+ )
if __name__ == "__main__":