Skip to content

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

fix(tts): surface post-cold-start streaming_call failures instead of dropping the delta (#3621)

konluee7f38d5f56c3
diff --git a/src/agentscope/tts/_dashscope/_cosyvoice_model.py b/src/agentscope/tts/_dashscope/_cosyvoice_model.py
index 34a3c368a..c0f09aeb2 100644
--- a/src/agentscope/tts/_dashscope/_cosyvoice_model.py
+++ b/src/agentscope/tts/_dashscope/_cosyvoice_model.py
@@ -266,6 +266,7 @@ class DashScopeCosyVoiceTTSModel(TTSModelBase):
if not text:
return TTSResponse(content=None)
+ previous_text = self._accumulated_text
self._accumulated_text += text
if self._cold_start_done:
@@ -286,9 +287,14 @@ class DashScopeCosyVoiceTTSModel(TTSModelBase):
try:
self._synthesizer.streaming_call(text_to_send)
except Exception:
- # Send failed: keep the cold-start buffer and flag intact so
- # the next push retries it, mirroring the realtime model.
- return TTSResponse(content=None)
+ if not self._cold_start_done:
+ # Cold-start flush failed: keep the buffer and flag intact
+ # so the next push retries the whole buffered text.
+ return TTSResponse(content=None)
+ # This delta is not buffered. Restore the accumulated text
+ # before raising so a caller retry counts the delta only once.
+ self._accumulated_text = previous_text
+ raise
if not self._cold_start_done:
# The cold-start flush succeeded; only now clear the buffer
diff --git a/tests/tts_dashscope_test.py b/tests/tts_dashscope_test.py
index 83120939d..165d90ce0 100644
--- a/tests/tts_dashscope_test.py
+++ b/tests/tts_dashscope_test.py
@@ -21,6 +21,7 @@ import wave
from typing import Any, AsyncGenerator
from unittest import IsolatedAsyncioTestCase
from unittest.mock import AsyncMock, MagicMock, Mock, patch
+from unittest.mock import call as mock_call
import dashscope
import httpx
@@ -1302,8 +1303,62 @@ class TestDashScopeCosyVoiceRealtimeMode(
self.mock_synthesizer.streaming_call.call_count,
2,
)
- self.mock_synthesizer.streaming_call.assert_any_call("Hello")
- self.mock_synthesizer.streaming_call.assert_any_call(" world")
+
+ async def test_push_after_cold_start_retries_failed_delta_once(
+ self,
+ ) -> None:
+ """Caller retries must not duplicate text in a full-utterance retry."""
+ with patch.dict("sys.modules", self.mock_modules):
+ async with self._make_model(
+ cold_start_length=3,
+ max_retries=2,
+ stream=False,
+ ) as model:
+ model._callback.get_audio_response = Mock(
+ return_value=TTSResponse(content=None),
+ )
+ self.mock_synthesizer.streaming_call.side_effect = [
+ None,
+ RuntimeError("socket gone"),
+ None,
+ None,
+ ]
+ await model.push("Hello")
+
+ with self.assertRaisesRegex(RuntimeError, "socket gone"):
+ await model.push(" world")
+ self.assertEqual(
+ (
+ model._accumulated_text,
+ model._cold_start_buffer,
+ model._cold_start_done,
+ ),
+ ("Hello", "", True),
+ )
+
+ await model.push(" world")
+ self.assertEqual(model._accumulated_text, "Hello world")
+
+ def complete() -> None:
+ # Only the reconnected synthesis produces audio.
+ if (
+ self.mock_synthesizer.streaming_complete.call_count
+ == 2
+ ):
+ model._callback.on_data(b"\x00\x00")
+ model._callback.on_complete()
+
+ self.mock_synthesizer.streaming_complete.side_effect = complete
+ await model.synthesize()
+ self.assertEqual(
+ self.mock_synthesizer.streaming_call.call_args_list,
+ [
+ mock_call("Hello"),
+ mock_call(" world"),
+ mock_call(" world"),
+ mock_call("Hello world"),
+ ],
+ )
async def test_push_returns_audio_when_available(self) -> None:
"""push() returns audio data from the callback when available."""