Skip to content

The agent that grows with you

fix(tts): streaming path also honors tts.openai.api_key from config

Teknium3356703d1f03
diff --git a/tests/tools/test_tts_streaming.py b/tests/tools/test_tts_streaming.py
index 962814642ad..c12e23e45ec 100644
--- a/tests/tools/test_tts_streaming.py
+++ b/tests/tools/test_tts_streaming.py
@@ -131,10 +131,14 @@ def test_elevenlabs_available_reflects_key(monkeypatch):
def test_openai_available_reflects_audio_key_resolution(monkeypatch):
+ monkeypatch.setattr(ts, "_openai_config_api_key", lambda: "")
monkeypatch.setattr(ts, "resolve_openai_audio_api_key", lambda: "voice-key")
assert ts.OpenAIStreamer.available() is True
monkeypatch.setattr(ts, "resolve_openai_audio_api_key", lambda: "")
assert ts.OpenAIStreamer.available() is False
+ # tts.openai.api_key from config.yaml counts too
+ monkeypatch.setattr(ts, "_openai_config_api_key", lambda: "cfg-key")
+ assert ts.OpenAIStreamer.available() is True
def test_openai_streamer_prefers_configured_base_url(monkeypatch):
@@ -188,6 +192,45 @@ def test_openai_streamer_prefers_configured_base_url(monkeypatch):
}
+def test_openai_streamer_prefers_configured_api_key(monkeypatch):
+ captured = {}
+
+ class _Response:
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *_args):
+ return False
+
+ def iter_bytes(self):
+ yield b"\x01\x00"
+
+ class _StreamingCreate:
+ @staticmethod
+ def create(**kwargs):
+ return _Response()
+
+ class _OpenAI:
+ def __init__(self, **kwargs):
+ captured["client"] = kwargs
+ self.audio = MagicMock()
+ self.audio.speech.with_streaming_response = _StreamingCreate()
+
+ monkeypatch.setattr(ts, "resolve_openai_audio_api_key", lambda: "env-key")
+ monkeypatch.setattr(ts, "get_env_value", lambda key, *args: None)
+ monkeypatch.setattr("openai.OpenAI", _OpenAI)
+
+ config = {
+ "provider": "openai",
+ "openai": {"api_key": "cfg-key", "base_url": "http://local-tts.example/v1"},
+ }
+ streamer = ts.resolve_streaming_provider(config)
+
+ assert streamer is not None
+ assert list(streamer.stream("Streaming test.")) == [b"\x01\x00"]
+ assert captured["client"]["api_key"] == "cfg-key"
+
+
# ── Dispatch: chunked streamer path ──────────────────────────────────────
diff --git a/tools/tts_streaming.py b/tools/tts_streaming.py
index 2f15c518a3e..7753a8ccf69 100644
--- a/tools/tts_streaming.py
+++ b/tools/tts_streaming.py
@@ -28,7 +28,7 @@ from abc import ABC, abstractmethod
from typing import Callable, Dict, Iterator, List, Optional
from tools.tool_backend_helpers import resolve_openai_audio_api_key
-from tools.tts_tool import _get_provider, get_env_value
+from tools.tts_tool import _get_provider, _load_tts_config, get_env_value
logger = logging.getLogger(__name__)
@@ -195,6 +195,15 @@ class ElevenLabsStreamer(StreamingTTSProvider):
)
+def _openai_config_api_key() -> str:
+ """Return ``tts.openai.api_key`` from config.yaml, or empty string."""
+ try:
+ openai_cfg = (_load_tts_config().get("openai") or {})
+ except Exception:
+ return ""
+ return openai_cfg.get("api_key") or ""
+
+
@register("openai")
class OpenAIStreamer(StreamingTTSProvider):
"""OpenAI speech with ``response_format=pcm`` (24 kHz mono int16)."""
@@ -203,13 +212,13 @@ class OpenAIStreamer(StreamingTTSProvider):
@staticmethod
def available() -> bool:
- return bool(resolve_openai_audio_api_key())
+ return bool(_openai_config_api_key() or resolve_openai_audio_api_key())
def stream(self, text: str) -> Iterator[bytes]:
from openai import OpenAI
client = OpenAI(
- api_key=resolve_openai_audio_api_key(),
+ api_key=(self.section.get("api_key") or resolve_openai_audio_api_key()),
base_url=(
self.section.get("base_url")
or get_env_value("OPENAI_BASE_URL")