Skip to content

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

fix(tts): respect per-call speech instructions (#2990)

little-spoon775569360b5706
diff --git a/src/agentscope/tts/_openai/_model.py b/src/agentscope/tts/_openai/_model.py
index 73cd59088..fcd4b407e 100644
--- a/src/agentscope/tts/_openai/_model.py
+++ b/src/agentscope/tts/_openai/_model.py
@@ -155,10 +155,10 @@ class OpenAITTSModel(TTSModelBase):
"voice": self.parameters.voice,
"input": text,
"response_format": self.parameters.response_format,
- **kwargs,
}
if self.parameters.instructions:
request_kwargs["instructions"] = self.parameters.instructions
+ request_kwargs.update(kwargs)
media_type = _MEDIA_TYPES.get(
request_kwargs["response_format"],
diff --git a/tests/tts_openai_test.py b/tests/tts_openai_test.py
index 176c93122..3f8c3993a 100644
--- a/tests/tts_openai_test.py
+++ b/tests/tts_openai_test.py
@@ -170,6 +170,57 @@ class TestOpenAITTSModel(IsolatedAsyncioTestCase):
[_MEDIA_TYPE_MP3] * len(responses),
)
+ async def test_per_call_instructions_override_default(self) -> None:
+ """Per-call instructions win without mutating the default."""
+ for stream in (False, True):
+ for default in ("Speak cheerfully", ""):
+ model = self._make_model(
+ stream=stream,
+ parameters=OpenAITTSModel.Parameters(
+ instructions=default,
+ ),
+ )
+ client = _make_mock_client(b"AAAA", [b"AAAA"])
+ model.client = client
+ create = (
+ client.audio.speech.with_streaming_response.create
+ if stream
+ else client.audio.speech.create
+ )
+ for override in ("Speak quietly", ""):
+ with self.subTest(
+ stream=stream,
+ default=default,
+ override=override,
+ ):
+ result = await model.synthesize(
+ "Hello",
+ instructions=override,
+ )
+ if stream:
+ async for _ in result:
+ pass
+ self.assertEqual(
+ create.call_args.kwargs["instructions"],
+ override,
+ )
+ self.assertEqual(
+ model.parameters.instructions,
+ default,
+ )
+
+ result = await model.synthesize("Next call")
+ if stream:
+ async for _ in result:
+ pass
+ if default:
+ self.assertEqual(
+ create.call_args.kwargs["instructions"],
+ default,
+ )
+ else:
+ self.assertNotIn("instructions", create.call_args.kwargs)
+
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"])