Repository navigation
feat(tools): forward OpenAI TTS instructions field through text_to_speech
#14205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
0xAlcibiades
wants to merge
3
commits into
NousResearch:main
from
0xAlcibiades:feat/tts-openai-instructions
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| """Tests for the OpenAI TTS `instructions` field passthrough. | ||
|
|
||
| Covers #14196: forwarding the OpenAI-spec `instructions` parameter through the | ||
| `text_to_speech` tool so the agent can control tone/emotion/pacing on | ||
| gpt-4o-mini-tts and OpenAI-compatible voice-design servers. | ||
| """ | ||
|
|
||
| import json | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| import pytest | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def clean_env(monkeypatch): | ||
| for key in ("OPENAI_API_KEY", "HERMES_SESSION_PLATFORM"): | ||
| monkeypatch.delenv(key, raising=False) | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Backend-level passthrough (_generate_openai_tts) | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| class TestOpenaiBackendInstructions: | ||
| def _run(self, tmp_path, monkeypatch, *, tts_config=None, instructions=None): | ||
| monkeypatch.setenv("OPENAI_API_KEY", "test-key") | ||
| mock_client = MagicMock() | ||
| mock_client.audio.speech.create.return_value = MagicMock() | ||
| mock_cls = MagicMock(return_value=mock_client) | ||
|
|
||
| with patch("tools.tts_tool._import_openai_client", return_value=mock_cls), \ | ||
| patch("tools.tts_tool._resolve_openai_audio_client_config", | ||
| return_value=("test-key", None)): | ||
| from tools.tts_tool import _generate_openai_tts | ||
| kwargs = {} | ||
| if instructions is not None: | ||
| kwargs["instructions"] = instructions | ||
| _generate_openai_tts( | ||
| "Hello", str(tmp_path / "out.mp3"), tts_config or {}, **kwargs | ||
| ) | ||
| return mock_client.audio.speech.create | ||
|
|
||
| def test_instructions_forwarded_when_provided(self, tmp_path, monkeypatch): | ||
| """Tool arg `instructions` is passed to audio.speech.create as-is.""" | ||
| create = self._run(tmp_path, monkeypatch, instructions="Speak cheerfully.") | ||
| assert create.call_args[1]["instructions"] == "Speak cheerfully." | ||
|
|
||
| def test_instructions_absent_by_default(self, tmp_path, monkeypatch): | ||
| """No instructions arg -> key not present in create kwargs. | ||
|
|
||
| Preserves behavior on `tts-1`/`tts-1-hd` and strict servers that | ||
| reject unknown kwargs. | ||
| """ | ||
| create = self._run(tmp_path, monkeypatch) | ||
| assert "instructions" not in create.call_args[1] | ||
|
|
||
| def test_empty_string_instructions_omitted(self, tmp_path, monkeypatch): | ||
| """Empty string is treated as absent (not forwarded).""" | ||
| create = self._run(tmp_path, monkeypatch, instructions="") | ||
| assert "instructions" not in create.call_args[1] | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Tool-level plumbing (text_to_speech_tool -> _generate_openai_tts) | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| class TestToolLevelInstructions: | ||
| def _invoke_tool(self, tmp_path, monkeypatch, *, instructions=None): | ||
| monkeypatch.setenv("OPENAI_API_KEY", "test-key") | ||
|
|
||
| mock_client = MagicMock() | ||
|
|
||
| def fake_stream(path): | ||
| # Mimic OpenAI SDK's stream_to_file by writing a tiny payload. | ||
| with open(path, "wb") as f: | ||
| f.write(b"ID3\x03\x00\x00\x00\x00\x00\x00") | ||
|
|
||
| response = MagicMock() | ||
| response.stream_to_file.side_effect = fake_stream | ||
| mock_client.audio.speech.create.return_value = response | ||
|
|
||
| mock_cls = MagicMock(return_value=mock_client) | ||
|
|
||
| with patch("tools.tts_tool._import_openai_client", return_value=mock_cls), \ | ||
| patch("tools.tts_tool._resolve_openai_audio_client_config", | ||
| return_value=("test-key", None)), \ | ||
| patch("tools.tts_tool._load_tts_config", | ||
| return_value={"provider": "openai"}): | ||
| from tools.tts_tool import text_to_speech_tool | ||
| kwargs = {"output_path": str(tmp_path / "out.mp3")} | ||
| if instructions is not None: | ||
| kwargs["instructions"] = instructions | ||
| result = text_to_speech_tool("Hello world", **kwargs) | ||
| return mock_client.audio.speech.create, json.loads(result) | ||
|
|
||
| def test_tool_threads_instructions_to_openai_create( | ||
| self, tmp_path, monkeypatch | ||
| ): | ||
| create, result = self._invoke_tool( | ||
| tmp_path, monkeypatch, instructions="Whisper conspiratorially." | ||
| ) | ||
| assert result.get("success") is True | ||
| assert create.call_args[1]["instructions"] == "Whisper conspiratorially." | ||
|
|
||
| def test_tool_omits_instructions_when_not_supplied( | ||
| self, tmp_path, monkeypatch | ||
| ): | ||
| create, result = self._invoke_tool(tmp_path, monkeypatch) | ||
| assert result.get("success") is True | ||
| assert "instructions" not in create.call_args[1] | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Schema | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| class TestSchema: | ||
| def test_schema_exposes_instructions_parameter(self): | ||
| from tools.tts_tool import TTS_SCHEMA | ||
| props = TTS_SCHEMA["parameters"]["properties"] | ||
| assert "instructions" in props | ||
| assert props["instructions"]["type"] == "string" | ||
| # Must stay optional — current behavior must be preserved. | ||
| assert "instructions" not in TTS_SCHEMA["parameters"].get("required", []) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Current main now unpacks three values from this helper (
tools/tts_tool.py:1027, commitb53ba0e188363800447b7aca4c0a85420c39d1a8). Return("test-key", None, False)here and in the second helper so these tests do not fail during setup.