diff --git a/litellm/llms/openrouter/text_to_speech/__init__.py b/litellm/llms/openrouter/text_to_speech/__init__.py new file mode 100644 index 00000000000..733bdd95c7d --- /dev/null +++ b/litellm/llms/openrouter/text_to_speech/__init__.py @@ -0,0 +1,5 @@ +from typing import Final + +from .transformation import OpenrouterTextToSpeechConfig + +__all__: Final = ("OpenrouterTextToSpeechConfig",) diff --git a/litellm/llms/openrouter/text_to_speech/transformation.py b/litellm/llms/openrouter/text_to_speech/transformation.py new file mode 100644 index 00000000000..0d5055896e7 --- /dev/null +++ b/litellm/llms/openrouter/text_to_speech/transformation.py @@ -0,0 +1,113 @@ +from typing import TYPE_CHECKING, Any, Final +from urllib.parse import urlparse + +import httpx + +import litellm +from litellm.llms.base_llm.text_to_speech.transformation import ( + BaseTextToSpeechConfig, + TextToSpeechRequestData, +) +from litellm.secret_managers.main import get_secret_str + +if TYPE_CHECKING: + from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj + from litellm.types.llms.openai import HttpxBinaryResponseContent +else: + LiteLLMLoggingObj: Final = Any + HttpxBinaryResponseContent: Final = Any + + +class OpenrouterTextToSpeechConfig(BaseTextToSpeechConfig): + DEFAULT_BASE_URL: Final = "https://openrouter.ai/api/v1" + + def get_supported_openai_params(self, model: str) -> list[str]: # mutable-ok: override abstract method signature + return ["voice", "response_format", "speed", "instructions"] # mutable-ok: list required by base signature + + def map_openai_params( + self, + model: str, + optional_params: dict, # mutable-ok: override abstract method signature + voice: str | dict | None = None, # mutable-ok: override abstract method signature + drop_params: bool = False, + kwargs: dict | None = None, # mutable-ok: override abstract method signature + ) -> tuple[str | None, dict]: # mutable-ok: override abstract method signature + mapped_voice: Final[str | None] = str(voice) if voice is not None else None + return mapped_voice, optional_params + + def validate_environment( + self, + headers: dict, # mutable-ok: override abstract method signature + model: str, + api_key: str | None = None, + api_base: str | None = None, + ) -> dict: # mutable-ok: override abstract method signature + return headers + + def is_trusted_api_base(self, api_base: str | None) -> bool: + if not api_base: + return True + configured_base: Final = get_secret_str("OPENROUTER_API_BASE") or litellm.api_base or self.DEFAULT_BASE_URL + if api_base.rstrip("/") == configured_base.rstrip("/"): + return True + parsed: Final = urlparse(api_base) + return parsed.netloc == "openrouter.ai" or parsed.netloc.endswith(".openrouter.ai") + + def resolve_api_base_and_key( + self, + api_base: str | None = None, + api_key: str | None = None, + dynamic_api_key: str | None = None, + ) -> tuple[str, str | None]: + resolved_base: Final = ( + api_base or litellm.api_base or get_secret_str("OPENROUTER_API_BASE") or self.DEFAULT_BASE_URL + ) + if api_key: + return resolved_base, api_key + if dynamic_api_key: + return resolved_base, dynamic_api_key + if self.is_trusted_api_base(api_base): + server_key: Final = ( + litellm.api_key + or litellm.openrouter_key + or get_secret_str("OPENROUTER_API_KEY") + or get_secret_str("OR_API_KEY") + ) + return resolved_base, server_key + return resolved_base, None + + def get_complete_url( + self, + model: str, + api_base: str | None, + litellm_params: dict, # mutable-ok: override abstract method signature + ) -> str: + base: Final = api_base or litellm.api_base or get_secret_str("OPENROUTER_API_BASE") or self.DEFAULT_BASE_URL + return f"{base.rstrip('/')}/audio/speech" + + def transform_text_to_speech_request( + self, + model: str, + input: str, + voice: str | None, + optional_params: dict, # mutable-ok: override abstract method signature + litellm_params: dict, # mutable-ok: override abstract method signature + headers: dict, # mutable-ok: override abstract method signature + ) -> TextToSpeechRequestData: + payload: Final[dict[str, object]] = { # mutable-ok: request body dict + "model": model, + "input": input, + "voice": voice, + **optional_params, + } + return TextToSpeechRequestData(dict_body=payload, headers=headers) + + def transform_text_to_speech_response( + self, + model: str, + raw_response: httpx.Response, + logging_obj: LiteLLMLoggingObj, + ) -> "HttpxBinaryResponseContent": + from litellm.types.llms.openai import HttpxBinaryResponseContent + + return HttpxBinaryResponseContent(response=raw_response) diff --git a/litellm/main.py b/litellm/main.py index 13d366d97f3..7daa299d2e0 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -8133,17 +8133,15 @@ def speech( model=model, llm_provider=custom_llm_provider, ) - if custom_llm_provider == "openrouter": - api_base = ( # rebind-ok: resolve openrouter speech api_base - api_base or litellm.api_base or get_secret_str("OPENROUTER_API_BASE") or "https://openrouter.ai/api/v1" - ) - api_key = ( # rebind-ok: resolve openrouter speech api_key - api_key - or dynamic_api_key - or litellm.api_key - or litellm.openrouter_key - or get_secret_str("OPENROUTER_API_KEY") - or get_secret_str("OR_API_KEY") + from litellm.llms.openrouter.text_to_speech.transformation import OpenrouterTextToSpeechConfig + + if isinstance(text_to_speech_provider_config, OpenrouterTextToSpeechConfig): + api_base, api_key = ( + text_to_speech_provider_config.resolve_api_base_and_key( # rebind-ok: resolve openrouter speech credentials via provider config + api_base=api_base, + api_key=api_key, + dynamic_api_key=dynamic_api_key, + ) ) else: api_base = ( diff --git a/litellm/utils.py b/litellm/utils.py index 252bc691301..34840528f6e 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -9738,6 +9738,12 @@ class ProviderConfigManager: ) return AWSPollyTextToSpeechConfig() + elif litellm.LlmProviders.OPENROUTER == provider: + from litellm.llms.openrouter.text_to_speech.transformation import ( + OpenrouterTextToSpeechConfig, + ) + + return OpenrouterTextToSpeechConfig() return None @staticmethod diff --git a/tests/audio_tests/test_audio_speech.py b/tests/audio_tests/test_audio_speech.py index 4ba6daabd71..6bf2feb1d43 100644 --- a/tests/audio_tests/test_audio_speech.py +++ b/tests/audio_tests/test_audio_speech.py @@ -754,6 +754,7 @@ def test_openrouter_speech_custom_api_base_and_key(): @pytest.mark.asyncio async def test_openrouter_aspeech_routing(monkeypatch: pytest.MonkeyPatch): + monkeypatch.delenv("OPENROUTER_API_KEY", raising=False) monkeypatch.setenv("OR_API_KEY", "sk-or-alt-key") with patch( "litellm.main.openai_chat_completions.audio_speech" @@ -782,3 +783,29 @@ async def test_openrouter_aspeech_routing(monkeypatch: pytest.MonkeyPatch): assert kwargs["api_key"] == "sk-or-alt-key" assert kwargs["aspeech"] is True + +def test_openrouter_speech_custom_api_base_without_key_does_not_leak_server_key( + monkeypatch: pytest.MonkeyPatch, +): + monkeypatch.setenv("OPENROUTER_API_KEY", "sk-or-secret-server-key") + with patch( + "litellm.main.openai_chat_completions.audio_speech" + ) as mock_audio_speech: + from litellm.types.llms.openai import HttpxBinaryResponseContent + + mock_audio_speech.return_value = MagicMock(spec=HttpxBinaryResponseContent) + + litellm.speech( + model="google/gemini-3.1-flash-tts-preview", + custom_llm_provider="openrouter", + voice="alloy", + input="custom config speech without key", + api_base="https://untrusted-external-proxy.com/v1", + ) + + mock_audio_speech.assert_called_once() + _, kwargs = mock_audio_speech.call_args + assert kwargs["model"] == "google/gemini-3.1-flash-tts-preview" + assert kwargs["api_base"] == "https://untrusted-external-proxy.com/v1" + assert kwargs["api_key"] is None +