Skip to content

Build and run agents you can see, understand and trust.

fix(tts): propagate DashScope synthesis errors (#2993)

little-spoon770ad28c3c748f
diff --git a/src/agentscope/tts/_dashscope/_model.py b/src/agentscope/tts/_dashscope/_model.py
index 8f63a60bb..758ea1975 100644
--- a/src/agentscope/tts/_dashscope/_model.py
+++ b/src/agentscope/tts/_dashscope/_model.py
@@ -150,6 +150,8 @@ class DashScopeTTSModel(TTSModelBase):
audio_bytes = bytearray()
usage = None
for chunk in response:
+ if chunk.status_code != 200:
+ raise RuntimeError(f"DashScope TTS API error: {chunk}")
if chunk.usage is not None:
usage = chunk.usage
if chunk.output is not None:
@@ -205,6 +207,8 @@ class DashScopeTTSModel(TTSModelBase):
chunk = next(it, _SENTINEL)
if chunk is _SENTINEL:
break
+ if chunk.status_code != 200:
+ raise RuntimeError(f"DashScope TTS API error: {chunk}")
if chunk.usage is not None:
usage = chunk.usage
if chunk.output is None:
diff --git a/tests/tts_dashscope_test.py b/tests/tts_dashscope_test.py
index 6c3fc2b47..39d27c2ac 100644
--- a/tests/tts_dashscope_test.py
+++ b/tests/tts_dashscope_test.py
@@ -54,6 +54,7 @@ def _make_api_chunk(
"""Build a chunk shaped like what dashscope.MultiModalConversation
yields. ``data_bytes=None`` represents a chunk with no output."""
chunk = MagicMock()
+ chunk.status_code = 200
chunk.usage = usage
if data_bytes is None:
chunk.output = None
@@ -202,6 +203,43 @@ class TestDashScopeTTSModel(IsolatedAsyncioTestCase):
# -- non-streaming --
+ async def test_api_errors_are_not_successful_audio(self) -> None:
+ """Provider errors must propagate, including after partial audio."""
+ from dashscope.api_entities.dashscope_response import (
+ MultiModalConversationResponse,
+ )
+
+ for stream in (False, True):
+ for status, code in ((401, "InvalidApiKey"), (429, "Throttling")):
+ for partial in (False, True):
+ with self.subTest(
+ stream=stream,
+ status=status,
+ partial=partial,
+ ):
+ error = MultiModalConversationResponse(
+ status_code=status,
+ code=code,
+ message="Request rejected",
+ request_id="test-request",
+ )
+ chunks = (
+ [
+ _make_api_chunk(b"AAAA"),
+ _make_api_chunk(b"BBBB"),
+ ]
+ if partial
+ else []
+ )
+ chunks.append(error)
+ self.mock_mmc.call.return_value = iter(chunks)
+ model = self._make_model(stream=stream)
+ with self.assertRaisesRegex(RuntimeError, code):
+ result = await model.synthesize("Hello")
+ if stream:
+ async for chunk in result:
+ self.assertFalse(chunk.is_last)
+
async def test_aggregates_chunks(self) -> None:
"""All API chunks are aggregated into one self-contained WAV."""
self.mock_mmc.call.return_value = _make_api_generator(