Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(tts): use first Gemini audio candidate (#3540)
Kiko03011663701233d3
diff --git a/src/agentscope/tts/_gemini/_model.py b/src/agentscope/tts/_gemini/_model.pyindex 658f7b202..9b74cd365 100644--- a/src/agentscope/tts/_gemini/_model.py+++ b/src/agentscope/tts/_gemini/_model.py@@ -204,7 +204,8 @@ class GeminiTTSModel(TTSModelBase):audio_bytes = bytearray()candidates = getattr(response, "candidates", None) or []- for candidate in candidates:+ # Candidates are alternative recordings, not consecutive audio parts.+ for candidate in candidates[:1]:content = getattr(candidate, "content", None)parts = getattr(content, "parts", None) or []for part in parts:@@ -272,7 +273,7 @@ class GeminiTTSModel(TTSModelBase):usage_metadata = chunk_usagecandidates = getattr(chunk, "candidates", None) or []- for candidate in candidates:+ for candidate in candidates[:1]:content = getattr(candidate, "content", None)parts = getattr(content, "parts", None) or []for part in parts:diff --git a/tests/tts_gemini_test.py b/tests/tts_gemini_test.pyindex 8762ed3ea..1cf614889 100644--- a/tests/tts_gemini_test.py+++ b/tests/tts_gemini_test.py@@ -10,7 +10,7 @@ Covers:import base64import ioimport wave-from typing import Any+from typing import Any, AsyncIteratorfrom unittest import IsolatedAsyncioTestCasefrom unittest.mock import AsyncMock, MagicMock, patch@@ -128,6 +128,45 @@ class TestGeminiTTSModel(IsolatedAsyncioTestCase):self.assertEqual(result.usage.input_tokens, 5)self.assertEqual(result.usage.output_tokens, 10)+ async def test_synthesizes_only_first_audio_candidate(self) -> None:+ """Alternative candidates must not be joined into one recording."""+ response = _make_api_response([b"AAAA"])+ response.candidates.extend(_make_api_response([b"BBBB"]).candidates)+ self.mock_client.aio.models.generate_content.return_value = response++ result = await self._make_model().synthesize("Hello world")++ wav_bytes = base64.b64decode(result.content.source.data)+ self.assertEqual(_parse_wav_payload(wav_bytes), b"AAAA")++ async def test_stream_uses_only_first_audio_candidate(self) -> None:+ """Streaming must not append an alternative candidate's audio."""+ first_chunk = _make_api_response([b"AAAA"])+ first_chunk.candidates.extend(+ _make_api_response([b"BBBB"]).candidates,+ )+ second_chunk = _make_api_response([b"CCCC"])+ second_chunk.candidates.extend(+ _make_api_response([b"DDDD"]).candidates,+ )++ async def chunks() -> AsyncIterator[Any]:+ yield first_chunk+ yield second_chunk++ parser = GeminiTTSModel._parse_stream_into_async_generator(+ chunks(),+ )+ results = [item async for item in parser]++ self.assertEqual(len(results), 2)+ self.assertFalse(results[0].is_last)+ self.assertTrue(results[1].is_last)+ wav_bytes = b"".join(+ base64.b64decode(result.content.source.data) for result in results+ )+ self.assertEqual(_parse_wav_payload(wav_bytes), b"AAAACCCC")+async def test_none_short_circuits(self) -> None:"""``synthesize(None)`` returns an empty response without touchingthe API."""