Skip to content

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

fix(tts): label audio with the effective response format (#2980)

little-spoon77830036dc56db
diff --git a/src/agentscope/tts/_openai/_model.py b/src/agentscope/tts/_openai/_model.py
index 2b45bf983..73cd59088 100644
--- a/src/agentscope/tts/_openai/_model.py
+++ b/src/agentscope/tts/_openai/_model.py
@@ -150,11 +150,6 @@ class OpenAITTSModel(TTSModelBase):
if not text:
return TTSResponse(content=None)
- media_type = _MEDIA_TYPES.get(
- self.parameters.response_format,
- _MEDIA_TYPES[_DEFAULT_RESPONSE_FORMAT],
- )
-
request_kwargs: dict[str, Any] = {
"model": self.model,
"voice": self.parameters.voice,
@@ -165,6 +160,11 @@ class OpenAITTSModel(TTSModelBase):
if self.parameters.instructions:
request_kwargs["instructions"] = self.parameters.instructions
+ media_type = _MEDIA_TYPES.get(
+ request_kwargs["response_format"],
+ _MEDIA_TYPES[_DEFAULT_RESPONSE_FORMAT],
+ )
+
if self.stream:
return self._stream(self.client, media_type, **request_kwargs)
diff --git a/tests/tts_openai_test.py b/tests/tts_openai_test.py
index 85fc60c73..176c93122 100644
--- a/tests/tts_openai_test.py
+++ b/tests/tts_openai_test.py
@@ -116,6 +116,60 @@ class TestOpenAITTSModel(IsolatedAsyncioTestCase):
self.assertEqual(result.content.source.media_type, _MEDIA_TYPE_WAV)
+ async def test_per_call_format_controls_media_type(self) -> None:
+ """Both output modes label audio using the effective API format."""
+ for stream in (False, True):
+ model = self._make_model(stream=stream)
+ client = _make_mock_client(b"AAAABBBB", [b"AAAA", b"BBBB"])
+ model.client = client
+ create = (
+ client.audio.speech.with_streaming_response.create
+ if stream
+ else client.audio.speech.create
+ )
+ for response_format, media_type in (
+ ("wav", "audio/wav"),
+ ("pcm", "audio/pcm"),
+ ("opus", "audio/opus"),
+ ):
+ with self.subTest(stream=stream, format=response_format):
+ result = await model.synthesize(
+ "Hello",
+ response_format=response_format,
+ )
+ responses = (
+ [chunk async for chunk in result]
+ if stream
+ else [result]
+ )
+ self.assertEqual(
+ create.call_args.kwargs["response_format"],
+ response_format,
+ )
+ self.assertEqual(
+ [r.content.source.media_type for r in responses],
+ [media_type] * len(responses),
+ )
+ self.assertEqual(
+ b"".join(
+ base64.b64decode(r.content.source.data)
+ for r in responses
+ ),
+ b"AAAABBBB",
+ )
+ self.assertEqual(model.parameters.response_format, "mp3")
+
+ # A one-off format override must not change the next call.
+ result = await model.synthesize("Next call")
+ responses = (
+ [chunk async for chunk in result] if stream else [result]
+ )
+ self.assertEqual(create.call_args.kwargs["response_format"], "mp3")
+ self.assertEqual(
+ [r.content.source.media_type for r in responses],
+ [_MEDIA_TYPE_MP3] * len(responses),
+ )
+
async def test_incremental_chunks(self) -> None:
"""Each streamed byte chunk yields one TTSResponse."""
client = _make_mock_client(b"", [b"AAAA", b"BBBB", b"CCCC"])