From 76d0936197c683408caf602ecc839f36998603ac Mon Sep 17 00:00:00 2001 From: luk384090-cloud Date: Thu, 12 Nov 2026 10:56:07 +0800 Subject: [PATCH] fix(tts): prevent KeyError for non-timestamp messages in speech parts - Add check to verify part.output is a dict with "sentence" key before access - Skip non-timestamp messages safely in SSML/background music scenarios - Avoids unintended KeyError exceptions during speech synthesis processing --- dashscope/legacy_1x/audio/tts/speech_synthesizer.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/dashscope/legacy_1x/audio/tts/speech_synthesizer.py b/dashscope/legacy_1x/audio/tts/speech_synthesizer.py index c3328681..5597c224 100644 --- a/dashscope/legacy_1x/audio/tts/speech_synthesizer.py +++ b/dashscope/legacy_1x/audio/tts/speech_synthesizer.py @@ -180,6 +180,14 @@ def call( # pylint: disable=R1702,too-many-branches # type: ignore[override] # part, ) else: + if ( + not isinstance(part.output, dict) + or "sentence" not in part.output + ): + # Non-timestamp message (e.g. extra messages returned + # in the SSML/bgm scenario). Skip it safely instead of + # raising KeyError on part.output["sentence"]. + continue if _callback is not None: sentence = SpeechSynthesisResult( None, # type: ignore[arg-type]