From f801354c70e7c18d236c214e7bf79127f1fc75cd Mon Sep 17 00:00:00 2001 From: "heng.yin" Date: Thu, 27 Aug 2026 17:19:05 +0800 Subject: [PATCH 01/11] feat: add FlowSpeech text-to-speech provider --- litellm/llms/flowspeech/__init__.py | 1 + .../flowspeech/text_to_speech/__init__.py | 1 + .../text_to_speech/transformation.py | 207 ++++++++++++++++++ litellm/main.py | 34 +++ litellm/types/utils.py | 1 + litellm/utils.py | 6 + model_prices_and_context_window.json | 8 + .../test_litellm/llms/flowspeech/__init__.py | 1 + .../test_text_to_speech_transformation.py | 168 ++++++++++++++ 9 files changed, 427 insertions(+) create mode 100644 litellm/llms/flowspeech/__init__.py create mode 100644 litellm/llms/flowspeech/text_to_speech/__init__.py create mode 100644 litellm/llms/flowspeech/text_to_speech/transformation.py create mode 100644 tests/test_litellm/llms/flowspeech/__init__.py create mode 100644 tests/test_litellm/llms/flowspeech/test_text_to_speech_transformation.py diff --git a/litellm/llms/flowspeech/__init__.py b/litellm/llms/flowspeech/__init__.py new file mode 100644 index 00000000000..8b137891791 --- /dev/null +++ b/litellm/llms/flowspeech/__init__.py @@ -0,0 +1 @@ + diff --git a/litellm/llms/flowspeech/text_to_speech/__init__.py b/litellm/llms/flowspeech/text_to_speech/__init__.py new file mode 100644 index 00000000000..8b137891791 --- /dev/null +++ b/litellm/llms/flowspeech/text_to_speech/__init__.py @@ -0,0 +1 @@ + diff --git a/litellm/llms/flowspeech/text_to_speech/transformation.py b/litellm/llms/flowspeech/text_to_speech/transformation.py new file mode 100644 index 00000000000..5d3f9847017 --- /dev/null +++ b/litellm/llms/flowspeech/text_to_speech/transformation.py @@ -0,0 +1,207 @@ +import base64 +import binascii +from typing import TYPE_CHECKING, Final + +import httpx +from httpx import Headers +from pydantic import BaseModel + +import litellm +from litellm.llms.base_llm.chat.transformation import BaseLLMException +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 + + +class FlowSpeechException(BaseLLMException): + def __init__( + self, + status_code: int, + message: str, + headers: dict[str, str] | Headers | None = None, # mutable-ok: exception contract accepts dict headers + ) -> None: + super().__init__( # pyright: ignore[reportUnknownMemberType] # upstream exception keeps untyped dict fields + status_code=status_code, + message=message, + headers=headers, + ) + + +class FlowSpeechAudioData(BaseModel): + mimeType: str + audioBase64: str + + +class FlowSpeechResponse(BaseModel): + code: int + message: str | None = None + data: FlowSpeechAudioData | None = None + + +class FlowSpeechTextToSpeechConfig(BaseTextToSpeechConfig): + TTS_BASE_URL = "https://flowspeech.io" + TTS_ENDPOINT_PATH = "/api/ai/text-to-speech" + DEFAULT_VOICE = "Kore" + + def get_supported_openai_params(self, model: str) -> list[str]: # mutable-ok: base provider interface returns list + return ["voice", "instructions"] # mutable-ok: base provider interface returns list + + def map_openai_params( + self, + model: str, + optional_params: dict[str, object], # mutable-ok: base provider interface requires dict + voice: str | dict[str, object] | None = None, # mutable-ok: base provider interface accepts dict voices + drop_params: bool = False, + kwargs: dict[str, object] | None = None, # mutable-ok: base provider interface requires dict + ) -> tuple[str | None, dict[str, object]]: # mutable-ok: base provider interface returns dict params + params: Final[dict[str, object]] = ( # mutable-ok: isolated request copy + dict(optional_params) if optional_params else {} # mutable-ok: isolated request copy + ) + mapped_voice: Final = self._resolve_voice(voice) + instructions: Final = params.get("instructions") + mapped_params: Final[dict[str, object]] = ( # mutable-ok: base provider interface returns dict params + {"prompt": instructions} # mutable-ok: base provider interface returns dict params + if isinstance(instructions, str) and instructions.strip() + else {} # mutable-ok: base provider interface returns dict params + ) + return mapped_voice, mapped_params + + def _resolve_voice( + self, + voice: str | dict[str, object] | None, # mutable-ok: base provider interface accepts dict voices + ) -> str: + if isinstance(voice, str) and voice.strip(): + return voice.strip() + if isinstance(voice, dict): + candidate: Final = next( + ( + value + for key in ("voice_name", "voiceName", "id", "name") + if isinstance((value := voice.get(key)), str) and value.strip() + ), + None, + ) + if isinstance(candidate, str): + return candidate.strip() + return self.DEFAULT_VOICE + + def validate_environment( + self, + headers: dict[str, str], # mutable-ok: base provider interface requires dict headers + model: str, + api_key: str | None = None, + api_base: str | None = None, + ) -> dict[str, str]: # mutable-ok: base provider interface returns dict headers + resolved_api_key: Final = api_key or litellm.api_key or get_secret_str("FLOWSPEECH_API_KEY") + if resolved_api_key is None: + raise ValueError("FlowSpeech API key is required. Set FLOWSPEECH_API_KEY environment variable.") + return { # mutable-ok: base provider interface returns dict headers + **headers, + "Authorization": f"Bearer {resolved_api_key}", + "Content-Type": "application/json", + } + + def get_error_class( + self, + error_message: str, + status_code: int, + headers: dict[str, str] | Headers, # mutable-ok: exception contract accepts dict headers + ) -> BaseLLMException: + return FlowSpeechException(message=error_message, status_code=status_code, headers=headers) + + def transform_text_to_speech_request( + self, + model: str, + input: str, + voice: str | None, + optional_params: dict[str, object], # mutable-ok: base provider interface requires dict + litellm_params: dict[str, object], # mutable-ok: base provider interface requires dict + headers: dict[str, str], # mutable-ok: base provider interface requires dict + ) -> TextToSpeechRequestData: + prompt: Final = optional_params.get("prompt") + prompt_data: Final[dict[str, str]] = ( # mutable-ok: JSON request body requires a dict + {"prompt": prompt} # mutable-ok: JSON request body requires a dict + if isinstance(prompt, str) and prompt.strip() + else {} # mutable-ok: JSON request body requires a dict + ) + request_body: Final[dict[str, object]] = { # mutable-ok: JSON request body requires a dict + "text": input, + "originalText": input, + "speakers": [ # mutable-ok: FlowSpeech JSON schema requires a speakers array + {"voiceName": voice or self.DEFAULT_VOICE} # mutable-ok: FlowSpeech JSON schema requires an object + ], + **prompt_data, + } + return TextToSpeechRequestData( + dict_body=request_body, + headers={"Content-Type": "application/json"}, # mutable-ok: base request type requires dict 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 + + try: + payload: Final = FlowSpeechResponse.model_validate(raw_response.json()) + except ValueError as exc: + raise FlowSpeechException( + status_code=raw_response.status_code, + message="FlowSpeech API returned an invalid JSON response", + headers=raw_response.headers, + ) from exc + + if payload.code != 0 or payload.data is None or not payload.data.audioBase64: + raise FlowSpeechException( + status_code=raw_response.status_code, + message=payload.message or "FlowSpeech API response did not include audio data", + headers=raw_response.headers, + ) + + try: + audio_bytes: Final = base64.b64decode(payload.data.audioBase64, validate=True) + except (binascii.Error, ValueError) as exc: + raise FlowSpeechException( + status_code=raw_response.status_code, + message="FlowSpeech API returned invalid base64 audio data", + headers=raw_response.headers, + ) from exc + + preserved_headers: Final = { # mutable-ok: httpx.Response requires materialized headers + key: value + for key, value in raw_response.headers.items() + if key.lower() not in frozenset({"content-encoding", "content-length", "content-type", "transfer-encoding"}) + } + response_headers: Final = { # mutable-ok: httpx.Response requires materialized headers + **preserved_headers, + "content-length": str(len(audio_bytes)), + "content-type": payload.data.mimeType, + } + binary_response: Final = httpx.Response( + status_code=200, + headers=response_headers, + content=audio_bytes, + request=raw_response.request, + ) + return HttpxBinaryResponseContent(binary_response) + + def get_complete_url( + self, + model: str, + api_base: str | None, + litellm_params: dict[str, object], # mutable-ok: base provider interface requires dict + ) -> str: + base_url: Final = api_base or get_secret_str("FLOWSPEECH_API_BASE") or self.TTS_BASE_URL + normalized_base_url: Final = base_url.rstrip("/") + if normalized_base_url.endswith(self.TTS_ENDPOINT_PATH): + return normalized_base_url + return f"{normalized_base_url}{self.TTS_ENDPOINT_PATH}" diff --git a/litellm/main.py b/litellm/main.py index 8ee102f5d07..e9c1b9fa281 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -8225,6 +8225,40 @@ def speech( client=client, _is_async=aspeech or False, ) + elif custom_llm_provider == "flowspeech": + from litellm.llms.flowspeech.text_to_speech.transformation import ( + FlowSpeechTextToSpeechConfig, + ) + + if text_to_speech_provider_config is None: + text_to_speech_provider_config = ( # rebind-ok: provider config is resolved lazily for speech dispatch + FlowSpeechTextToSpeechConfig() + ) + + if not isinstance(text_to_speech_provider_config, FlowSpeechTextToSpeechConfig): + raise TypeError("FlowSpeech TTS configuration has an unexpected type") + flowspeech_config: Final = text_to_speech_provider_config + + if api_base is not None: + litellm_params_dict["api_base"] = api_base + if api_key is not None: + litellm_params_dict["api_key"] = api_key + + voice_name: Final = voice if isinstance(voice, str) else None + response = base_llm_http_handler.text_to_speech_handler( # rebind-ok: provider branch sets dispatch result + model=model, + input=input, + voice=voice_name, + text_to_speech_provider_config=flowspeech_config, + text_to_speech_optional_params=optional_params, + custom_llm_provider=custom_llm_provider, + litellm_params=litellm_params_dict, + logging_obj=logging_obj, + timeout=timeout, + extra_headers=extra_headers, + client=client, + _is_async=aspeech or False, + ) elif custom_llm_provider == "vertex_ai" or custom_llm_provider == "vertex_ai_beta": from litellm.llms.vertex_ai.text_to_speech.transformation import ( VertexAITextToSpeechConfig, diff --git a/litellm/types/utils.py b/litellm/types/utils.py index 58103b84749..092a405a180 100644 --- a/litellm/types/utils.py +++ b/litellm/types/utils.py @@ -3783,6 +3783,7 @@ class LlmProviders(str, Enum): INFINITY = "infinity" DEEPGRAM = "deepgram" ELEVENLABS = "elevenlabs" + FLOWSPEECH = "flowspeech" NOVITA = "novita" AIOHTTP_OPENAI = "aiohttp_openai" LANGFUSE = "langfuse" diff --git a/litellm/utils.py b/litellm/utils.py index 54f97ccae54..85da141dab1 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -9309,6 +9309,12 @@ class ProviderConfigManager: ) return ElevenLabsTextToSpeechConfig() + elif litellm.LlmProviders.FLOWSPEECH == provider: + from litellm.llms.flowspeech.text_to_speech.transformation import ( + FlowSpeechTextToSpeechConfig, + ) + + return FlowSpeechTextToSpeechConfig() elif litellm.LlmProviders.RUNWAYML == provider: from litellm.llms.runwayml.text_to_speech.transformation import ( RunwayMLTextToSpeechConfig, diff --git a/model_prices_and_context_window.json b/model_prices_and_context_window.json index dd367e875de..8a543eddcd4 100644 --- a/model_prices_and_context_window.json +++ b/model_prices_and_context_window.json @@ -17298,6 +17298,14 @@ "/v1/audio/transcriptions" ] }, + "flowspeech/flowspeech-tts": { + "litellm_provider": "flowspeech", + "mode": "audio_speech", + "source": "https://flowspeech.io/", + "supported_endpoints": [ + "/v1/audio/speech" + ] + }, "elevenlabs/eleven_v3": { "input_cost_per_character": 0.00018, "litellm_provider": "elevenlabs", diff --git a/tests/test_litellm/llms/flowspeech/__init__.py b/tests/test_litellm/llms/flowspeech/__init__.py new file mode 100644 index 00000000000..8b137891791 --- /dev/null +++ b/tests/test_litellm/llms/flowspeech/__init__.py @@ -0,0 +1 @@ + diff --git a/tests/test_litellm/llms/flowspeech/test_text_to_speech_transformation.py b/tests/test_litellm/llms/flowspeech/test_text_to_speech_transformation.py new file mode 100644 index 00000000000..903495a19d4 --- /dev/null +++ b/tests/test_litellm/llms/flowspeech/test_text_to_speech_transformation.py @@ -0,0 +1,168 @@ +import base64 +import json + +import httpx +import pytest + +import litellm +from litellm.llms.custom_httpx.http_handler import HTTPHandler +from litellm.llms.flowspeech.text_to_speech.transformation import ( + FlowSpeechException, + FlowSpeechTextToSpeechConfig, +) + + +def test_maps_voice_and_instructions_to_flowspeech_request(): + config = FlowSpeechTextToSpeechConfig() + voice, params = config.map_openai_params( + model="flowspeech-tts", + optional_params={"instructions": "Speak with calm confidence"}, + voice={"voiceName": "Aoede"}, + ) + + request = config.transform_text_to_speech_request( + model="flowspeech-tts", + input="Hello from FlowSpeech", + voice=voice, + optional_params=params, + litellm_params={}, + headers={}, + ) + + assert request["dict_body"] == { + "text": "Hello from FlowSpeech", + "originalText": "Hello from FlowSpeech", + "speakers": [{"voiceName": "Aoede"}], + "prompt": "Speak with calm confidence", + } + + +def test_uses_default_voice_and_ignores_unsupported_openai_params(): + config = FlowSpeechTextToSpeechConfig() + voice, params = config.map_openai_params( + model="flowspeech-tts", + optional_params={"response_format": "mp3", "speed": 1.5}, + ) + + assert voice == "Kore" + assert params == {} + + +def test_validates_bearer_api_key(): + config = FlowSpeechTextToSpeechConfig() + + headers = config.validate_environment({}, "flowspeech-tts", api_key="test-key") + + assert headers == { + "Authorization": "Bearer test-key", + "Content-Type": "application/json", + } + + +def test_requires_api_key(monkeypatch): + config = FlowSpeechTextToSpeechConfig() + monkeypatch.setattr(litellm, "api_key", None) + monkeypatch.delenv("FLOWSPEECH_API_KEY", raising=False) + + with pytest.raises(ValueError, match="FlowSpeech API key is required"): + config.validate_environment({}, "flowspeech-tts") + + +def test_decodes_flowspeech_audio_response(): + config = FlowSpeechTextToSpeechConfig() + audio = b"test audio bytes" + response = httpx.Response( + 200, + headers={"content-type": "application/json", "x-request-id": "request-1"}, + json={ + "code": 0, + "data": { + "mimeType": "audio/mpeg", + "audioBase64": base64.b64encode(audio).decode(), + }, + }, + request=httpx.Request("POST", "https://flowspeech.io/api/ai/text-to-speech"), + ) + + result = config.transform_text_to_speech_response("flowspeech-tts", response, logging_obj=None) + + assert result.content == audio + assert result.response.headers["content-type"] == "audio/mpeg" + assert result.response.headers["x-request-id"] == "request-1" + + +def test_rejects_api_error_response(): + config = FlowSpeechTextToSpeechConfig() + response = httpx.Response( + 400, + json={"code": 4001, "message": "Quota exceeded", "data": None}, + request=httpx.Request("POST", "https://flowspeech.io/api/ai/text-to-speech"), + ) + + with pytest.raises(FlowSpeechException, match="Quota exceeded"): + config.transform_text_to_speech_response("flowspeech-tts", response, logging_obj=None) + + +def test_builds_default_and_custom_urls(): + config = FlowSpeechTextToSpeechConfig() + + assert config.get_complete_url("flowspeech-tts", None, {}) == "https://flowspeech.io/api/ai/text-to-speech" + assert ( + config.get_complete_url( + "flowspeech-tts", + "https://example.com/api/ai/text-to-speech", + {}, + ) + == "https://example.com/api/ai/text-to-speech" + ) + + +def test_registers_flowspeech_provider_and_config(): + from litellm.utils import ProviderConfigManager + + model, provider, _, _ = litellm.get_llm_provider("flowspeech/flowspeech-tts") + config = ProviderConfigManager.get_provider_text_to_speech_config( + model=model, + provider=litellm.LlmProviders.FLOWSPEECH, + ) + + assert model == "flowspeech-tts" + assert provider == "flowspeech" + assert isinstance(config, FlowSpeechTextToSpeechConfig) + + +def test_speech_dispatches_to_flowspeech_handler(): + audio = b"generated audio" + + def respond(request: httpx.Request) -> httpx.Response: + assert request.url == "https://flowspeech.io/api/ai/text-to-speech" + assert request.headers["authorization"] == "Bearer test-key" + assert json.loads(request.content) == { + "text": "Hello", + "originalText": "Hello", + "speakers": [{"voiceName": "Aoede"}], + "prompt": "Speak warmly", + } + return httpx.Response( + 200, + json={ + "code": 0, + "data": { + "mimeType": "audio/mpeg", + "audioBase64": base64.b64encode(audio).decode(), + }, + }, + ) + + client = HTTPHandler(client=httpx.Client(transport=httpx.MockTransport(respond))) + result = litellm.speech( + model="flowspeech/flowspeech-tts", + input="Hello", + voice="Aoede", + instructions="Speak warmly", + api_key="test-key", + client=client, + ) + + assert result.content == audio + assert result.response.headers["content-type"] == "audio/mpeg" From 2c9a01f776d23a155fa8f2b7874ba40fb22eb200 Mon Sep 17 00:00:00 2001 From: waeckerlinfederowicz66-sketch <267864308+waeckerlinfederowicz66-sketch@users.noreply.github.com> Date: Mon, 14 Sep 2026 12:14:02 +0800 Subject: [PATCH 02/11] fix: register FlowSpeech speech endpoint support --- provider_endpoints_support.json | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/provider_endpoints_support.json b/provider_endpoints_support.json index 1d8d374c2c4..2bf1b2ec4a9 100644 --- a/provider_endpoints_support.json +++ b/provider_endpoints_support.json @@ -883,6 +883,24 @@ "interactions": true } }, + "flowspeech": { + "display_name": "FlowSpeech (`flowspeech`)", + "url": "https://docs.litellm.ai/docs/providers/flowspeech", + "endpoints": { + "chat_completions": false, + "messages": false, + "responses": false, + "embeddings": false, + "image_generations": false, + "audio_transcriptions": false, + "audio_speech": true, + "moderations": false, + "batches": false, + "rerank": false, + "a2a": false, + "interactions": false + } + }, "exa_ai": { "display_name": "Exa AI (`exa_ai`)", "url": "https://docs.litellm.ai/docs/search/exa_ai", From 1fa8e5d0248840c2662addefaa5ef12bae7413df Mon Sep 17 00:00:00 2001 From: waeckerlinfederowicz66-sketch <267864308+waeckerlinfederowicz66-sketch@users.noreply.github.com> Date: Mon, 14 Sep 2026 12:18:00 +0800 Subject: [PATCH 03/11] fix: stop advertising unsupported speech instructions --- .../text_to_speech/transformation.py | 20 ++----------------- .../test_text_to_speech_transformation.py | 6 ++---- 2 files changed, 4 insertions(+), 22 deletions(-) diff --git a/litellm/llms/flowspeech/text_to_speech/transformation.py b/litellm/llms/flowspeech/text_to_speech/transformation.py index 5d3f9847017..784f40cf4df 100644 --- a/litellm/llms/flowspeech/text_to_speech/transformation.py +++ b/litellm/llms/flowspeech/text_to_speech/transformation.py @@ -50,7 +50,7 @@ class FlowSpeechTextToSpeechConfig(BaseTextToSpeechConfig): DEFAULT_VOICE = "Kore" def get_supported_openai_params(self, model: str) -> list[str]: # mutable-ok: base provider interface returns list - return ["voice", "instructions"] # mutable-ok: base provider interface returns list + return ["voice"] # mutable-ok: base provider interface returns list def map_openai_params( self, @@ -60,17 +60,8 @@ class FlowSpeechTextToSpeechConfig(BaseTextToSpeechConfig): drop_params: bool = False, kwargs: dict[str, object] | None = None, # mutable-ok: base provider interface requires dict ) -> tuple[str | None, dict[str, object]]: # mutable-ok: base provider interface returns dict params - params: Final[dict[str, object]] = ( # mutable-ok: isolated request copy - dict(optional_params) if optional_params else {} # mutable-ok: isolated request copy - ) mapped_voice: Final = self._resolve_voice(voice) - instructions: Final = params.get("instructions") - mapped_params: Final[dict[str, object]] = ( # mutable-ok: base provider interface returns dict params - {"prompt": instructions} # mutable-ok: base provider interface returns dict params - if isinstance(instructions, str) and instructions.strip() - else {} # mutable-ok: base provider interface returns dict params - ) - return mapped_voice, mapped_params + return mapped_voice, {} # mutable-ok: base provider interface returns dict params def _resolve_voice( self, @@ -124,19 +115,12 @@ class FlowSpeechTextToSpeechConfig(BaseTextToSpeechConfig): litellm_params: dict[str, object], # mutable-ok: base provider interface requires dict headers: dict[str, str], # mutable-ok: base provider interface requires dict ) -> TextToSpeechRequestData: - prompt: Final = optional_params.get("prompt") - prompt_data: Final[dict[str, str]] = ( # mutable-ok: JSON request body requires a dict - {"prompt": prompt} # mutable-ok: JSON request body requires a dict - if isinstance(prompt, str) and prompt.strip() - else {} # mutable-ok: JSON request body requires a dict - ) request_body: Final[dict[str, object]] = { # mutable-ok: JSON request body requires a dict "text": input, "originalText": input, "speakers": [ # mutable-ok: FlowSpeech JSON schema requires a speakers array {"voiceName": voice or self.DEFAULT_VOICE} # mutable-ok: FlowSpeech JSON schema requires an object ], - **prompt_data, } return TextToSpeechRequestData( dict_body=request_body, diff --git a/tests/test_litellm/llms/flowspeech/test_text_to_speech_transformation.py b/tests/test_litellm/llms/flowspeech/test_text_to_speech_transformation.py index 903495a19d4..6b5653ef99b 100644 --- a/tests/test_litellm/llms/flowspeech/test_text_to_speech_transformation.py +++ b/tests/test_litellm/llms/flowspeech/test_text_to_speech_transformation.py @@ -12,7 +12,7 @@ from litellm.llms.flowspeech.text_to_speech.transformation import ( ) -def test_maps_voice_and_instructions_to_flowspeech_request(): +def test_maps_voice_without_unsupported_instructions(): config = FlowSpeechTextToSpeechConfig() voice, params = config.map_openai_params( model="flowspeech-tts", @@ -33,8 +33,8 @@ def test_maps_voice_and_instructions_to_flowspeech_request(): "text": "Hello from FlowSpeech", "originalText": "Hello from FlowSpeech", "speakers": [{"voiceName": "Aoede"}], - "prompt": "Speak with calm confidence", } + assert config.get_supported_openai_params("flowspeech-tts") == ["voice"] def test_uses_default_voice_and_ignores_unsupported_openai_params(): @@ -141,7 +141,6 @@ def test_speech_dispatches_to_flowspeech_handler(): "text": "Hello", "originalText": "Hello", "speakers": [{"voiceName": "Aoede"}], - "prompt": "Speak warmly", } return httpx.Response( 200, @@ -159,7 +158,6 @@ def test_speech_dispatches_to_flowspeech_handler(): model="flowspeech/flowspeech-tts", input="Hello", voice="Aoede", - instructions="Speak warmly", api_key="test-key", client=client, ) From 6e1f50dbf2286b67337e35ec94b0c19c36cbc35a Mon Sep 17 00:00:00 2001 From: waeckerlinfederowicz66-sketch <267864308+waeckerlinfederowicz66-sketch@users.noreply.github.com> Date: Mon, 14 Sep 2026 12:23:50 +0800 Subject: [PATCH 04/11] fix: synchronize FlowSpeech fallback model registry --- litellm/model_prices_and_context_window_backup.json | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/litellm/model_prices_and_context_window_backup.json b/litellm/model_prices_and_context_window_backup.json index dd367e875de..8a543eddcd4 100644 --- a/litellm/model_prices_and_context_window_backup.json +++ b/litellm/model_prices_and_context_window_backup.json @@ -17298,6 +17298,14 @@ "/v1/audio/transcriptions" ] }, + "flowspeech/flowspeech-tts": { + "litellm_provider": "flowspeech", + "mode": "audio_speech", + "source": "https://flowspeech.io/", + "supported_endpoints": [ + "/v1/audio/speech" + ] + }, "elevenlabs/eleven_v3": { "input_cost_per_character": 0.00018, "litellm_provider": "elevenlabs", From 76977b52ef3ba52662f278eeb9ae7d8e7292f700 Mon Sep 17 00:00:00 2001 From: "heng.yin" Date: Mon, 26 Oct 2026 16:21:24 +0800 Subject: [PATCH 05/11] fix(flowspeech): migrate speech provider to public API v1 --- .../llms/flowspeech/text_to_speech/handler.py | 372 +++++++++++++ .../text_to_speech/transformation.py | 293 +++++----- litellm/main.py | 58 +- litellm/proxy/proxy_server.py | 12 + .../llms/flowspeech/test_handler.py | 512 ++++++++++++++++++ .../test_text_to_speech_transformation.py | 349 +++++++----- .../proxy/proxy_server/test_routes_audio.py | 43 ++ 7 files changed, 1378 insertions(+), 261 deletions(-) create mode 100644 litellm/llms/flowspeech/text_to_speech/handler.py create mode 100644 tests/test_litellm/llms/flowspeech/test_handler.py diff --git a/litellm/llms/flowspeech/text_to_speech/handler.py b/litellm/llms/flowspeech/text_to_speech/handler.py new file mode 100644 index 00000000000..20d83c3d493 --- /dev/null +++ b/litellm/llms/flowspeech/text_to_speech/handler.py @@ -0,0 +1,372 @@ +import asyncio +import io +import json +import math +import struct +import time +import wave +from collections.abc import Awaitable, Callable, Generator, Mapping +from datetime import timezone +from email.utils import parsedate_to_datetime +from types import MappingProxyType +from typing import Final, TypeAlias + +import httpx + +from litellm.types.llms.openai import HttpxBinaryResponseContent + +from .transformation import ( + FlowSpeechErrorResponse, + FlowSpeechException, + FlowSpeechJob, + validate_url, + validate_uuid, +) + +Flow: TypeAlias = Generator[ + httpx.Request | float, httpx.Response | httpx.TransportError | None, HttpxBinaryResponseContent +] +Exchange: TypeAlias = Generator[httpx.Request | float, httpx.Response | httpx.TransportError | None, httpx.Response] + + +def validate_wav_structure(content: bytes) -> None: + if ( + len(content) < 12 + or content[:4] != b"RIFF" + or content[8:12] != b"WAVE" + or int.from_bytes(content[4:8], "little") != len(content) - 8 + ): + raise ValueError("Invalid RIFF container length") + stream: Final = io.BytesIO(content) + stream.seek(12) + for header in iter(lambda: stream.read(8), b""): + if len(header) != 8: + raise ValueError("Truncated WAV chunk header") + size = int.from_bytes(header[4:], "little") # rebind-ok: each RIFF chunk declares its own size + end = stream.tell() + size + size % 2 # rebind-ok: boundary belongs to the current chunk + if end > len(content): + raise ValueError("Truncated WAV chunk") + if header[:4] == b"fmt " and stream.read(min(size, 16))[2:] != struct.pack(" None: + seconds: Final = timeout.read if isinstance(timeout, httpx.Timeout) else timeout + if isinstance(seconds, bool) or seconds is None or not math.isfinite(seconds) or seconds <= 0: + raise ValueError("FlowSpeech timeout must be finite and positive (httpx.Timeout uses read as total)") + if isinstance(max_retries, bool) or not isinstance(max_retries, int) or not 0 <= max_retries <= 10: + raise ValueError("FlowSpeech max_retries must be an integer between 0 and 10") + self.url = str(validate_url(url)) + self.headers = httpx.Headers(headers) + self.request_id = validate_uuid(self.headers["Idempotency-Key"]) + self.job_id: str | None = None + serialized: Final = json.dumps( + dict(body), # mutable-ok: JSON encoder requires a concrete dict + ensure_ascii=False, + separators=(",", ":"), + ) + self.body = serialized.encode("utf-8") + if len(self.body) > self.MAX_JSON_BYTES: + raise ValueError("FlowSpeech request exceeds 1 MiB") + self.timeout = timeout + self.deadline = clock() + seconds + self.clock = clock + self.wall_clock = wall_clock + self.sleep = sleep + self.async_sleep = async_sleep + self.max_retries = max_retries + self.on_completed = on_completed + + def error(self, status: int, message: str, headers: httpx.Headers | None = None) -> FlowSpeechException: + return FlowSpeechException( + status, + f"{message} (request_id={self.request_id}, job_id={self.job_id or 'unknown'})", + headers, + ) + + def remaining(self) -> float: + remaining: Final = self.deadline - self.clock() + if remaining <= 0: + raise self.error(408, "FlowSpeech total deadline exceeded; recover using the same request_id") + return remaining + + def delay(self, response: httpx.Response | None = None) -> float: + value: Final = ( + response.headers["retry-after"] if response is not None and "retry-after" in response.headers else None + ) + if not value: + return self.POLL_INTERVAL + try: + seconds: Final = float(value) + except ValueError: + try: + date: Final = parsedate_to_datetime(value) + return max( + self.POLL_INTERVAL, date.replace(tzinfo=date.tzinfo or timezone.utc).timestamp() - self.wall_clock() + ) + except (ValueError, TypeError, OverflowError): + return self.POLL_INTERVAL + return max(self.POLL_INTERVAL, seconds) if math.isfinite(seconds) else self.POLL_INTERVAL + + def wait_budget(self, delay: float) -> None: + if delay >= self.remaining(): + raise self.error(408, "FlowSpeech total deadline cannot accommodate Retry-After/poll delay") + + def request(self, method: str, url: str, download: bool) -> httpx.Request: + remaining: Final = self.remaining() + timeouts: Final[Mapping[str, float | None]] = ( + self.timeout.as_dict() if isinstance(self.timeout, httpx.Timeout) else MappingProxyType({}) + ) + return httpx.Request( + method, + url, + headers=MappingProxyType({"Accept-Encoding": "identity"}) + if download + else MappingProxyType({**self.headers, "Accept-Encoding": "identity"}), + content=self.body if method == "POST" else None, + extensions=MappingProxyType( + { + "timeout": MappingProxyType( + { + phase: min(limit, remaining) if (limit := timeouts.get(phase)) is not None else remaining + for phase in ("connect", "read", "write", "pool") + } + ), + "flowspeech_download": download, + } + ), + ) + + def exchange(self, method: str, url: str, *, download: bool = False) -> Exchange: + for attempt in range(self.max_retries + 1): + response = yield self.request(method, url, download) # rebind-ok: one response per retry + self.remaining() + if isinstance(response, httpx.TransportError): + if attempt == self.max_retries: + raise self.error( + 502, "FlowSpeech download transport failed" if download else "FlowSpeech API transport failed" + ) + yield self.delay() + continue + if not isinstance(response, httpx.Response): + raise self.error(502, "FlowSpeech transport did not return a response") + if response.status_code in ((200,) if download or method == "GET" else (200, 202)): + return response + if response.is_redirect: + raise self.error(502, "FlowSpeech unexpected redirect rejected", response.headers) + try: + payload = FlowSpeechErrorResponse.model_validate_json( + response.content + ) # rebind-ok: parse each retry response + except ValueError: + payload = None # rebind-ok: malformed server errors have no structured payload + retryable = response.status_code == 429 or ( # rebind-ok: evaluated for each retry response + response.status_code >= 500 and (download or (payload is not None and payload.error.retryable)) + ) + if retryable and attempt < self.max_retries: + yield self.delay(response) + continue + detail = ( # rebind-ok: evaluated for each retry response + f"{payload.error.code}: {payload.error.message}; traceId={payload.traceId or 'unknown'}" + if payload + else f"unexpected HTTP {response.status_code}" + ) + raise self.error( + response.status_code if response.status_code >= 400 else 502, + f"FlowSpeech {'download' if download else 'API'}: {detail}", + response.headers, + ) + raise self.error(502, "FlowSpeech retries exhausted") + + def flow(self) -> Flow: + response = yield from self.exchange("POST", self.url) # rebind-ok: advances through job status responses + while True: + try: + job = FlowSpeechJob.model_validate_json(response.content) # rebind-ok: validate each status response + job_id = job.id # rebind-ok: model has normalized each status response's UUID + except ValueError as exc: + raise self.error(502, "FlowSpeech returned an invalid job, ID, status or result") from exc + if job.requestId != self.request_id: + raise self.error(502, "FlowSpeech returned a different request ID") + if self.job_id is not None and self.job_id != job_id: + raise self.error(502, "FlowSpeech returned a different job ID") + self.job_id = job_id + if job.status in ("failed", "cancelled"): + detail = ( + f"{job.error.code}: {job.error.message}" if job.error else job.status + ) # rebind-ok: terminal loop branch + raise self.error(422, f"FlowSpeech job {job.status}: {detail}") + if job.status == "success": + if job.resultDeleted: + raise self.error(410, "FlowSpeech recording was deleted (resultDeleted=true)") + if job.result is None: + raise self.error(502, "FlowSpeech successful job has no result") + try: + audio_url = str(validate_url(job.result.url)) # rebind-ok: terminal loop branch + except ValueError as exc: + raise self.error(502, "FlowSpeech returned an unsafe audio URL") from exc + audio = yield from self.exchange("GET", audio_url, download=True) # rebind-ok: terminal loop branch + return self.completed_response(job, audio) + yield self.delay(response) + response = yield from self.exchange("GET", f"{self.url}/{job_id}") # rebind-ok: advance job polling + + def completed_response(self, job: FlowSpeechJob, response: httpx.Response) -> HttpxBinaryResponseContent: + result: Final = self.audio_response(response) + if self.on_completed is not None: + self.on_completed(job) + return result + + def audio_response(self, response: httpx.Response) -> HttpxBinaryResponseContent: + try: + validate_wav_structure(response.content) + with wave.open(io.BytesIO(response.content), "rb") as audio: + if (audio.getframerate(), audio.getnchannels(), audio.getsampwidth(), audio.getcomptype()) != ( + 24000, + 1, + 2, + "NONE", + ): + raise ValueError("Unexpected WAV encoding") + if audio.getnframes() == 0 or len(audio.readframes(audio.getnframes() + 1)) != audio.getnframes() * 2: + raise ValueError("Empty or truncated WAV") + except (wave.Error, EOFError, ValueError) as exc: + raise self.error(502, "FlowSpeech download is not complete 24 kHz mono 16-bit WAV audio") from exc + return HttpxBinaryResponseContent( + httpx.Response( + 200, + content=response.content, + headers=MappingProxyType( + { + "content-type": "audio/wav", + "x-flowspeech-request-id": self.request_id, + "x-flowspeech-job-id": self.job_id or "", + } + ), + request=response.request, + ) + ) + + def limit(self, request: httpx.Request, response: httpx.Response) -> int: + maximum: Final = self.MAX_AUDIO_BYTES if request.extensions["flowspeech_download"] else self.MAX_JSON_BYTES + if "content-encoding" in response.headers and response.headers["content-encoding"].lower() != "identity": + raise self.error(502, "FlowSpeech encoded response rejected") + try: + length: Final = int(response.headers["content-length"]) if "content-length" in response.headers else 0 + except ValueError as exc: + raise self.error(502, "FlowSpeech invalid Content-Length") from exc + if not 0 <= length <= maximum: + raise self.error(502, "FlowSpeech response exceeds size limit") + return maximum + + def read(self, client: httpx.Client, request: httpx.Request) -> httpx.Response: + response: Final = client.send(request, stream=True, auth=None, follow_redirects=False) + try: + maximum: Final = self.limit(request, response) + with io.BytesIO() as content: + for chunk in response.iter_bytes(): + self.remaining() + if content.tell() + len(chunk) > maximum: + raise self.error(502, "FlowSpeech response exceeds size limit") + content.write(chunk) + self.remaining() + return httpx.Response( + response.status_code, headers=response.headers, content=content.getvalue(), request=request + ) + finally: + response.close() + + async def aread(self, client: httpx.AsyncClient, request: httpx.Request) -> httpx.Response: + response: Final = await client.send(request, stream=True, auth=None, follow_redirects=False) + try: + maximum: Final = self.limit(request, response) + with io.BytesIO() as content: + async for chunk in response.aiter_bytes(): + self.remaining() + if content.tell() + len(chunk) > maximum: + raise self.error(502, "FlowSpeech response exceeds size limit") + content.write(chunk) + self.remaining() + return httpx.Response( + response.status_code, headers=response.headers, content=content.getvalue(), request=request + ) + finally: + await response.aclose() + + def run(self, client: httpx.Client | None = None) -> HttpxBinaryResponseContent: + if client is None: + with httpx.Client(trust_env=False) as owned_client: + return self.run(owned_client) + flow: Final = self.flow() + action = next(flow) # rebind-ok: workflow driver advances actions + try: + while True: + result: httpx.Response | httpx.TransportError | None = None + if not isinstance(action, httpx.Request): + self.wait_budget(action) + self.sleep(action) + else: + try: + result = self.read(client, action) # rebind-ok: result feeds shared workflow + except httpx.TransportError as exc: + result = exc # rebind-ok: retry policy handles transport errors + action = flow.send(result) # rebind-ok: advance shared workflow + except StopIteration as done: + value: Final[object] = done.value # pyright: ignore[reportAny] # StopIteration erases the typed generator return; checked below + if not isinstance(value, HttpxBinaryResponseContent): + raise self.error(502, "FlowSpeech workflow did not return audio") + return value + finally: + flow.close() + + async def arun(self, client: httpx.AsyncClient | None = None) -> HttpxBinaryResponseContent: + if client is None: + async with httpx.AsyncClient(trust_env=False) as owned_client: + return await self.arun(owned_client) + try: + return await asyncio.wait_for(self._arun(client), timeout=self.remaining()) + except asyncio.TimeoutError as exc: + raise self.error(408, "FlowSpeech total deadline exceeded; recover using the same request_id") from exc + + async def _arun(self, client: httpx.AsyncClient) -> HttpxBinaryResponseContent: + flow: Final = self.flow() + action = next(flow) # rebind-ok: workflow driver advances actions + try: + while True: + result: httpx.Response | httpx.TransportError | None = None + if not isinstance(action, httpx.Request): + self.wait_budget(action) + await self.async_sleep(action) + else: + try: + result = await self.aread(client, action) # rebind-ok: result feeds shared workflow + except httpx.TransportError as exc: + result = exc # rebind-ok: retry policy handles transport errors + action = flow.send(result) # rebind-ok: advance shared workflow + except StopIteration as done: + value: Final[object] = done.value # pyright: ignore[reportAny] # StopIteration erases the typed generator return; checked below + if not isinstance(value, HttpxBinaryResponseContent): + raise self.error(502, "FlowSpeech workflow did not return audio") + return value + finally: + flow.close() diff --git a/litellm/llms/flowspeech/text_to_speech/transformation.py b/litellm/llms/flowspeech/text_to_speech/transformation.py index 784f40cf4df..a2ebaff40ef 100644 --- a/litellm/llms/flowspeech/text_to_speech/transformation.py +++ b/litellm/llms/flowspeech/text_to_speech/transformation.py @@ -1,17 +1,14 @@ -import base64 -import binascii -from typing import TYPE_CHECKING, Final +from collections.abc import Mapping +from types import MappingProxyType +from typing import TYPE_CHECKING, Final, Literal +from uuid import UUID, uuid4 import httpx -from httpx import Headers -from pydantic import BaseModel +from pydantic import BaseModel, ConfigDict, Field, TypeAdapter, field_validator import litellm from litellm.llms.base_llm.chat.transformation import BaseLLMException -from litellm.llms.base_llm.text_to_speech.transformation import ( - BaseTextToSpeechConfig, - TextToSpeechRequestData, -) +from litellm.llms.base_llm.text_to_speech.transformation import BaseTextToSpeechConfig, TextToSpeechRequestData from litellm.secret_managers.main import get_secret_str if TYPE_CHECKING: @@ -24,77 +21,146 @@ class FlowSpeechException(BaseLLMException): self, status_code: int, message: str, - headers: dict[str, str] | Headers | None = None, # mutable-ok: exception contract accepts dict headers + headers: dict[str, str] | httpx.Headers | None = None, # mutable-ok: upstream exception interface ) -> None: - super().__init__( # pyright: ignore[reportUnknownMemberType] # upstream exception keeps untyped dict fields - status_code=status_code, - message=message, - headers=headers, + super().__init__( # pyright: ignore[reportUnknownMemberType] # upstream exception has untyped dictionary fields + status_code=status_code, message=message, headers=headers ) -class FlowSpeechAudioData(BaseModel): - mimeType: str - audioBase64: str +class FlowSpeechSegment(BaseModel): + model_config = ConfigDict(extra="forbid", strict=True, frozen=True) + speaker: str = Field(min_length=1, max_length=100) + text: str = Field(min_length=1, max_length=100_000) + voiceName: str = Field(min_length=1, max_length=100) -class FlowSpeechResponse(BaseModel): - code: int - message: str | None = None - data: FlowSpeechAudioData | None = None +class FlowSpeechResult(BaseModel): + model_config = ConfigDict(strict=True, frozen=True) + url: str + mimeType: Literal["audio/wav"] + sampleRate: Literal[24000] + channels: Literal[1] + bitsPerSample: Literal[16] + + +class FlowSpeechError(BaseModel): + model_config = ConfigDict(strict=True, frozen=True) + code: str + message: str + retryable: bool = False + + +class FlowSpeechUsage(BaseModel): + model_config = ConfigDict(strict=True, frozen=True) + billableCharacters: int = Field(ge=0) + reservedCredits: int = Field(ge=0) + chargedCredits: int = Field(ge=0) + + +class FlowSpeechJob(BaseModel): + model_config = ConfigDict(strict=True, frozen=True) + id: str + requestId: str + usage: FlowSpeechUsage + status: Literal["queued", "running", "merging", "settling", "success", "failed", "cancelled"] + result: FlowSpeechResult | None = None + resultDeleted: bool = False + error: FlowSpeechError | None = None + + @field_validator("id", "requestId") + @classmethod + def canonical_identifier(cls, value: str) -> str: + return validate_uuid(value) + + +class FlowSpeechErrorResponse(BaseModel): + model_config = ConfigDict(strict=True, frozen=True) + error: FlowSpeechError + traceId: str | None = None + + +def validate_uuid(value: object) -> str: + text: Final = TypeAdapter(str).validate_python(value, strict=True) + try: + parsed: Final = str(UUID(text)) + except ValueError as exc: + raise ValueError("FlowSpeech requires a UUID") from exc + if parsed != text.lower(): + raise ValueError("FlowSpeech requires a hyphenated UUID") + return parsed + + +def validate_url(value: str) -> httpx.URL: + try: + url: Final = httpx.URL(value) + except httpx.InvalidURL as exc: + raise ValueError("FlowSpeech requires a valid HTTPS URL") from exc + if url.scheme != "https" or not url.host or url.userinfo or url.fragment: + raise ValueError("FlowSpeech requires an HTTPS URL without credentials or fragment") + return url class FlowSpeechTextToSpeechConfig(BaseTextToSpeechConfig): TTS_BASE_URL = "https://flowspeech.io" - TTS_ENDPOINT_PATH = "/api/ai/text-to-speech" + TTS_ENDPOINT_PATH = "/api/v1/tts/jobs" DEFAULT_VOICE = "Kore" - def get_supported_openai_params(self, model: str) -> list[str]: # mutable-ok: base provider interface returns list - return ["voice"] # mutable-ok: base provider interface returns list + def get_supported_openai_params(self, model: str) -> list[str]: # mutable-ok: base provider interface + return ["voice", "response_format"] # mutable-ok: base provider interface def map_openai_params( self, model: str, - optional_params: dict[str, object], # mutable-ok: base provider interface requires dict - voice: str | dict[str, object] | None = None, # mutable-ok: base provider interface accepts dict voices + optional_params: Mapping[str, object], + voice: str | Mapping[str, object] | None = None, drop_params: bool = False, - kwargs: dict[str, object] | None = None, # mutable-ok: base provider interface requires dict - ) -> tuple[str | None, dict[str, object]]: # mutable-ok: base provider interface returns dict params - mapped_voice: Final = self._resolve_voice(voice) - return mapped_voice, {} # mutable-ok: base provider interface returns dict params + kwargs: Mapping[str, object] | None = None, + ) -> tuple[str | None, dict[str, object]]: # mutable-ok: base provider interface + if model != "flowspeech-tts": + raise ValueError("FlowSpeech only supports the flowspeech/flowspeech-tts route") + if any(key != "response_format" for key in optional_params): + raise ValueError("FlowSpeech does not support instructions or speed") + if optional_params.get("response_format") not in (None, "wav"): + raise ValueError("FlowSpeech only supports response_format='wav'") + supplied: Final = kwargs or MappingProxyType({}) + if any( + key in supplied + for key in ("source", "format", "speakers", "originalText", "prompt", "stream", "extra_body") + ): + raise ValueError("Unsupported FlowSpeech option; use voice or segments") + if "segments" in supplied and voice is not None: + raise ValueError("Use segment voiceName fields instead of voice for multi-speaker input") + return self._resolve_voice(voice), { # mutable-ok: base provider interface + key: supplied[key] for key in ("segments", "request_id") if key in supplied + } - def _resolve_voice( - self, - voice: str | dict[str, object] | None, # mutable-ok: base provider interface accepts dict voices - ) -> str: + def _resolve_voice(self, voice: str | Mapping[str, object] | None) -> str: + if voice is None: + return self.DEFAULT_VOICE if isinstance(voice, str) and voice.strip(): return voice.strip() - if isinstance(voice, dict): + if isinstance(voice, Mapping): candidate: Final = next( - ( - value - for key in ("voice_name", "voiceName", "id", "name") - if isinstance((value := voice.get(key)), str) and value.strip() - ), - None, + (voice[key] for key in ("voice_name", "voiceName", "id", "name") if key in voice), None ) - if isinstance(candidate, str): + if isinstance(candidate, str) and candidate.strip(): return candidate.strip() - return self.DEFAULT_VOICE + raise ValueError("FlowSpeech voice must be a nonempty voice name") def validate_environment( - self, - headers: dict[str, str], # mutable-ok: base provider interface requires dict headers - model: str, - api_key: str | None = None, - api_base: str | None = None, - ) -> dict[str, str]: # mutable-ok: base provider interface returns dict headers - resolved_api_key: Final = api_key or litellm.api_key or get_secret_str("FLOWSPEECH_API_KEY") - if resolved_api_key is None: - raise ValueError("FlowSpeech API key is required. Set FLOWSPEECH_API_KEY environment variable.") - return { # mutable-ok: base provider interface returns dict headers - **headers, - "Authorization": f"Bearer {resolved_api_key}", + self, headers: Mapping[str, str], model: str, api_key: str | None = None, api_base: str | None = None + ) -> dict[str, str]: # mutable-ok: base provider interface + key: Final = api_key if api_key is not None else (litellm.api_key or get_secret_str("FLOWSPEECH_API_KEY")) + if not key or not key.isascii() or any(char.isspace() or ord(char) < 33 or ord(char) == 127 for char in key): + raise ValueError("FlowSpeech API key is required and must be a nonempty token. Set FLOWSPEECH_API_KEY.") + if any(name.lower() in ("cookie", "proxy-authorization", "host", "content-length") for name in headers): + raise ValueError("FlowSpeech does not accept cookie, proxy credential, host or content-length overrides") + return { # mutable-ok: base provider interface + **{ # mutable-ok: headers are materialized once + name: value for name, value in headers.items() if name.lower() not in ("authorization", "content-type") + }, + "Authorization": f"Bearer {key}", "Content-Type": "application/json", } @@ -102,29 +168,56 @@ class FlowSpeechTextToSpeechConfig(BaseTextToSpeechConfig): self, error_message: str, status_code: int, - headers: dict[str, str] | Headers, # mutable-ok: exception contract accepts dict headers + headers: dict[str, str] | httpx.Headers, # mutable-ok: upstream exception interface ) -> BaseLLMException: - return FlowSpeechException(message=error_message, status_code=status_code, headers=headers) + return FlowSpeechException(status_code, error_message, headers) + + def _input_body( + self, + input: str, + voice: str | None, + optional_params: Mapping[str, object], + ) -> dict[str, object]: # mutable-ok: JSON body required by base interface + if "segments" in optional_params: + if input: + raise ValueError("Use input='' with FlowSpeech segments") + segments: Final = TypeAdapter(list[FlowSpeechSegment]).validate_python(optional_params["segments"]) + if not 1 <= len(segments) <= 1000 or any( + not part.speaker.strip() or not part.text.strip() or not part.voiceName.strip() for part in segments + ): + raise ValueError("FlowSpeech requires 1 to 1000 nonempty speaker turns") + return { # mutable-ok: JSON body required by base interface + "mode": "multi", + "segments": [part.model_dump() for part in segments], # mutable-ok: JSON speaker array + } + if not input.strip(): + raise ValueError("FlowSpeech input must contain text") + return { # mutable-ok: JSON body required by base interface + "mode": "single", + "text": input, + "voiceName": self._resolve_voice(voice), + } def transform_text_to_speech_request( self, model: str, input: str, voice: str | None, - optional_params: dict[str, object], # mutable-ok: base provider interface requires dict - litellm_params: dict[str, object], # mutable-ok: base provider interface requires dict - headers: dict[str, str], # mutable-ok: base provider interface requires dict + optional_params: Mapping[str, object], + litellm_params: Mapping[str, object], + headers: Mapping[str, str], ) -> TextToSpeechRequestData: - request_body: Final[dict[str, object]] = { # mutable-ok: JSON request body requires a dict - "text": input, - "originalText": input, - "speakers": [ # mutable-ok: FlowSpeech JSON schema requires a speakers array - {"voiceName": voice or self.DEFAULT_VOICE} # mutable-ok: FlowSpeech JSON schema requires an object - ], - } + if frozenset(optional_params) - frozenset(("segments", "request_id")): + raise ValueError("Unsupported FlowSpeech request options") + body: Final = self._input_body(input, voice, optional_params) + header_id: Final = next((value for key, value in headers.items() if key.lower() == "idempotency-key"), None) + supplied_id: Final = optional_params.get("request_id", header_id) + request_id: Final = validate_uuid(supplied_id) if supplied_id is not None else str(uuid4()) + if header_id is not None and validate_uuid(header_id) != request_id: + raise ValueError("FlowSpeech request_id and Idempotency-Key must agree") return TextToSpeechRequestData( - dict_body=request_body, - headers={"Content-Type": "application/json"}, # mutable-ok: base request type requires dict headers + dict_body={"input": body}, # mutable-ok: base request interface + headers={"Idempotency-Key": request_id}, # mutable-ok: base request interface ) def transform_text_to_speech_response( @@ -135,57 +228,13 @@ class FlowSpeechTextToSpeechConfig(BaseTextToSpeechConfig): ) -> "HttpxBinaryResponseContent": from litellm.types.llms.openai import HttpxBinaryResponseContent - try: - payload: Final = FlowSpeechResponse.model_validate(raw_response.json()) - except ValueError as exc: - raise FlowSpeechException( - status_code=raw_response.status_code, - message="FlowSpeech API returned an invalid JSON response", - headers=raw_response.headers, - ) from exc + if raw_response.status_code != 200 or raw_response.headers.get("content-type") != "audio/wav": + raise FlowSpeechException(502, "FlowSpeech requires completed, downloaded WAV audio") + return HttpxBinaryResponseContent(raw_response) - if payload.code != 0 or payload.data is None or not payload.data.audioBase64: - raise FlowSpeechException( - status_code=raw_response.status_code, - message=payload.message or "FlowSpeech API response did not include audio data", - headers=raw_response.headers, - ) - - try: - audio_bytes: Final = base64.b64decode(payload.data.audioBase64, validate=True) - except (binascii.Error, ValueError) as exc: - raise FlowSpeechException( - status_code=raw_response.status_code, - message="FlowSpeech API returned invalid base64 audio data", - headers=raw_response.headers, - ) from exc - - preserved_headers: Final = { # mutable-ok: httpx.Response requires materialized headers - key: value - for key, value in raw_response.headers.items() - if key.lower() not in frozenset({"content-encoding", "content-length", "content-type", "transfer-encoding"}) - } - response_headers: Final = { # mutable-ok: httpx.Response requires materialized headers - **preserved_headers, - "content-length": str(len(audio_bytes)), - "content-type": payload.data.mimeType, - } - binary_response: Final = httpx.Response( - status_code=200, - headers=response_headers, - content=audio_bytes, - request=raw_response.request, - ) - return HttpxBinaryResponseContent(binary_response) - - def get_complete_url( - self, - model: str, - api_base: str | None, - litellm_params: dict[str, object], # mutable-ok: base provider interface requires dict - ) -> str: - base_url: Final = api_base or get_secret_str("FLOWSPEECH_API_BASE") or self.TTS_BASE_URL - normalized_base_url: Final = base_url.rstrip("/") - if normalized_base_url.endswith(self.TTS_ENDPOINT_PATH): - return normalized_base_url - return f"{normalized_base_url}{self.TTS_ENDPOINT_PATH}" + def get_complete_url(self, model: str, api_base: str | None, litellm_params: Mapping[str, object]) -> str: + base: Final = api_base if api_base is not None else (get_secret_str("FLOWSPEECH_API_BASE") or self.TTS_BASE_URL) + url: Final = validate_url(base) + if url.query or url.path.rstrip("/") not in ("", self.TTS_ENDPOINT_PATH): + raise ValueError("FlowSpeech api_base must be an origin or the /api/v1/tts/jobs endpoint") + return str(url.copy_with(path=self.TTS_ENDPOINT_PATH)) diff --git a/litellm/main.py b/litellm/main.py index e9c1b9fa281..5c87cb3ca0e 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -8226,7 +8226,9 @@ def speech( _is_async=aspeech or False, ) elif custom_llm_provider == "flowspeech": + from litellm.llms.flowspeech.text_to_speech.handler import FlowSpeechTTS from litellm.llms.flowspeech.text_to_speech.transformation import ( + FlowSpeechJob, FlowSpeechTextToSpeechConfig, ) @@ -8244,20 +8246,56 @@ def speech( if api_key is not None: litellm_params_dict["api_key"] = api_key - voice_name: Final = voice if isinstance(voice, str) else None - response = base_llm_http_handler.text_to_speech_handler( # rebind-ok: provider branch sets dispatch result + flowspeech_headers: Final = flowspeech_config.validate_environment( + extra_headers or headers or MappingProxyType({}), model, litellm_params_dict.get("api_key"), api_base + ) + flowspeech_request: Final = flowspeech_config.transform_text_to_speech_request( model=model, input=input, - voice=voice_name, - text_to_speech_provider_config=flowspeech_config, - text_to_speech_optional_params=optional_params, - custom_llm_provider=custom_llm_provider, + voice=voice if isinstance(voice, str) else None, + optional_params=optional_params, litellm_params=litellm_params_dict, - logging_obj=logging_obj, + headers=flowspeech_headers, + ) + flowspeech_url: Final = flowspeech_config.get_complete_url(model, api_base, litellm_params_dict) + + def flowspeech_post_call(job: FlowSpeechJob) -> None: + logging_obj.post_call( + input=input, + api_key="", + original_response=job.model_dump_json( + include={"id", "requestId", "status", "usage"} # mutable-ok: Pydantic include contract requires set + ), + additional_args=MappingProxyType({"request_id": job.requestId, "job_id": job.id}), + ) + + flowspeech_job: Final = FlowSpeechTTS( + url=flowspeech_url, + headers=MappingProxyType( + { + **MappingProxyType( + {key: value for key, value in flowspeech_headers.items() if key.lower() != "idempotency-key"} + ), + **flowspeech_request["headers"], + } + ), + body=flowspeech_request["dict_body"], timeout=timeout, - extra_headers=extra_headers, - client=client, - _is_async=aspeech or False, + max_retries=max_retries, + on_completed=flowspeech_post_call, + ) + logging_obj.pre_call( + input=input, + api_key="", + additional_args=MappingProxyType({"api_base": flowspeech_url, "request_id": flowspeech_job.request_id}), + ) + flowspeech_client: Final = client.client if isinstance(client, (HTTPHandler, AsyncHTTPHandler)) else client + if flowspeech_client is not None and not isinstance( + flowspeech_client, httpx.AsyncClient if aspeech else httpx.Client + ): + raise ValueError("FlowSpeech client must match the synchronous or asynchronous speech call") + response = ( # rebind-ok: provider branch sets dispatch result + flowspeech_job.arun(flowspeech_client) if aspeech else flowspeech_job.run(flowspeech_client) ) elif custom_llm_provider == "vertex_ai" or custom_llm_provider == "vertex_ai_beta": from litellm.llms.vertex_ai.text_to_speech.transformation import ( diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index 990682f10a5..993a7ae2346 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -10826,6 +10826,18 @@ async def audio_speech( ): media_type = "audio/wav" # Gemini TTS returns WAV format after conversion + if isinstance(response, HttpxBinaryResponseContent) and "x-flowspeech-job-id" in response.response.headers: + media_type = "audio/wav" # rebind-ok: FlowSpeech v1 returns downloaded WAV even for proxy model aliases + custom_headers.update( + MappingProxyType( + { + key: response.response.headers[key] + for key in ("x-flowspeech-job-id", "x-flowspeech-request-id") + if key in response.response.headers + } + ) + ) + return StreamingResponse( _audio_speech_chunk_generator(response), media_type=media_type, diff --git a/tests/test_litellm/llms/flowspeech/test_handler.py b/tests/test_litellm/llms/flowspeech/test_handler.py new file mode 100644 index 00000000000..c0641e9e493 --- /dev/null +++ b/tests/test_litellm/llms/flowspeech/test_handler.py @@ -0,0 +1,512 @@ +import asyncio +import io +import json +import struct +import wave +from datetime import datetime, timezone +from email.utils import format_datetime + +import httpx +import pytest + +from litellm.llms.flowspeech.text_to_speech.handler import FlowSpeechTTS +from litellm.llms.flowspeech.text_to_speech.transformation import FlowSpeechException + +REQUEST_ID = "b698824b-de05-486d-8833-05d5c4522a39" +JOB_ID = "11111111-2222-4333-8444-555555555555" +URL = "https://flowspeech.io/api/v1/tts/jobs" +AUDIO_URL = "https://audio.example.com/recording.wav" + + +def wav_bytes(pcm=b"\x00\x00" * 24, metadata=False): + output = io.BytesIO() + with wave.open(output, "wb") as audio: + audio.setparams((1, 2, 24000, 0, "NONE", "not compressed")) + audio.writeframes(pcm) + content = output.getvalue() + if metadata: + content = content[:12] + b"JUNK" + struct.pack(" httpx.Response: - assert request.url == "https://flowspeech.io/api/ai/text-to-speech" - assert request.headers["authorization"] == "Bearer test-key" - assert json.loads(request.content) == { - "text": "Hello", - "originalText": "Hello", - "speakers": [{"voiceName": "Aoede"}], - } - return httpx.Response( - 200, - json={ - "code": 0, - "data": { - "mimeType": "audio/mpeg", - "audioBase64": base64.b64encode(audio).decode(), - }, - }, + def log_event(details): + if details.get("log_event_type") == "post_api_call": + logs.append((details["api_key"], details["original_response"], dict(details["additional_args"]))) + + def respond(request): + requests.append(request) + if request.method == "POST": + assert request.url.path == "/api/v1/tts/jobs" + assert request.headers["authorization"] == "Bearer test-key" + assert request.headers["idempotency-key"] == REQUEST_ID + assert json.loads(request.content) == { + "input": ( + {"mode": "multi", "segments": segments} + if multi + else {"mode": "single", "text": "Hello", "voiceName": "Aoede"} + ) + } + return httpx.Response(202, json=success()) + assert request.url.host == "audio.example.com" + assert logs == [] + assert "authorization" not in request.headers and "cookie" not in request.headers + return httpx.Response(200, content=wav_bytes()) + + with httpx.Client(transport=httpx.MockTransport(respond)) as http_client: + result = litellm.speech( + model="flowspeech/flowspeech-tts", + input="" if multi else "Hello", + api_key="test-key", + response_format="wav", + request_id=REQUEST_ID, + timeout=60, + client=HTTPHandler(client=http_client), + logger_fn=log_event, + **({"segments": segments} if multi else {"voice": "Aoede"}), ) + assert result.content == wav_bytes() + assert result.response.headers["content-type"] == "audio/wav" + assert result.response.headers["x-flowspeech-job-id"] == JOB_ID + assert len(requests) == 2 + assert len(logs) == 1 and logs[0][0] == "" + assert json.loads(logs[0][1]) == {key: success()[key] for key in ("id", "requestId", "status", "usage")} + assert logs[0][2] == {"request_id": REQUEST_ID, "job_id": JOB_ID} + assert "test-key" not in repr(logs) and "audio.example.com" not in repr(logs) - client = HTTPHandler(client=httpx.Client(transport=httpx.MockTransport(respond))) - result = litellm.speech( - model="flowspeech/flowspeech-tts", - input="Hello", - voice="Aoede", - api_key="test-key", - client=client, - ) - assert result.content == audio - assert result.response.headers["content-type"] == "audio/mpeg" +@pytest.mark.asyncio +async def test_aspeech_dispatch(): + requests = [] + logs = [] + + def log_event(details): + if details.get("log_event_type") == "post_api_call": + logs.append((details["api_key"], details["original_response"], dict(details["additional_args"]))) + + async def respond(request): + requests.append(request) + if request.method == "POST": + assert json.loads(request.content)["input"]["voiceName"] == "Kore" + return httpx.Response(200, json=success(request.headers["idempotency-key"])) + assert "authorization" not in request.headers + assert logs == [] + return httpx.Response(200, content=wav_bytes()) + + async with httpx.AsyncClient(transport=httpx.MockTransport(respond)) as client: + result = await litellm.aspeech( + model="flowspeech/flowspeech-tts", + input="Hello", + api_key="test-key", + client=client, + timeout=60, + logger_fn=log_event, + ) + assert result.content == wav_bytes() + assert len(requests) == 2 + assert len(logs) == 1 and logs[0][0] == "" + assert json.loads(logs[0][1])["requestId"] == requests[0].headers["idempotency-key"] + assert logs[0][2]["job_id"] == JOB_ID + assert "test-key" not in repr(logs) and "audio.example.com" not in repr(logs) diff --git a/tests/test_litellm/proxy/proxy_server/test_routes_audio.py b/tests/test_litellm/proxy/proxy_server/test_routes_audio.py index 74542a3eaf6..df344ce656f 100644 --- a/tests/test_litellm/proxy/proxy_server/test_routes_audio.py +++ b/tests/test_litellm/proxy/proxy_server/test_routes_audio.py @@ -12,6 +12,7 @@ from __future__ import annotations import io from unittest.mock import AsyncMock, MagicMock +import httpx import pytest from litellm.proxy import proxy_server @@ -152,6 +153,48 @@ def test_audio_speech_happy_path(client, auth_as, patched_speech, path): } +@pytest.mark.parametrize("path", ["/v1/audio/speech", "/audio/speech"]) +def test_flowspeech_proxy_preserves_wav_and_recovery_ids(client, auth_as, patched_speech, monkeypatch, path): + from litellm.types.llms.openai import HttpxBinaryResponseContent + + job_id = "11111111-2222-4333-8444-555555555555" + request_id = "b698824b-de05-486d-8833-05d5c4522a39" + + async def audio(): + return HttpxBinaryResponseContent( + httpx.Response( + 200, + content=b"RIFF-test-WAVE", + headers={ + "content-type": "audio/wav", + "x-flowspeech-job-id": job_id, + "x-flowspeech-request-id": request_id, + }, + ) + ) + + async def route(*args, **kwargs): + assert kwargs["data"]["request_id"] == request_id + return audio() + + monkeypatch.setattr(proxy_server, "route_request", route) + with auth_as(): + response = client.post( + path, + json={ + "model": "narration-alias", + "input": "Hi", + "voice": "Kore", + "request_id": request_id, + }, + ) + assert response.status_code == 200 + assert response.content == b"RIFF-test-WAVE" + assert response.headers["content-type"] == "audio/wav" + assert response.headers["x-flowspeech-job-id"] == job_id + assert response.headers["x-flowspeech-request-id"] == request_id + + @pytest.mark.parametrize("path", ["/v1/audio/speech", "/audio/speech"]) def test_audio_speech_error(client, auth_as, patched_speech_error, path): """Pins ``POST /v1/audio/speech`` and ``POST /audio/speech`` (error).""" From d84f5aa51d2d29f15f23a87f54ac046cf55460a3 Mon Sep 17 00:00:00 2001 From: "heng.yin" Date: Mon, 26 Oct 2026 18:29:08 +0800 Subject: [PATCH 06/11] fix(flowspeech): satisfy provider CI lint and shard checks --- .github/workflows/test-unit.yml | 1 + .../llms/flowspeech/text_to_speech/handler.py | 18 ++--- .../text_to_speech/transformation.py | 8 +-- litellm/main.py | 4 +- tests/unit/llms/flowspeech/test_handler.py | 45 ++++++++++++ .../test_text_to_speech_transformation.py | 70 ++++++++++++------- 6 files changed, 105 insertions(+), 41 deletions(-) diff --git a/.github/workflows/test-unit.yml b/.github/workflows/test-unit.yml index 76af64c8ccd..7833bccc222 100644 --- a/.github/workflows/test-unit.yml +++ b/.github/workflows/test-unit.yml @@ -269,6 +269,7 @@ jobs: tests/unit/llms/featherless_ai tests/unit/llms/firecrawl tests/unit/llms/fireworks_ai + tests/unit/llms/flowspeech tests/unit/llms/friendliai tests/unit/llms/gdc tests/unit/llms/gemini diff --git a/litellm/llms/flowspeech/text_to_speech/handler.py b/litellm/llms/flowspeech/text_to_speech/handler.py index 1111cca42f7..dff182c82fd 100644 --- a/litellm/llms/flowspeech/text_to_speech/handler.py +++ b/litellm/llms/flowspeech/text_to_speech/handler.py @@ -176,9 +176,7 @@ class FlowSpeechTTS: if response.is_redirect: raise self.error(502, "FlowSpeech unexpected redirect rejected", response.headers) try: - payload = FlowSpeechErrorResponse.model_validate_json( - response.content - ) + payload = FlowSpeechErrorResponse.model_validate_json(response.content) except ValueError: payload = None retryable = response.status_code == 429 or ( @@ -213,9 +211,7 @@ class FlowSpeechTTS: raise self.error(502, "FlowSpeech returned a different job ID") self.job_id = job_id if job.status in ("failed", "cancelled"): - detail = ( - f"{job.error.code}: {job.error.message}" if job.error else job.status - ) + detail = f"{job.error.code}: {job.error.message}" if job.error else job.status raise self.error(422, f"FlowSpeech job {job.status}: {detail}") if job.status == "success": if job.resultDeleted: @@ -316,7 +312,10 @@ class FlowSpeechTTS: def run(self, client: httpx.Client | None = None) -> HttpxBinaryResponseContent: if client is None: with httpx.Client(trust_env=False) as owned_client: - return self.run(owned_client) + return self._run(owned_client) + return self._run(client) + + def _run(self, client: httpx.Client) -> HttpxBinaryResponseContent: flow: Final = self.flow() action = next(flow) # rebind-ok: workflow driver advances actions try: @@ -342,7 +341,10 @@ class FlowSpeechTTS: async def arun(self, client: httpx.AsyncClient | None = None) -> HttpxBinaryResponseContent: if client is None: async with httpx.AsyncClient(trust_env=False) as owned_client: - return await self.arun(owned_client) + return await self._arun_with_deadline(owned_client) + return await self._arun_with_deadline(client) + + async def _arun_with_deadline(self, client: httpx.AsyncClient) -> HttpxBinaryResponseContent: try: return await asyncio.wait_for(self._arun(client), timeout=self.remaining()) except asyncio.TimeoutError as exc: diff --git a/litellm/llms/flowspeech/text_to_speech/transformation.py b/litellm/llms/flowspeech/text_to_speech/transformation.py index 9e94fbc936b..4e27e4eaaac 100644 --- a/litellm/llms/flowspeech/text_to_speech/transformation.py +++ b/litellm/llms/flowspeech/text_to_speech/transformation.py @@ -131,9 +131,7 @@ class FlowSpeechTextToSpeechConfig(BaseTextToSpeechConfig): raise ValueError("Unsupported FlowSpeech option; use voice or segments") if "segments" in supplied and voice is not None: raise ValueError("Use segment voiceName fields instead of voice for multi-speaker input") - return self._resolve_voice(voice), { - key: supplied[key] for key in ("segments", "request_id") if key in supplied - } + return self._resolve_voice(voice), {key: supplied[key] for key in ("segments", "request_id") if key in supplied} def _resolve_voice(self, voice: str | Mapping[str, object] | None) -> str: if voice is None: @@ -157,9 +155,7 @@ class FlowSpeechTextToSpeechConfig(BaseTextToSpeechConfig): if any(name.lower() in ("cookie", "proxy-authorization", "host", "content-length") for name in headers): raise ValueError("FlowSpeech does not accept cookie, proxy credential, host or content-length overrides") return { - **{ - name: value for name, value in headers.items() if name.lower() not in ("authorization", "content-type") - }, + **{name: value for name, value in headers.items() if name.lower() not in ("authorization", "content-type")}, "Authorization": f"Bearer {key}", "Content-Type": "application/json", } diff --git a/litellm/main.py b/litellm/main.py index 757d5abad63..694e371afca 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -8909,9 +8909,7 @@ def speech( logging_obj.post_call( input=input, api_key="", - original_response=job.model_dump_json( - include={"id", "requestId", "status", "usage"} - ), + original_response=job.model_dump_json(include={"id", "requestId", "status", "usage"}), additional_args=MappingProxyType({"request_id": job.requestId, "job_id": job.id}), ) diff --git a/tests/unit/llms/flowspeech/test_handler.py b/tests/unit/llms/flowspeech/test_handler.py index c0641e9e493..578aee7e9d4 100644 --- a/tests/unit/llms/flowspeech/test_handler.py +++ b/tests/unit/llms/flowspeech/test_handler.py @@ -483,6 +483,51 @@ def test_stream_size_limit_without_content_length(invoke): invoke(tts, respond) +@pytest.mark.parametrize("asynchronous", [False, True]) +@pytest.mark.parametrize("owned", [False, True]) +@pytest.mark.parametrize("successful", [False, True]) +@pytest.mark.asyncio +async def test_client_lifetime(monkeypatch, asynchronous, owned, successful): + methods = [] + + def respond(request): + methods.append(request.method) + if request.method == "POST": + return httpx.Response(202, json=job("success" if successful else "failed")) + return httpx.Response(200, content=wav_bytes()) + + client_type = httpx.AsyncClient if asynchronous else httpx.Client + client = client_type(transport=httpx.MockTransport(respond)) + + def create_client(*, trust_env): + assert trust_env is False + return client + + if owned: + monkeypatch.setattr(httpx, "AsyncClient" if asynchronous else "Client", create_client) + tts = handler(Clock()) + + async def call(): + if asynchronous: + return await tts.arun(None if owned else client) + return tts.run(None if owned else client) + + try: + if successful: + result = await call() + assert result.content == wav_bytes() + else: + with pytest.raises(FlowSpeechException, match="FlowSpeech job failed"): + await call() + assert methods == (["POST", "GET"] if successful else ["POST"]) + assert client.is_closed is owned + finally: + if asynchronous: + await client.aclose() + else: + client.close() + + @pytest.mark.asyncio async def test_async_total_deadline_interrupts_network(): async def respond(request): diff --git a/tests/unit/llms/flowspeech/test_text_to_speech_transformation.py b/tests/unit/llms/flowspeech/test_text_to_speech_transformation.py index fd37766ddb9..3b11512ea70 100644 --- a/tests/unit/llms/flowspeech/test_text_to_speech_transformation.py +++ b/tests/unit/llms/flowspeech/test_text_to_speech_transformation.py @@ -58,9 +58,16 @@ def test_maps_multi_and_recovery_id(): assert request["headers"]["Idempotency-Key"] == REQUEST_ID -@pytest.mark.parametrize("params", [{"speed": 1}, {"instructions": "calm"}, {"response_format": "mp3"}]) -def test_rejects_unsupported_options_even_with_drop_params(params): - with pytest.raises(ValueError): +@pytest.mark.parametrize( + "params,message", + [ + ({"speed": 1}, "FlowSpeech does not support instructions or speed"), + ({"instructions": "calm"}, "FlowSpeech does not support instructions or speed"), + ({"response_format": "mp3"}, "FlowSpeech only supports response_format='wav'"), + ], +) +def test_rejects_unsupported_options_even_with_drop_params(params, message): + with pytest.raises(ValueError, match=message): FlowSpeechTextToSpeechConfig().map_openai_params("flowspeech-tts", params, drop_params=True) @@ -72,23 +79,35 @@ def test_rejects_legacy_and_unsupported_kwargs(key): @pytest.mark.parametrize("voice", ["", " ", {}, {"voiceName": ""}, {"voiceName": 7}]) def test_rejects_invalid_voice(voice): - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="FlowSpeech voice must be a nonempty voice name"): FlowSpeechTextToSpeechConfig().map_openai_params("flowspeech-tts", {}, voice) @pytest.mark.parametrize( - "params,text", + "params,text,message", [ - ({"segments": []}, ""), - ({"segments": [{"speaker": "A", "text": "Hi", "voiceName": "Kore", "speed": 1}]}, ""), - ({"segments": [{"speaker": " ", "text": "Hi", "voiceName": "Kore"}]}, ""), - ({"segments": [{"speaker": "A", "text": "Hi", "voiceName": "Kore"}]}, "ambiguous"), - ({"request_id": "../bad"}, "Hi"), - ({}, " "), + ({"segments": []}, "", "FlowSpeech requires 1 to 1000 nonempty speaker turns"), + ( + {"segments": [{"speaker": "A", "text": "Hi", "voiceName": "Kore", "speed": 1}]}, + "", + "Extra inputs are not permitted", + ), + ( + {"segments": [{"speaker": " ", "text": "Hi", "voiceName": "Kore"}]}, + "", + "FlowSpeech requires 1 to 1000 nonempty speaker turns", + ), + ( + {"segments": [{"speaker": "A", "text": "Hi", "voiceName": "Kore"}]}, + "ambiguous", + "Use input='' with FlowSpeech segments", + ), + ({"request_id": "../bad"}, "Hi", "FlowSpeech requires a UUID"), + ({}, " ", "FlowSpeech input must contain text"), ], ) -def test_invalid_requests(params, text): - with pytest.raises(ValueError): +def test_invalid_requests(params, text, message): + with pytest.raises(ValueError, match=message): FlowSpeechTextToSpeechConfig().transform_text_to_speech_request("flowspeech-tts", text, None, params, {}, {}) @@ -125,7 +144,7 @@ def test_rejects_invalid_key(key): @pytest.mark.parametrize("name", ["Cookie", "Proxy-Authorization", "Host", "content-length"]) def test_rejects_unsafe_api_headers(name): - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="does not accept cookie, proxy credential, host or content-length overrides"): FlowSpeechTextToSpeechConfig().validate_environment({name: "test"}, "flowspeech-tts", "test-key") @@ -145,19 +164,22 @@ def test_complete_url(base): @pytest.mark.parametrize( - "base", + "base,message", [ - "http://example.com", - "https://u:p@example.com", - "file:///tmp/test", - "https://example.com/?key=x", - "https://example.com/#x", - "https://example.com/api/ai/text-to-speech", - "", + ("http://example.com", "FlowSpeech requires an HTTPS URL without credentials or fragment"), + ("https://u:p@example.com", "FlowSpeech requires an HTTPS URL without credentials or fragment"), + ("file:///tmp/test", "FlowSpeech requires an HTTPS URL without credentials or fragment"), + ("https://example.com/?key=x", "FlowSpeech api_base must be an origin or the /api/v1/tts/jobs endpoint"), + ("https://example.com/#x", "FlowSpeech requires an HTTPS URL without credentials or fragment"), + ( + "https://example.com/api/ai/text-to-speech", + "FlowSpeech api_base must be an origin or the /api/v1/tts/jobs endpoint", + ), + ("", "FlowSpeech requires an HTTPS URL without credentials or fragment"), ], ) -def test_rejects_invalid_base(base): - with pytest.raises(ValueError): +def test_rejects_invalid_base(base, message): + with pytest.raises(ValueError, match=message): FlowSpeechTextToSpeechConfig().get_complete_url("flowspeech-tts", base, {}) From 337b6791db3c2e9b591dfde0522fa6444d416021 Mon Sep 17 00:00:00 2001 From: "heng.yin" Date: Mon, 26 Oct 2026 19:15:50 +0800 Subject: [PATCH 07/11] fix: align FlowSpeech integration with provider contracts --- litellm/__init__.py | 5 ++ .../llms/flowspeech/text_to_speech/handler.py | 12 ++-- .../text_to_speech/transformation.py | 10 +++- litellm/main.py | 43 ++++++++------ litellm/proxy/proxy_server.py | 26 +++++---- .../provider_create_fields.json | 28 +++++++++ tests/unit/llms/flowspeech/test_handler.py | 37 +++++++++++- .../test_text_to_speech_transformation.py | 58 +++++++++++++++++-- .../public_endpoints/test_public_endpoints.py | 18 ++++++ 9 files changed, 194 insertions(+), 43 deletions(-) diff --git a/litellm/__init__.py b/litellm/__init__.py index c0141f43611..0a72fd38b26 100644 --- a/litellm/__init__.py +++ b/litellm/__init__.py @@ -673,6 +673,7 @@ nebius_embedding_models: Set = set() aiml_models: Set = set() deepgram_models: Set = set() elevenlabs_models: Set = set() +flowspeech_models: Final[set[str]] = set() # mutable-ok: populated when the shared model catalog loads dashscope_models: Set = set() qwencloud_models: Set = set() qwen_ai_platform_models: Set = set() @@ -949,6 +950,8 @@ def _populate_provider_model_sets(model_cost_map: Mapping[str, Mapping[str, obje deepgram_models.add(key) elif value.get("litellm_provider") == "elevenlabs": elevenlabs_models.add(key) + elif value.get("litellm_provider") == "flowspeech": + flowspeech_models.add(key) elif value.get("litellm_provider") == "heroku": heroku_models.add(key) elif value.get("litellm_provider") == "dashscope": @@ -1131,6 +1134,7 @@ model_list = list( | nscale_models | deepgram_models | elevenlabs_models + | flowspeech_models | dashscope_models | qwencloud_models | qwen_ai_platform_models @@ -1247,6 +1251,7 @@ def _build_models_by_provider() -> dict: "featherless_ai": featherless_ai_models, "deepgram": deepgram_models, "elevenlabs": elevenlabs_models, + "flowspeech": flowspeech_models, "heroku": heroku_models, "dashscope": dashscope_models, "qwencloud": qwencloud_models, diff --git a/litellm/llms/flowspeech/text_to_speech/handler.py b/litellm/llms/flowspeech/text_to_speech/handler.py index dff182c82fd..f538302a05c 100644 --- a/litellm/llms/flowspeech/text_to_speech/handler.py +++ b/litellm/llms/flowspeech/text_to_speech/handler.py @@ -13,7 +13,11 @@ from typing import Final, TypeAlias import httpx +from litellm.llms.custom_httpx.http_handler import ( + get_async_httpx_client, # pyright: ignore[reportUnknownVariableType] # unused factory params are untyped upstream +) from litellm.types.llms.openai import HttpxBinaryResponseContent +from litellm.types.utils import LlmProviders from .transformation import ( FlowSpeechErrorResponse, @@ -339,10 +343,10 @@ class FlowSpeechTTS: flow.close() async def arun(self, client: httpx.AsyncClient | None = None) -> HttpxBinaryResponseContent: - if client is None: - async with httpx.AsyncClient(trust_env=False) as owned_client: - return await self._arun_with_deadline(owned_client) - return await self._arun_with_deadline(client) + selected_client: Final = ( + client if client is not None else get_async_httpx_client(LlmProviders.FLOWSPEECH).client + ) + return await self._arun_with_deadline(selected_client) async def _arun_with_deadline(self, client: httpx.AsyncClient) -> HttpxBinaryResponseContent: try: diff --git a/litellm/llms/flowspeech/text_to_speech/transformation.py b/litellm/llms/flowspeech/text_to_speech/transformation.py index 4e27e4eaaac..f53a8915454 100644 --- a/litellm/llms/flowspeech/text_to_speech/transformation.py +++ b/litellm/llms/flowspeech/text_to_speech/transformation.py @@ -231,6 +231,10 @@ class FlowSpeechTextToSpeechConfig(BaseTextToSpeechConfig): def get_complete_url(self, model: str, api_base: str | None, litellm_params: Mapping[str, object]) -> str: base: Final = api_base if api_base is not None else (get_secret_str("FLOWSPEECH_API_BASE") or self.TTS_BASE_URL) url: Final = validate_url(base) - if url.query or url.path.rstrip("/") not in ("", self.TTS_ENDPOINT_PATH): - raise ValueError("FlowSpeech api_base must be an origin or the /api/v1/tts/jobs endpoint") - return str(url.copy_with(path=self.TTS_ENDPOINT_PATH)) + if url.query: + raise ValueError("FlowSpeech api_base must not contain a query") + path: Final = url.path.rstrip("/") + if path.endswith("/api/ai/text-to-speech"): + raise ValueError("FlowSpeech api_base must not use the legacy text-to-speech endpoint") + endpoint: Final = path if path.endswith(self.TTS_ENDPOINT_PATH) else path + self.TTS_ENDPOINT_PATH + return str(url.copy_with(path=endpoint)) diff --git a/litellm/main.py b/litellm/main.py index 694e371afca..59e8591a625 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -36,7 +36,7 @@ if TYPE_CHECKING: import dotenv import httpx import openai -from pydantic import BaseModel, TypeAdapter, ValidationError +from pydantic import BaseModel, StrictStr, TypeAdapter, ValidationError from typing_extensions import assert_never, overload import litellm @@ -8892,21 +8892,27 @@ def speech( if api_key is not None: litellm_params_dict["api_key"] = api_key + flowspeech_params: Final = TypeAdapter(Mapping[str, object]).validate_python(litellm_params_dict) flowspeech_headers: Final = flowspeech_config.validate_environment( - extra_headers or headers or MappingProxyType({}), model, litellm_params_dict.get("api_key"), api_base + TypeAdapter(Mapping[StrictStr, StrictStr]).validate_python( + extra_headers or headers or MappingProxyType({}) + ), + model, + TypeAdapter[str | None](str | None).validate_python(flowspeech_params.get("api_key"), strict=True), + api_base, ) flowspeech_request: Final = flowspeech_config.transform_text_to_speech_request( model=model, input=input, voice=voice if isinstance(voice, str) else None, - optional_params=optional_params, - litellm_params=litellm_params_dict, + optional_params=TypeAdapter(Mapping[str, object]).validate_python(optional_params), + litellm_params=flowspeech_params, headers=flowspeech_headers, ) - flowspeech_url: Final = flowspeech_config.get_complete_url(model, api_base, litellm_params_dict) + flowspeech_url: Final = flowspeech_config.get_complete_url(model, api_base, flowspeech_params) def flowspeech_post_call(job: FlowSpeechJob) -> None: - logging_obj.post_call( + logging_obj.post_call( # pyright: ignore[reportUnknownMemberType] # upstream logging callback has untyped parameters input=input, api_key="", original_response=job.model_dump_json(include={"id", "requestId", "status", "usage"}), @@ -8920,27 +8926,30 @@ def speech( **MappingProxyType( {key: value for key, value in flowspeech_headers.items() if key.lower() != "idempotency-key"} ), - **flowspeech_request["headers"], + **TypeAdapter(Mapping[str, str]).validate_python(flowspeech_request.get("headers"), strict=True), } ), - body=flowspeech_request["dict_body"], - timeout=timeout, + body=TypeAdapter(Mapping[str, object]).validate_python(flowspeech_request.get("dict_body")), + timeout=http_timeout, max_retries=max_retries, on_completed=flowspeech_post_call, ) - logging_obj.pre_call( + logging_obj.pre_call( # pyright: ignore[reportUnknownMemberType] # upstream logging callback has untyped parameters input=input, api_key="", additional_args=MappingProxyType({"api_base": flowspeech_url, "request_id": flowspeech_job.request_id}), ) - flowspeech_client: Final = client.client if isinstance(client, (HTTPHandler, AsyncHTTPHandler)) else client - if flowspeech_client is not None and not isinstance( - flowspeech_client, httpx.AsyncClient if aspeech else httpx.Client - ): - raise ValueError("FlowSpeech client must match the synchronous or asynchronous speech call") - response = ( # rebind-ok: provider branch sets dispatch result - flowspeech_job.arun(flowspeech_client) if aspeech else flowspeech_job.run(flowspeech_client) + flowspeech_client: Final = TypeAdapter(object).validate_python( + client.client if isinstance(client, (HTTPHandler, AsyncHTTPHandler)) else client ) + if aspeech: + if flowspeech_client is not None and not isinstance(flowspeech_client, httpx.AsyncClient): + raise ValueError("FlowSpeech client must match the synchronous or asynchronous speech call") + response = flowspeech_job.arun(flowspeech_client) # rebind-ok: provider branch sets dispatch result + else: + if flowspeech_client is not None and not isinstance(flowspeech_client, httpx.Client): + raise ValueError("FlowSpeech client must match the synchronous or asynchronous speech call") + response = flowspeech_job.run(flowspeech_client) # rebind-ok: provider branch sets dispatch result elif custom_llm_provider == "vertex_ai" or custom_llm_provider == "vertex_ai_beta": from litellm.llms.vertex_ai.text_to_speech.transformation import ( VertexAILyriaTextToSpeechConfig, diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index 55486e26517..fee9feecb73 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -13378,22 +13378,26 @@ async def audio_speech( response_format=requested_format if isinstance(requested_format, str) else None, ) - if isinstance(response, HttpxBinaryResponseContent) and "x-flowspeech-job-id" in response.response.headers: - media_type = "audio/wav" - custom_headers.update( - MappingProxyType( - { - key: response.response.headers[key] - for key in ("x-flowspeech-job-id", "x-flowspeech-request-id") - if key in response.response.headers - } - ) + flowspeech_raw_response: Final = _UNTYPED_VALUE.validate_python( + getattr(_UNTYPED_VALUE.validate_python(response), "response", None) + ) + flowspeech_recovery_headers: Final[Mapping[str, str]] = ( + MappingProxyType( + { + key: flowspeech_raw_response.headers[key] + for key in ("x-flowspeech-job-id", "x-flowspeech-request-id") + if key in flowspeech_raw_response.headers + } ) + if isinstance(flowspeech_raw_response, httpx.Response) + and "x-flowspeech-job-id" in flowspeech_raw_response.headers + else MappingProxyType({}) + ) return StreamingResponse( _audio_speech_chunk_generator(response), media_type=media_type, - headers=custom_headers, + headers={**TypeAdapter(Mapping[str, str]).validate_python(custom_headers), **flowspeech_recovery_headers}, ) except Exception as e: diff --git a/litellm/proxy/public_endpoints/provider_create_fields.json b/litellm/proxy/public_endpoints/provider_create_fields.json index 19ee465c7dc..ce092f26572 100644 --- a/litellm/proxy/public_endpoints/provider_create_fields.json +++ b/litellm/proxy/public_endpoints/provider_create_fields.json @@ -1561,6 +1561,34 @@ ], "default_model_placeholder": "gpt-3.5-turbo" }, + { + "provider": "FlowSpeech", + "provider_display_name": "FlowSpeech", + "litellm_provider": "flowspeech", + "credential_fields": [ + { + "key": "api_key", + "label": "API Key", + "placeholder": null, + "tooltip": null, + "required": true, + "field_type": "password", + "options": null, + "default_value": null + }, + { + "key": "api_base", + "label": "API Base", + "placeholder": "https://flowspeech.io", + "tooltip": "HTTPS origin, trusted gateway prefix, or complete /api/v1/tts/jobs endpoint", + "required": false, + "field_type": "text", + "options": null, + "default_value": null + } + ], + "default_model_placeholder": "flowspeech/flowspeech-tts" + }, { "provider": "FRIENDLIAI", "provider_display_name": "Friendliai", diff --git a/tests/unit/llms/flowspeech/test_handler.py b/tests/unit/llms/flowspeech/test_handler.py index 578aee7e9d4..965f001afa1 100644 --- a/tests/unit/llms/flowspeech/test_handler.py +++ b/tests/unit/llms/flowspeech/test_handler.py @@ -9,8 +9,10 @@ from email.utils import format_datetime import httpx import pytest +from litellm.llms.custom_httpx.http_handler import get_async_httpx_client from litellm.llms.flowspeech.text_to_speech.handler import FlowSpeechTTS from litellm.llms.flowspeech.text_to_speech.transformation import FlowSpeechException +from litellm.types.utils import LlmProviders REQUEST_ID = "b698824b-de05-486d-8833-05d5c4522a39" JOB_ID = "11111111-2222-4333-8444-555555555555" @@ -504,7 +506,10 @@ async def test_client_lifetime(monkeypatch, asynchronous, owned, successful): return client if owned: - monkeypatch.setattr(httpx, "AsyncClient" if asynchronous else "Client", create_client) + if asynchronous: + get_async_httpx_client(LlmProviders.FLOWSPEECH).client = client + else: + monkeypatch.setattr(httpx, "Client", create_client) tts = handler(Clock()) async def call(): @@ -520,7 +525,7 @@ async def test_client_lifetime(monkeypatch, asynchronous, owned, successful): with pytest.raises(FlowSpeechException, match="FlowSpeech job failed"): await call() assert methods == (["POST", "GET"] if successful else ["POST"]) - assert client.is_closed is owned + assert client.is_closed is (owned and not asynchronous) finally: if asynchronous: await client.aclose() @@ -528,6 +533,34 @@ async def test_client_lifetime(monkeypatch, asynchronous, owned, successful): client.close() +@pytest.mark.asyncio +async def test_default_async_client_is_reused_without_download_credentials(): + requests = [] + + def respond(request): + requests.append(request) + if request.method == "POST": + return httpx.Response(202, json=job()) + assert "authorization" not in request.headers + assert "cookie" not in request.headers + assert "x-client-secret" not in request.headers + return httpx.Response(200, content=wav_bytes()) + + cached = get_async_httpx_client(LlmProviders.FLOWSPEECH) + async with httpx.AsyncClient( + transport=httpx.MockTransport(respond), + headers={"Authorization": "discard", "X-Client-Secret": "discard"}, + cookies={"session": "discard"}, + ) as client: + cached.client = client + for _ in range(2): + result = await handler(Clock()).arun() + assert result.content == wav_bytes() + assert not client.is_closed + assert get_async_httpx_client(LlmProviders.FLOWSPEECH).client is client + assert [request.method for request in requests] == ["POST", "GET", "POST", "GET"] + + @pytest.mark.asyncio async def test_async_total_deadline_interrupts_network(): async def respond(request): diff --git a/tests/unit/llms/flowspeech/test_text_to_speech_transformation.py b/tests/unit/llms/flowspeech/test_text_to_speech_transformation.py index 3b11512ea70..4698fefacd3 100644 --- a/tests/unit/llms/flowspeech/test_text_to_speech_transformation.py +++ b/tests/unit/llms/flowspeech/test_text_to_speech_transformation.py @@ -169,11 +169,11 @@ def test_complete_url(base): ("http://example.com", "FlowSpeech requires an HTTPS URL without credentials or fragment"), ("https://u:p@example.com", "FlowSpeech requires an HTTPS URL without credentials or fragment"), ("file:///tmp/test", "FlowSpeech requires an HTTPS URL without credentials or fragment"), - ("https://example.com/?key=x", "FlowSpeech api_base must be an origin or the /api/v1/tts/jobs endpoint"), + ("https://example.com/?key=x", "FlowSpeech api_base must not contain a query"), ("https://example.com/#x", "FlowSpeech requires an HTTPS URL without credentials or fragment"), ( "https://example.com/api/ai/text-to-speech", - "FlowSpeech api_base must be an origin or the /api/v1/tts/jobs endpoint", + "FlowSpeech api_base must not use the legacy text-to-speech endpoint", ), ("", "FlowSpeech requires an HTTPS URL without credentials or fragment"), ], @@ -183,6 +183,19 @@ def test_rejects_invalid_base(base, message): FlowSpeechTextToSpeechConfig().get_complete_url("flowspeech-tts", base, {}) +@pytest.mark.parametrize( + "base,expected", + [ + ("https://gateway.example/p", "https://gateway.example/p/api/v1/tts/jobs"), + ("https://gateway.example/p/", "https://gateway.example/p/api/v1/tts/jobs"), + ("https://gateway.example/p/api/v1/tts/jobs/", "https://gateway.example/p/api/v1/tts/jobs"), + ("https://api/v1/", "https://api/v1/api/v1/tts/jobs"), + ], +) +def test_gateway_base_preserves_prefix_without_duplicating_endpoint(base, expected): + assert FlowSpeechTextToSpeechConfig().get_complete_url("flowspeech-tts", base, {}) == expected + + def test_registers_provider(): from litellm.utils import ProviderConfigManager @@ -191,10 +204,14 @@ def test_registers_provider(): assert model == "flowspeech-tts" assert provider == "flowspeech" assert isinstance(config, FlowSpeechTextToSpeechConfig) + assert "flowspeech/flowspeech-tts" in litellm.model_list + assert litellm.models_by_provider[provider] == litellm.flowspeech_models + assert "flowspeech/flowspeech-tts" in litellm.models_by_provider[provider] @pytest.mark.parametrize("multi", [False, True]) -def test_speech_dispatch(multi): +@pytest.mark.parametrize("timeout_args", [{}, {"timeout": None}, {"timeout": 60}]) +def test_speech_dispatch(multi, timeout_args): requests = [] logs = [] segments = [{"speaker": "A", "text": "Hi", "voiceName": "Kore"}] @@ -229,9 +246,9 @@ def test_speech_dispatch(multi): api_key="test-key", response_format="wav", request_id=REQUEST_ID, - timeout=60, client=HTTPHandler(client=http_client), logger_fn=log_event, + **timeout_args, **({"segments": segments} if multi else {"voice": "Aoede"}), ) assert result.content == wav_bytes() @@ -245,7 +262,8 @@ def test_speech_dispatch(multi): @pytest.mark.asyncio -async def test_aspeech_dispatch(): +@pytest.mark.parametrize("timeout_args", [{}, {"timeout": None}, {"timeout": 60}]) +async def test_aspeech_dispatch(timeout_args): requests = [] logs = [] @@ -268,8 +286,8 @@ async def test_aspeech_dispatch(): input="Hello", api_key="test-key", client=client, - timeout=60, logger_fn=log_event, + **timeout_args, ) assert result.content == wav_bytes() assert len(requests) == 2 @@ -277,3 +295,31 @@ async def test_aspeech_dispatch(): assert json.loads(logs[0][1])["requestId"] == requests[0].headers["idempotency-key"] assert logs[0][2]["job_id"] == JOB_ID assert "test-key" not in repr(logs) and "audio.example.com" not in repr(logs) + + +@pytest.mark.asyncio +@pytest.mark.parametrize("asynchronous", [False, True]) +async def test_dispatch_rejects_mismatched_client_before_sending(asynchronous): + requests = [] + + def respond(request): + requests.append(request) + return httpx.Response(200, json=success()) + + client_type = httpx.Client if asynchronous else httpx.AsyncClient + client = client_type(transport=httpx.MockTransport(respond)) + try: + kwargs = {"model": "flowspeech/flowspeech-tts", "input": "Hello", "api_key": "test-key", "client": client} + message = "client must match the synchronous or asynchronous speech call" + if asynchronous: + with pytest.raises(litellm.APIConnectionError, match=message): + await litellm.aspeech(**kwargs) + else: + with pytest.raises(ValueError, match=message): + litellm.speech(**kwargs) + assert requests == [] + finally: + if isinstance(client, httpx.AsyncClient): + await client.aclose() + else: + client.close() diff --git a/tests/unit/proxy/public_endpoints/test_public_endpoints.py b/tests/unit/proxy/public_endpoints/test_public_endpoints.py index cc61b6f2fcf..c180b348a95 100644 --- a/tests/unit/proxy/public_endpoints/test_public_endpoints.py +++ b/tests/unit/proxy/public_endpoints/test_public_endpoints.py @@ -560,6 +560,24 @@ def test_every_backend_provider_is_listed_in_add_model_or_frozen_as_unlisted(): ) +def test_flowspeech_provider_fields_support_configured_credentials(): + app_instance = FastAPI() + app_instance.include_router(router) + test_client = TestClient(app_instance) + + response = test_client.get("/public/providers/fields") + assert response.status_code == 200 + entry = next(p for p in response.json() if p["litellm_provider"] == "flowspeech") + fields = {field["key"]: field for field in entry["credential_fields"]} + assert entry["provider_display_name"] == "FlowSpeech" + assert entry["default_model_placeholder"] in litellm.models_by_provider["flowspeech"] + assert fields["api_key"]["required"] is True + assert fields["api_key"]["field_type"] == "password" + assert fields["api_base"]["required"] is False + assert fields["api_base"]["field_type"] == "text" + assert fields["api_base"]["placeholder"] == "https://flowspeech.io" + + def test_google_ai_studio_provider_fields_expose_api_base(): """The Google AI Studio (gemini) credential form must let admins set a custom api_base so they can point at a Gemini-compatible gateway (e.g. a self-hosted From f248535545d663b412cf833755bfafe359fb8d75 Mon Sep 17 00:00:00 2001 From: "heng.yin" Date: Mon, 26 Oct 2026 21:14:19 +0800 Subject: [PATCH 08/11] fix: protect FlowSpeech credentials and deadline boundaries --- .../llms/flowspeech/text_to_speech/handler.py | 11 + .../text_to_speech/transformation.py | 40 +++- litellm/main.py | 8 +- .../sdk/test_flowspeech_deadline_wire.py | 213 ++++++++++++++++++ tests/unit/llms/flowspeech/test_handler.py | 96 +++++++- .../test_text_to_speech_transformation.py | 81 +++++++ 6 files changed, 431 insertions(+), 18 deletions(-) create mode 100644 tests/integration/sdk/test_flowspeech_deadline_wire.py diff --git a/litellm/llms/flowspeech/text_to_speech/handler.py b/litellm/llms/flowspeech/text_to_speech/handler.py index f538302a05c..297d35eba4d 100644 --- a/litellm/llms/flowspeech/text_to_speech/handler.py +++ b/litellm/llms/flowspeech/text_to_speech/handler.py @@ -232,9 +232,12 @@ class FlowSpeechTTS: response = yield from self.exchange("GET", f"{self.url}/{job_id}") def completed_response(self, job: FlowSpeechJob, response: httpx.Response) -> HttpxBinaryResponseContent: + self.remaining() result: Final = self.audio_response(response) + self.remaining() if self.on_completed is not None: self.on_completed(job) + self.remaining() return result def audio_response(self, response: httpx.Response) -> HttpxBinaryResponseContent: @@ -280,15 +283,19 @@ class FlowSpeechTTS: return maximum def read(self, client: httpx.Client, request: httpx.Request) -> httpx.Response: + self.remaining() response: Final = client.send(request, stream=True, auth=None, follow_redirects=False) try: + self.remaining() maximum: Final = self.limit(request, response) with io.BytesIO() as content: + self.remaining() for chunk in response.iter_bytes(): self.remaining() if content.tell() + len(chunk) > maximum: raise self.error(502, "FlowSpeech response exceeds size limit") content.write(chunk) + self.remaining() self.remaining() return httpx.Response( response.status_code, headers=response.headers, content=content.getvalue(), request=request @@ -297,15 +304,19 @@ class FlowSpeechTTS: response.close() async def aread(self, client: httpx.AsyncClient, request: httpx.Request) -> httpx.Response: + self.remaining() response: Final = await client.send(request, stream=True, auth=None, follow_redirects=False) try: + self.remaining() maximum: Final = self.limit(request, response) with io.BytesIO() as content: + self.remaining() async for chunk in response.aiter_bytes(): self.remaining() if content.tell() + len(chunk) > maximum: raise self.error(502, "FlowSpeech response exceeds size limit") content.write(chunk) + self.remaining() self.remaining() return httpx.Response( response.status_code, headers=response.headers, content=content.getvalue(), request=request diff --git a/litellm/llms/flowspeech/text_to_speech/transformation.py b/litellm/llms/flowspeech/text_to_speech/transformation.py index f53a8915454..7aac647e4dd 100644 --- a/litellm/llms/flowspeech/text_to_speech/transformation.py +++ b/litellm/llms/flowspeech/text_to_speech/transformation.py @@ -4,7 +4,7 @@ from typing import TYPE_CHECKING, Final, Literal from uuid import UUID, uuid4 import httpx -from pydantic import BaseModel, ConfigDict, Field, TypeAdapter, field_validator +from pydantic import BaseModel, ConfigDict, Field, StrictStr, TypeAdapter, ValidationError, field_validator import litellm from litellm.llms.base_llm.chat.transformation import BaseLLMException @@ -101,6 +101,13 @@ def validate_url(value: str) -> httpx.URL: return url +def credential_headers(value: object) -> Mapping[str, str] | None: + try: + return TypeAdapter(Mapping[StrictStr, StrictStr]).validate_python(value) + except ValidationError: + return None + + class FlowSpeechTextToSpeechConfig(BaseTextToSpeechConfig): TTS_BASE_URL = "https://flowspeech.io" TTS_ENDPOINT_PATH = "/api/v1/tts/jobs" @@ -147,15 +154,27 @@ class FlowSpeechTextToSpeechConfig(BaseTextToSpeechConfig): raise ValueError("FlowSpeech voice must be a nonempty voice name") def validate_environment( - self, headers: Mapping[str, str], model: str, api_key: str | None = None, api_base: str | None = None + self, headers: object, model: str, api_key: object = None, api_base: str | None = None ) -> dict[str, str]: # mutable-ok: base provider interface key: Final = api_key if api_key is not None else (litellm.api_key or get_secret_str("FLOWSPEECH_API_KEY")) - if not key or not key.isascii() or any(char.isspace() or ord(char) < 33 or ord(char) == 127 for char in key): + if ( + not isinstance(key, str) + or not key + or not key.isascii() + or any(char.isspace() or ord(char) < 33 or ord(char) == 127 for char in key) + ): raise ValueError("FlowSpeech API key is required and must be a nonempty token. Set FLOWSPEECH_API_KEY.") - if any(name.lower() in ("cookie", "proxy-authorization", "host", "content-length") for name in headers): + checked_headers: Final = credential_headers(headers) + if checked_headers is None: + raise ValueError("FlowSpeech headers must be a mapping of string names to string values") + if any(name.lower() in ("cookie", "proxy-authorization", "host", "content-length") for name in checked_headers): raise ValueError("FlowSpeech does not accept cookie, proxy credential, host or content-length overrides") return { - **{name: value for name, value in headers.items() if name.lower() not in ("authorization", "content-type")}, + **{ + name: value + for name, value in checked_headers.items() + if name.lower() not in ("authorization", "content-type") + }, "Authorization": f"Bearer {key}", "Content-Type": "application/json", } @@ -231,10 +250,11 @@ class FlowSpeechTextToSpeechConfig(BaseTextToSpeechConfig): def get_complete_url(self, model: str, api_base: str | None, litellm_params: Mapping[str, object]) -> str: base: Final = api_base if api_base is not None else (get_secret_str("FLOWSPEECH_API_BASE") or self.TTS_BASE_URL) url: Final = validate_url(base) - if url.query: + if url.query or b"?" in url.raw_path: raise ValueError("FlowSpeech api_base must not contain a query") - path: Final = url.path.rstrip("/") - if path.endswith("/api/ai/text-to-speech"): + if url.path.rstrip("/").endswith("/api/ai/text-to-speech"): raise ValueError("FlowSpeech api_base must not use the legacy text-to-speech endpoint") - endpoint: Final = path if path.endswith(self.TTS_ENDPOINT_PATH) else path + self.TTS_ENDPOINT_PATH - return str(url.copy_with(path=endpoint)) + path: Final = url.raw_path.rstrip(b"/") + suffix: Final = self.TTS_ENDPOINT_PATH.encode("ascii") + endpoint: Final = path if path.endswith(suffix) else path + suffix + return str(url.copy_with(raw_path=endpoint)) diff --git a/litellm/main.py b/litellm/main.py index 59e8591a625..2a84c76e43b 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -36,7 +36,7 @@ if TYPE_CHECKING: import dotenv import httpx import openai -from pydantic import BaseModel, StrictStr, TypeAdapter, ValidationError +from pydantic import BaseModel, TypeAdapter, ValidationError from typing_extensions import assert_never, overload import litellm @@ -8894,11 +8894,9 @@ def speech( flowspeech_params: Final = TypeAdapter(Mapping[str, object]).validate_python(litellm_params_dict) flowspeech_headers: Final = flowspeech_config.validate_environment( - TypeAdapter(Mapping[StrictStr, StrictStr]).validate_python( - extra_headers or headers or MappingProxyType({}) - ), + TypeAdapter(object).validate_python(extra_headers or headers or MappingProxyType({})), model, - TypeAdapter[str | None](str | None).validate_python(flowspeech_params.get("api_key"), strict=True), + flowspeech_params.get("api_key"), api_base, ) flowspeech_request: Final = flowspeech_config.transform_text_to_speech_request( diff --git a/tests/integration/sdk/test_flowspeech_deadline_wire.py b/tests/integration/sdk/test_flowspeech_deadline_wire.py new file mode 100644 index 00000000000..60dd0806c57 --- /dev/null +++ b/tests/integration/sdk/test_flowspeech_deadline_wire.py @@ -0,0 +1,213 @@ +import asyncio +import io +import ipaddress +import json +import ssl +import threading +import time +import wave +from concurrent.futures import ThreadPoolExecutor +from datetime import datetime, timezone +from http.server import BaseHTTPRequestHandler, HTTPServer + +import httpx +import pytest +from cryptography import x509 +from cryptography.hazmat.primitives import hashes, serialization +from cryptography.hazmat.primitives.asymmetric import ec +from cryptography.x509.oid import NameOID + +import litellm +from litellm.llms.flowspeech.text_to_speech.transformation import FlowSpeechException + +REQUEST_ID = "b698824b-de05-486d-8833-05d5c4522a39" +JOB_ID = "11111111-2222-4333-8444-555555555555" +BUDGET = 0.3 + + +def wav_bytes(): + buffer = io.BytesIO() + with wave.open(buffer, "wb") as audio: + audio.setparams((1, 2, 24000, 0, "NONE", "not compressed")) + audio.writeframes(b"\x00\x00" * 24) + return buffer.getvalue() + + +def tls_contexts(directory): + key = ec.generate_private_key(ec.SECP256R1()) + name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "localhost")]) + certificate = ( + x509.CertificateBuilder() + .subject_name(name) + .issuer_name(name) + .public_key(key.public_key()) + .serial_number(1) + .not_valid_before(datetime(2020, 1, 1, tzinfo=timezone.utc)) + .not_valid_after(datetime(2100, 1, 1, tzinfo=timezone.utc)) + .add_extension(x509.SubjectAlternativeName([x509.IPAddress(ipaddress.ip_address("127.0.0.1"))]), False) + .sign(key, hashes.SHA256()) + ) + cert_file = directory / "loopback.pem" + key_file = directory / "loopback.key" + cert_file.write_bytes(certificate.public_bytes(serialization.Encoding.PEM)) + key_file.write_bytes( + key.private_bytes(serialization.Encoding.PEM, serialization.PrivateFormat.PKCS8, serialization.NoEncryption()) + ) + server_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) + server_context.load_cert_chain(cert_file, key_file) + return server_context, ssl.create_default_context(cafile=str(cert_file)) + + +@pytest.fixture +def slow_speech_peer(tmp_path, scenario): + requests = [] + body = wav_bytes() + server_context, client_context = tls_contexts(tmp_path) + + class Handler(BaseHTTPRequestHandler): + protocol_version = "HTTP/1.1" + + def setup(self): + super().setup() + self.connection.settimeout(2) + + def log_message(self, format, *args): + return + + def reply(self, content): + self.send_response(200) + self.send_header("Content-Length", str(len(content))) + self.end_headers() + self.wfile.write(content) + self.wfile.flush() + + def do_POST(self): + request_body = self.rfile.read(int(self.headers["Content-Length"])) + requests.append((self.command, self.path, dict(self.headers), json.loads(request_body))) + self.reply( + json.dumps( + { + "id": JOB_ID, + "requestId": self.headers["Idempotency-Key"], + "status": "success", + "usage": {"billableCharacters": 5, "reservedCredits": 0, "chargedCredits": 5}, + "result": { + "url": f"{base_url}/audio.wav", + "mimeType": "audio/wav", + "sampleRate": 24000, + "channels": 1, + "bitsPerSample": 16, + }, + } + ).encode() + ) + + def do_GET(self): + requests.append((self.command, self.path, dict(self.headers), None)) + if self.path == "/health": + self.reply(b"ok") + return + try: + if scenario == "slow_headers_body": + time.sleep(0.2) + self.send_response(200) + self.send_header("Content-Length", str(len(body))) + self.end_headers() + self.wfile.flush() + time.sleep(0.2) + self.wfile.write(body) + elif scenario == "slow_header_progress": + self.wfile.write(b"HTTP/1.1 200 OK\r\nX-Progress: ") + for _ in range(10): + self.wfile.write(b"x") + self.wfile.flush() + time.sleep(0.05) + self.wfile.write(f"\r\nContent-Length: {len(body)}\r\n\r\n".encode() + body) + else: + self.send_response(200) + self.send_header("Content-Length", str(len(body))) + self.end_headers() + for offset in range(0, len(body), 8): + time.sleep(0.05) + self.wfile.write(body[offset : offset + 8]) + self.wfile.flush() + self.wfile.flush() + except (BrokenPipeError, ConnectionResetError, ssl.SSLEOFError): + self.close_connection = True + + server = HTTPServer(("127.0.0.1", 0), Handler) + server.socket = server_context.wrap_socket(server.socket, server_side=True) + base_url = f"https://127.0.0.1:{server.server_port}" + worker = threading.Thread(target=server.serve_forever, kwargs={"poll_interval": 0.02}) + worker.start() + try: + yield base_url, client_context, requests + finally: + server.shutdown() + server.server_close() + worker.join(timeout=3) + assert not worker.is_alive() + + +@pytest.mark.asyncio +@pytest.mark.parametrize("mode", ["sync", "worker", "async"]) +@pytest.mark.parametrize("scenario", ["slow_headers_body", "slow_body_progress", "slow_header_progress"]) +async def test_cooperative_deadline_rejects_late_audio_without_retry_or_client_closure(slow_speech_peer, mode): + base_url, client_context, requests = slow_speech_peer + responses = [] + completed = [] + + def log_event(details): + if details.get("log_event_type") == "post_api_call": + completed.append(details) + + async def record_response(response): + responses.append(response) + + kwargs = { + "model": "flowspeech/flowspeech-tts", + "input": "Hello", + "api_key": "synthetic-loopback-only", + "api_base": base_url, + "request_id": REQUEST_ID, + "timeout": BUDGET, + "max_retries": 2, + "logger_fn": log_event, + } + options = { + "verify": client_context, + "trust_env": False, + "limits": httpx.Limits(max_connections=1, max_keepalive_connections=1), + } + if mode == "async": + async with httpx.AsyncClient(**options, event_hooks={"response": [record_response]}) as client: + with pytest.raises(litellm.Timeout, match="total deadline") as error: + await asyncio.wait_for(litellm.aspeech(**kwargs, client=client), timeout=5) + assert not client.is_closed + assert responses and all(response.is_closed for response in responses) + assert (await client.get(f"{base_url}/health", timeout=3)).content == b"ok" + else: + with httpx.Client(**options, event_hooks={"response": [responses.append]}) as client: + if mode == "worker": + with ThreadPoolExecutor(max_workers=1) as pool: + result = pool.submit(litellm.speech, **kwargs, client=client) + with pytest.raises(FlowSpeechException, match="total deadline") as error: + result.result(timeout=5) + else: + with pytest.raises(FlowSpeechException, match="total deadline") as error: + litellm.speech(**kwargs, client=client) + assert not client.is_closed + assert responses and all(response.is_closed for response in responses) + assert client.get(f"{base_url}/health", timeout=3).content == b"ok" + + assert REQUEST_ID in str(error.value) + assert JOB_ID in str(error.value) + assert [(method, path) for method, path, _, _ in requests] == [ + ("POST", "/api/v1/tts/jobs"), + ("GET", "/audio.wav"), + ("GET", "/health"), + ] + assert httpx.Headers(requests[0][2])["Idempotency-Key"] == REQUEST_ID + assert requests[0][3] == {"input": {"mode": "single", "text": "Hello", "voiceName": "Kore"}} + assert "authorization" not in httpx.Headers(requests[1][2]) + assert completed == [] diff --git a/tests/unit/llms/flowspeech/test_handler.py b/tests/unit/llms/flowspeech/test_handler.py index 965f001afa1..2a42f9210da 100644 --- a/tests/unit/llms/flowspeech/test_handler.py +++ b/tests/unit/llms/flowspeech/test_handler.py @@ -11,7 +11,7 @@ import pytest from litellm.llms.custom_httpx.http_handler import get_async_httpx_client from litellm.llms.flowspeech.text_to_speech.handler import FlowSpeechTTS -from litellm.llms.flowspeech.text_to_speech.transformation import FlowSpeechException +from litellm.llms.flowspeech.text_to_speech.transformation import FlowSpeechException, FlowSpeechTextToSpeechConfig from litellm.types.utils import LlmProviders REQUEST_ID = "b698824b-de05-486d-8833-05d5c4522a39" @@ -81,9 +81,9 @@ class Clock: await asyncio.sleep(0) -def handler(clock, timeout=120, **kwargs): +def handler(clock, timeout=120, url=URL, **kwargs): return FlowSpeechTTS( - URL, + url, { "Authorization": "Bearer test-key", "Idempotency-Key": REQUEST_ID, @@ -154,6 +154,29 @@ def test_all_pending_states_and_uncredentialed_download(invoke): assert requests[-1].extensions["timeout"]["read"] == 100 +@pytest.mark.parametrize("prefix", ["/p%2Fq", "/p%2fq", "/p%252Fq", "/p%3Fq", "/p%23q"]) +def test_encoded_gateway_prefix_survives_submission_and_polling(invoke, prefix): + requests = [] + endpoint = f"{prefix}/api/v1/tts/jobs" + url = FlowSpeechTextToSpeechConfig().get_complete_url("flowspeech-tts", f"https://gateway.example{prefix}/", {}) + + def respond(request): + requests.append(request) + if request.method == "POST": + return httpx.Response(202, json=job("queued")) + if request.url.host == "gateway.example": + return httpx.Response(200, json=job()) + return httpx.Response(200, content=wav_bytes()) + + assert invoke(handler(Clock(), url=url), respond).content == wav_bytes() + assert [(request.method, request.url.raw_path) for request in requests] == [ + ("POST", endpoint.encode()), + ("GET", f"{endpoint}/{JOB_ID}".encode()), + ("GET", b"/recording.wav"), + ] + assert requests[0].headers["Idempotency-Key"] == REQUEST_ID + + @pytest.mark.parametrize("failure", ["network", "server", "rate_limit"]) def test_create_retry_preserves_uuid_and_body(invoke, failure): clock = Clock() @@ -427,6 +450,73 @@ def test_deadline_during_request(invoke): invoke(handler(clock, timeout=10), respond) +@pytest.mark.parametrize("stage", ["submission", "download"]) +@pytest.mark.parametrize("header_delay,body_delay,expected_reads", [(0.125, 0, 0), (0.075, 0.075, 1)]) +def test_expired_stream_is_closed_without_more_work(invoke, stage, header_delay, body_delay, expected_reads): + clock = Clock() + completed = [] + requests = [] + + class Stream(httpx.SyncByteStream, httpx.AsyncByteStream): + def __init__(self): + self.reads = 0 + self.closed = False + + def __iter__(self): + self.reads += 1 + clock.now += body_delay + yield wav_bytes() if stage == "download" else json.dumps(job()).encode() + pytest.fail("must not advance a stream after its budget expires") + + async def __aiter__(self): + for chunk in self: + yield chunk + + def close(self): + self.closed = True + + async def aclose(self): + self.close() + + stream = Stream() + + def respond(request): + requests.append(request) + if stage == "download" and request.method == "POST": + return httpx.Response(202, json=job()) + clock.now += header_delay + return httpx.Response(200, stream=stream) + + with pytest.raises(FlowSpeechException, match="total deadline") as error: + invoke(handler(clock, timeout=0.1, on_completed=completed.append), respond) + assert error.value.status_code == 408 + assert REQUEST_ID in str(error.value) + assert f"job_id={JOB_ID if stage == 'download' else 'unknown'}" in str(error.value) + assert stream.reads == expected_reads + assert stream.closed + assert [request.method for request in requests] == (["POST", "GET"] if stage == "download" else ["POST"]) + assert clock.sleeps == [] + assert completed == [] + + +def test_deadline_after_wav_validation_prevents_success_callback(invoke, monkeypatch): + clock = Clock() + completed = [] + content = wav_bytes() + open_wav = wave.open + + def delayed_open(*args, **kwargs): + clock.now = 11 + return open_wav(*args, **kwargs) + + monkeypatch.setattr(wave, "open", delayed_open) + responses = iter([httpx.Response(202, json=job()), httpx.Response(200, content=content)]) + with pytest.raises(FlowSpeechException, match="total deadline"): + invoke(handler(clock, timeout=10, on_completed=completed.append), lambda request: next(responses)) + assert completed == [] + assert clock.sleeps == [] + + @pytest.mark.parametrize("timeout", [0, -1, float("inf"), float("nan"), httpx.Timeout(None)]) def test_invalid_timeout(timeout): with pytest.raises(ValueError, match="timeout"): diff --git a/tests/unit/llms/flowspeech/test_text_to_speech_transformation.py b/tests/unit/llms/flowspeech/test_text_to_speech_transformation.py index 4698fefacd3..6e8e75995cc 100644 --- a/tests/unit/llms/flowspeech/test_text_to_speech_transformation.py +++ b/tests/unit/llms/flowspeech/test_text_to_speech_transformation.py @@ -1,5 +1,7 @@ import io import json +import logging +import traceback import wave from uuid import UUID @@ -7,11 +9,27 @@ import httpx import pytest import litellm +from litellm.integrations.custom_logger import CustomLogger from litellm.llms.custom_httpx.http_handler import HTTPHandler from litellm.llms.flowspeech.text_to_speech.transformation import FlowSpeechTextToSpeechConfig REQUEST_ID = "b698824b-de05-486d-8833-05d5c4522a39" JOB_ID = "11111111-2222-4333-8444-555555555555" +SECRET_SENTINEL = "credential-value-must-not-appear-672911" + + +class FailureRecorder(CustomLogger): + def __init__(self): + super().__init__() + self.errors = [] + + def log_failure_event(self, kwargs, response_obj, start_time, end_time): + self.errors.append( + (str(kwargs["exception"]), kwargs["traceback_exception"], kwargs["standard_logging_object"]["error_str"]) + ) + + async def async_log_failure_event(self, kwargs, response_obj, start_time, end_time): + self.log_failure_event(kwargs, response_obj, start_time, end_time) def wav_bytes(): @@ -170,6 +188,7 @@ def test_complete_url(base): ("https://u:p@example.com", "FlowSpeech requires an HTTPS URL without credentials or fragment"), ("file:///tmp/test", "FlowSpeech requires an HTTPS URL without credentials or fragment"), ("https://example.com/?key=x", "FlowSpeech api_base must not contain a query"), + ("https://example.com/p?", "FlowSpeech api_base must not contain a query"), ("https://example.com/#x", "FlowSpeech requires an HTTPS URL without credentials or fragment"), ( "https://example.com/api/ai/text-to-speech", @@ -196,6 +215,68 @@ def test_gateway_base_preserves_prefix_without_duplicating_endpoint(base, expect assert FlowSpeechTextToSpeechConfig().get_complete_url("flowspeech-tts", base, {}) == expected +@pytest.mark.parametrize("prefix", ["/p%2Fq", "/p%2fq", "/p%252Fq", "/p%3Fq", "/p%23q"]) +@pytest.mark.parametrize("suffix", ["/", "/api/v1/tts/jobs", "/api/v1/tts/jobs/"]) +def test_gateway_prefix_preserves_encoded_path(prefix, suffix): + url = FlowSpeechTextToSpeechConfig().get_complete_url("flowspeech-tts", f"https://gateway.example{prefix}{suffix}", {}) + assert httpx.URL(url).raw_path == f"{prefix}/api/v1/tts/jobs".encode("ascii") + assert httpx.URL(url).query == b"" + assert httpx.URL(url).fragment == "" + + +@pytest.mark.parametrize( + "headers", + [SECRET_SENTINEL, {SECRET_SENTINEL.encode(): "invalid"}, {"Authorization": {"value": SECRET_SENTINEL}}], +) +def test_invalid_headers_raise_no_input_bearing_exception_chain(headers): + with pytest.raises(ValueError, match="FlowSpeech headers must be a mapping") as error: + FlowSpeechTextToSpeechConfig().validate_environment(headers, "flowspeech-tts", "test-key") + assert error.value.__context__ is None + assert SECRET_SENTINEL not in "".join(traceback.format_exception(error.value)) + + +@pytest.mark.asyncio +@pytest.mark.parametrize("asynchronous", [False, True]) +@pytest.mark.parametrize( + "invalid,message", + [ + ({"api_key": SECRET_SENTINEL.encode()}, "FlowSpeech API key is required"), + ({"api_key": {"value": SECRET_SENTINEL}}, "FlowSpeech API key is required"), + ({"headers": {"Authorization": SECRET_SENTINEL.encode()}}, "FlowSpeech headers must be a mapping"), + ({"headers": {"Authorization": {"value": SECRET_SENTINEL}}}, "FlowSpeech headers must be a mapping"), + ({"extra_headers": {"Authorization": SECRET_SENTINEL.encode()}}, "FlowSpeech headers must be a mapping"), + ({"extra_headers": {"Authorization": {"value": SECRET_SENTINEL}}}, "FlowSpeech headers must be a mapping"), + ], +) +async def test_invalid_credentials_never_reach_errors_or_logs(asynchronous, invalid, message, monkeypatch, caplog, capsys): + caplog.set_level(logging.DEBUG, logger="LiteLLM") + recorder = FailureRecorder() + monkeypatch.setattr(litellm, "failure_callback", [recorder]) + monkeypatch.setattr(litellm, "_async_failure_callback", [recorder]) + requests = [] + + def respond(request): + requests.append(request) + return httpx.Response(200, json=success()) + + kwargs = {"model": "flowspeech/flowspeech-tts", "input": "Hello", "api_key": "test-key", **invalid} + if asynchronous: + async with httpx.AsyncClient(transport=httpx.MockTransport(respond)) as client: + with pytest.raises(litellm.APIConnectionError, match=message) as error: + await litellm.aspeech(**kwargs, client=client) + else: + with httpx.Client(transport=httpx.MockTransport(respond)) as client: + with pytest.raises(ValueError, match=message) as error: + litellm.speech(**kwargs, client=client) + captured = capsys.readouterr() + assert requests == [] + assert recorder.errors + assert SECRET_SENTINEL not in str(error.value) + assert SECRET_SENTINEL not in "".join(traceback.format_exception(error.value)) + assert SECRET_SENTINEL not in repr(recorder.errors) + assert SECRET_SENTINEL not in caplog.text + captured.out + captured.err + + def test_registers_provider(): from litellm.utils import ProviderConfigManager From 278dc554cc74fd5f36425fb9d2d4f3c5e494a3df Mon Sep 17 00:00:00 2001 From: "heng.yin" Date: Tue, 27 Oct 2026 12:57:44 +0800 Subject: [PATCH 09/11] fix: redact positional credentials and malformed request headers --- .../sensitive_data_masker.py | 46 ++++- litellm/utils.py | 43 ++++- .../test_sensitive_data_masker.py | 83 +++++++++ .../test_text_to_speech_transformation.py | 175 +++++++++++++++++- tests/unit/test_utils.py | 119 ++++++++++++ 5 files changed, 457 insertions(+), 9 deletions(-) diff --git a/litellm/litellm_core_utils/sensitive_data_masker.py b/litellm/litellm_core_utils/sensitive_data_masker.py index 8c402fe9b44..6bc411e2319 100644 --- a/litellm/litellm_core_utils/sensitive_data_masker.py +++ b/litellm/litellm_core_utils/sensitive_data_masker.py @@ -1,7 +1,7 @@ from collections.abc import ItemsView, Mapping, Sequence from collections.abc import Set as AbstractSet from dataclasses import dataclass, field -from typing import Any, Final +from typing import Any, Final, cast from pydantic import BaseModel @@ -255,6 +255,50 @@ def mask_sensitive_keys(data: Mapping[str, object], sensitive_fields: set[str]) return masked +_REQUEST_LOG_HEADER_NAMES: Final = frozenset( + ( + "accept", + "accept-encoding", + "authorization", + "proxy-authorization", + "cookie", + "set-cookie", + "api-key", + "x-api-key", + "x-goog-api-key", + "content-type", + "content-length", + "user-agent", + "idempotency-key", + "x-request-id", + "x-correlation-id", + ) +) + + +def _redact_request_headers(value: object) -> object: + if value is None: + return None + if not isinstance(value, Mapping): + return REDACTED + headers: Final = cast(Mapping[object, object], value) # cast-ok: Mapping checked above, keys remain untrusted + if any( + not isinstance(name, str) or name.lower() not in _REQUEST_LOG_HEADER_NAMES or not isinstance(item, str) + for name, item in headers.items() + ): + return REDACTED + return headers + + +def redact_credentials_in_request(data: Mapping[str, object]) -> Mapping[str, object]: + return redact_credentials_in_payload( + { + key: _redact_request_headers(value) if key in ("headers", "extra_headers") else value + for key, value in data.items() + } + ) + + def redact_credentials_in_payload(data: Mapping[str, object]) -> Mapping[str, object]: """Return a copy of ``data`` where every value under a credential-named key is replaced by the shared ``REDACTED`` marker, nested mappings are recursed into, diff --git a/litellm/utils.py b/litellm/utils.py index da1bdf01ae8..a403300fe93 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -94,7 +94,11 @@ from litellm.litellm_core_utils.fallback_generalizations import ( from litellm.litellm_core_utils.get_model_cost_map import AliasedCostEntry from litellm.litellm_core_utils.model_param_helper import peek_reasoning_summary_aliases from litellm.litellm_core_utils.offline_mode import is_offline_mode -from litellm.litellm_core_utils.sensitive_data_masker import redact_credentials_in_payload +from litellm.litellm_core_utils.secret_redaction import REDACTED +from litellm.litellm_core_utils.sensitive_data_masker import ( + redact_credentials_in_payload, + redact_credentials_in_request, +) from litellm.litellm_core_utils.tokenizer import strip_special_tokens from litellm.litellm_core_utils.tool_fields import MalformedField, resolve_deployment_tool_fields from litellm.rust_bridge import tokenizer as tokenizer_dispatch @@ -8537,7 +8541,38 @@ def get_valid_models( return [] # NON-Blocking -def print_args_passed_to_litellm(original_function, args, kwargs): +def _request_logging_parameters(original_function: Callable[..., object]) -> tuple[str, ...]: + parameters: Final = inspect.signature(original_function).parameters.values() + positional: Final = tuple( + parameter.name + for parameter in parameters + if parameter.kind in (inspect.Parameter.POSITIONAL_ONLY, inspect.Parameter.POSITIONAL_OR_KEYWORD) + ) + if positional or not inspect.iscoroutinefunction(original_function): + return positional + if original_function.__name__.startswith("a"): + synchronous: Final = getattr(inspect.getmodule(original_function), original_function.__name__[1:], None) + if callable(synchronous) and not inspect.iscoroutinefunction(synchronous): + return _request_logging_parameters(synchronous) + return () + + +def _redact_positional_request_args( + original_function: Callable[..., object], args: tuple[object, ...] +) -> tuple[object, ...]: + if not args: + return () + try: + names: Final = _request_logging_parameters(original_function) + except (TypeError, ValueError): + return (REDACTED,) * len(args) + redacted: Final = redact_credentials_in_request(dict(zip(names, args))) + return tuple(redacted[name] for name in names[: len(args)]) + (REDACTED,) * max(0, len(args) - len(names)) + + +def print_args_passed_to_litellm( + original_function: Callable[..., object], args: tuple[object, ...], kwargs: Mapping[str, object] +) -> None: if not is_debugging_on(): return try: @@ -8557,8 +8592,8 @@ def print_args_passed_to_litellm(original_function, args, kwargs): ): return - args_str: Final = ", ".join(map(repr, args)) - redacted_kwargs: Final = redact_credentials_in_payload(kwargs) + args_str: Final = ", ".join(map(repr, _redact_positional_request_args(original_function, args))) + redacted_kwargs: Final = redact_credentials_in_request(kwargs) kwargs_str: Final = ", ".join(f"{key}={value!r}" for key, value in redacted_kwargs.items()) print_verbose( "\n", diff --git a/tests/unit/litellm_core_utils/test_sensitive_data_masker.py b/tests/unit/litellm_core_utils/test_sensitive_data_masker.py index 2182620164c..86010a79982 100644 --- a/tests/unit/litellm_core_utils/test_sensitive_data_masker.py +++ b/tests/unit/litellm_core_utils/test_sensitive_data_masker.py @@ -3,13 +3,96 @@ Unit tests for SensitiveDataMasker - List Preservation """ from functools import reduce +from types import MappingProxyType from typing import Final +import httpx import pytest from litellm.litellm_core_utils.sensitive_data_masker import SensitiveDataMasker +@pytest.mark.parametrize("field", ["headers", "extra_headers"]) +@pytest.mark.parametrize( + "malformed", + [ + "synthetic-header-secret", + b"synthetic-header-secret", + [("Authorization", "synthetic-header-secret")], + {b"synthetic-header-secret": "unused"}, + {123: "synthetic-header-secret"}, + {"Bearer synthetic-header-secret": "unused"}, + {"synthetic-header-secret\r\n": "unused"}, + {"": "synthetic-header-secret"}, + {"\u2603": "synthetic-header-secret"}, + {"SYNTHETIC_KEY": "Bearer synthetic-header-secret"}, + {"q8m4p9x2": "synthetic-header-secret"}, + {"X-Request-Id": b"synthetic-header-secret"}, + {"X-Request-Id": {"value": "synthetic-header-secret"}}, + {"X-Request-Id": ["synthetic-header-secret"]}, + ], +) +def test_request_redaction_hides_malformed_headers_only(field: str, malformed: object) -> None: + from litellm.litellm_core_utils.sensitive_data_masker import redact_credentials_in_request + + payload: Final = {field: malformed, "model": "synthetic-model", "temperature": 0.25} + assert redact_credentials_in_request(payload) == { + field: "REDACTED", + "model": "synthetic-model", + "temperature": 0.25, + } + assert payload[field] is malformed + + +@pytest.mark.parametrize("factory", [dict, MappingProxyType, httpx.Headers]) +@pytest.mark.parametrize("field", ["headers", "extra_headers"]) +def test_request_redaction_preserves_normal_headers_and_tool_schema(factory, field: str) -> None: + from litellm.litellm_core_utils.sensitive_data_masker import redact_credentials_in_request + + headers: Final = factory( + { + "authorization": "synthetic-header-secret", + "cookie": "synthetic-cookie", + "x-request-id": "safe-correlation", + "content-type": "application/json", + } + ) + schema: Final = { + "type": "object", + "properties": {"headers": {"type": "string"}, "extra_headers": {"type": "array"}}, + } + result: Final = redact_credentials_in_request({field: headers, "tools": [{"parameters": schema}]}) + assert result == { + field: { + "authorization": "REDACTED", + "cookie": "REDACTED", + "x-request-id": "safe-correlation", + "content-type": "application/json", + }, + "tools": [{"parameters": schema}], + } + assert headers["authorization"] == "synthetic-header-secret" + + +@pytest.mark.parametrize("headers", [None, {}]) +def test_request_redaction_preserves_unset_headers(headers) -> None: + from litellm.litellm_core_utils.sensitive_data_masker import redact_credentials_in_request + + assert redact_credentials_in_request({"headers": headers}) == {"headers": headers} + + +def test_request_redaction_never_renders_non_string_header_values() -> None: + from litellm.litellm_core_utils.sensitive_data_masker import redact_credentials_in_request + + class SensitiveHeaderValue: + def __repr__(self) -> str: + pytest.fail("A non-string header value must not be rendered") + + assert redact_credentials_in_request({"headers": {"X-Request-Id": SensitiveHeaderValue()}}) == { + "headers": "REDACTED" + } + + def test_lists_are_preserved_not_converted_to_strings(): """ Regression test: Ensure lists are preserved as JSON arrays, not converted to strings. diff --git a/tests/unit/llms/flowspeech/test_text_to_speech_transformation.py b/tests/unit/llms/flowspeech/test_text_to_speech_transformation.py index 6e8e75995cc..c60ba4bff82 100644 --- a/tests/unit/llms/flowspeech/test_text_to_speech_transformation.py +++ b/tests/unit/llms/flowspeech/test_text_to_speech_transformation.py @@ -3,6 +3,7 @@ import json import logging import traceback import wave +from typing import Final from uuid import UUID import httpx @@ -11,13 +12,26 @@ import pytest import litellm from litellm.integrations.custom_logger import CustomLogger from litellm.llms.custom_httpx.http_handler import HTTPHandler -from litellm.llms.flowspeech.text_to_speech.transformation import FlowSpeechTextToSpeechConfig +from litellm.llms.flowspeech.text_to_speech.transformation import FlowSpeechException, FlowSpeechTextToSpeechConfig REQUEST_ID = "b698824b-de05-486d-8833-05d5c4522a39" JOB_ID = "11111111-2222-4333-8444-555555555555" SECRET_SENTINEL = "credential-value-must-not-appear-672911" +def assert_clean_exception_chain(error: BaseException | None, seen: frozenset[int] = frozenset()) -> None: + if error is None or id(error) in seen: + return + assert SECRET_SENTINEL not in repr(error.args) + assert SECRET_SENTINEL not in repr(vars(error)) + assert SECRET_SENTINEL not in "".join(traceback.format_exception(error)) + request: Final = getattr(error, "request", None) + if isinstance(request, httpx.Request): + assert SECRET_SENTINEL not in str(request.url) + repr(request.headers) + repr(request.content) + assert_clean_exception_chain(error.__cause__, seen | {id(error)}) + assert_clean_exception_chain(error.__context__, seen | {id(error)}) + + class FailureRecorder(CustomLogger): def __init__(self): super().__init__() @@ -237,6 +251,7 @@ def test_invalid_headers_raise_no_input_bearing_exception_chain(headers): @pytest.mark.asyncio @pytest.mark.parametrize("asynchronous", [False, True]) +@pytest.mark.parametrize("positional", [False, True]) @pytest.mark.parametrize( "invalid,message", [ @@ -246,9 +261,25 @@ def test_invalid_headers_raise_no_input_bearing_exception_chain(headers): ({"headers": {"Authorization": {"value": SECRET_SENTINEL}}}, "FlowSpeech headers must be a mapping"), ({"extra_headers": {"Authorization": SECRET_SENTINEL.encode()}}, "FlowSpeech headers must be a mapping"), ({"extra_headers": {"Authorization": {"value": SECRET_SENTINEL}}}, "FlowSpeech headers must be a mapping"), + ({"headers": SECRET_SENTINEL}, "FlowSpeech headers must be a mapping"), + ({"extra_headers": SECRET_SENTINEL}, "FlowSpeech headers must be a mapping"), + ({"headers": {SECRET_SENTINEL.encode(): "unused"}}, "FlowSpeech headers must be a mapping"), + ({"extra_headers": {SECRET_SENTINEL.encode(): "unused"}}, "FlowSpeech headers must be a mapping"), + ({"headers": {"X-Correlation-Id": SECRET_SENTINEL.encode()}}, "FlowSpeech headers must be a mapping"), + ({"headers": {"X-Correlation-Id": {"value": SECRET_SENTINEL}}}, "FlowSpeech headers must be a mapping"), + ({"extra_headers": {"X-Correlation-Id": SECRET_SENTINEL.encode()}}, "FlowSpeech headers must be a mapping"), + ({"extra_headers": {"X-Correlation-Id": {"value": SECRET_SENTINEL}}}, "FlowSpeech headers must be a mapping"), ], ) -async def test_invalid_credentials_never_reach_errors_or_logs(asynchronous, invalid, message, monkeypatch, caplog, capsys): +async def test_invalid_credentials_never_reach_errors_or_logs( + asynchronous, + positional, + invalid, + message, + monkeypatch, + caplog, + capsys, +): caplog.set_level(logging.DEBUG, logger="LiteLLM") recorder = FailureRecorder() monkeypatch.setattr(litellm, "failure_callback", [recorder]) @@ -260,14 +291,20 @@ async def test_invalid_credentials_never_reach_errors_or_logs(asynchronous, inva return httpx.Response(200, json=success()) kwargs = {"model": "flowspeech/flowspeech-tts", "input": "Hello", "api_key": "test-key", **invalid} + args: Final = (kwargs["model"], kwargs["input"], None, kwargs["api_key"]) if positional else () + call_kwargs: Final = ( + {key: value for key, value in kwargs.items() if key not in ("model", "input", "api_key")} + if positional + else kwargs + ) if asynchronous: async with httpx.AsyncClient(transport=httpx.MockTransport(respond)) as client: with pytest.raises(litellm.APIConnectionError, match=message) as error: - await litellm.aspeech(**kwargs, client=client) + await litellm.aspeech(*args, **call_kwargs, client=client) else: with httpx.Client(transport=httpx.MockTransport(respond)) as client: with pytest.raises(ValueError, match=message) as error: - litellm.speech(**kwargs, client=client) + litellm.speech(*args, **call_kwargs, client=client) captured = capsys.readouterr() assert requests == [] assert recorder.errors @@ -275,6 +312,136 @@ async def test_invalid_credentials_never_reach_errors_or_logs(asynchronous, inva assert SECRET_SENTINEL not in "".join(traceback.format_exception(error.value)) assert SECRET_SENTINEL not in repr(recorder.errors) assert SECRET_SENTINEL not in caplog.text + captured.out + captured.err + assert_clean_exception_chain(error.value) + + +@pytest.mark.asyncio +@pytest.mark.parametrize("asynchronous", [False, True]) +@pytest.mark.parametrize("positional", [False, True]) +@pytest.mark.parametrize("successful", [False, True]) +@pytest.mark.parametrize("verbose", [False, True]) +@pytest.mark.parametrize("header_field", ["headers", "extra_headers"]) +@pytest.mark.parametrize("header_name", ["Authorization", SECRET_SENTINEL]) +async def test_request_log_redaction_preserves_speech_transport( + asynchronous: bool, + positional: bool, + successful: bool, + verbose: bool, + header_field: str, + header_name: str, + monkeypatch: pytest.MonkeyPatch, + caplog: pytest.LogCaptureFixture, + capsys: pytest.CaptureFixture[str], +) -> None: + monkeypatch.setattr(litellm, "set_verbose", verbose) + monkeypatch.setattr("litellm._logging.set_verbose", verbose) + caplog.set_level(logging.WARNING if verbose else logging.DEBUG, logger="LiteLLM") + headers: Final = {header_name: SECRET_SENTINEL, "X-Correlation-Id": "safe-correlation"} + args: Final = ("flowspeech/flowspeech-tts", "Hello", "Kore", SECRET_SENTINEL) if positional else () + kwargs: Final = ( + {} + if positional + else { + "model": "flowspeech/flowspeech-tts", + "input": "Hello", + "voice": "Kore", + "api_key": SECRET_SENTINEL, + } + ) + calls: Final = [] + + def respond(request: httpx.Request) -> httpx.Response: + calls.append(request.method) + if request.method == "POST": + assert request.headers["authorization"] == f"Bearer {SECRET_SENTINEL}" + assert request.headers["x-correlation-id"] == "safe-correlation" + assert request.headers["idempotency-key"] == REQUEST_ID + assert json.loads(request.content) == {"input": {"mode": "single", "text": "Hello", "voiceName": "Kore"}} + if not successful: + return httpx.Response( + 400, json={"error": {"code": "synthetic", "message": "synthetic", "retryable": False}} + ) + return httpx.Response(200, json=success()) + assert "authorization" not in request.headers + assert "cookie" not in request.headers + assert "x-correlation-id" not in request.headers + return httpx.Response(200, content=wav_bytes()) + + async def call(): + if asynchronous: + async with httpx.AsyncClient(transport=httpx.MockTransport(respond)) as client: + return await litellm.aspeech( + *args, + **kwargs, + **{header_field: headers}, + request_id=REQUEST_ID, + max_retries=0, + client=client, + ) + with httpx.Client(transport=httpx.MockTransport(respond)) as client: + return litellm.speech( + *args, + **kwargs, + **{header_field: headers}, + request_id=REQUEST_ID, + max_retries=0, + client=client, + ) + + if successful: + result: Final = await call() + assert result.content == wav_bytes() + else: + with pytest.raises(litellm.BadRequestError if asynchronous else FlowSpeechException) as error: + await call() + assert_clean_exception_chain(error.value) + assert error.value.status_code == 400 + captured: Final = capsys.readouterr() + output: Final = caplog.text + captured.out + captured.err + assert SECRET_SENTINEL not in output + assert "REDACTED" in output + assert ("safe-correlation" in output) is (header_name == "Authorization") + assert "Hello" in output and "Kore" in output + assert calls == (["POST", "GET"] if successful else ["POST"]) + assert headers == {header_name: SECRET_SENTINEL, "X-Correlation-Id": "safe-correlation"} + + +@pytest.mark.asyncio +@pytest.mark.parametrize("asynchronous", [False, True]) +@pytest.mark.parametrize("debug", [False, True]) +async def test_positional_key_debug_controls( + asynchronous: bool, + debug: bool, + monkeypatch: pytest.MonkeyPatch, + caplog: pytest.LogCaptureFixture, + capsys: pytest.CaptureFixture[str], +) -> None: + monkeypatch.setattr(litellm, "set_verbose", False) + monkeypatch.setattr("litellm._logging.set_verbose", False) + caplog.set_level(logging.DEBUG if debug else logging.ERROR, logger="LiteLLM") + calls: Final = [] + + def respond(request: httpx.Request) -> httpx.Response: + calls.append(request.method) + assert request.headers["authorization"] == f"Bearer {SECRET_SENTINEL}" + return httpx.Response(400, json={"error": {"code": "synthetic", "message": "synthetic", "retryable": False}}) + + with pytest.raises(litellm.BadRequestError if asynchronous else FlowSpeechException) as error: + if asynchronous: + async with httpx.AsyncClient(transport=httpx.MockTransport(respond)) as client: + await litellm.aspeech( + "flowspeech/flowspeech-tts", "Hello", "Kore", SECRET_SENTINEL, client=client, max_retries=0 + ) + else: + with httpx.Client(transport=httpx.MockTransport(respond)) as client: + litellm.speech( + "flowspeech/flowspeech-tts", "Hello", "Kore", SECRET_SENTINEL, client=client, max_retries=0 + ) + captured: Final = capsys.readouterr() + assert SECRET_SENTINEL not in caplog.text + captured.out + captured.err + assert ("Request to litellm:" in caplog.text) is debug + assert calls == ["POST"] + assert_clean_exception_chain(error.value) def test_registers_provider(): diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 55b7bc78a11..8fc72fc748b 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -4,6 +4,7 @@ import base64 import copy import contextlib import contextvars +import inspect import io import json import logging @@ -6946,6 +6947,124 @@ class TestVerboseRequestLineRedaction: FAKE_API_KEY: Final = "sk-fake-lit6823-0000000000000000" + @pytest.mark.parametrize( + "function_name", ["speech", "aspeech", "embedding", "aembedding", "completion", "acompletion"] + ) + @pytest.mark.parametrize("verbose", [False, True]) + def test_positional_credentials_follow_public_signatures( + self, + function_name: str, + verbose: bool, + caplog: pytest.LogCaptureFixture, + capsys: pytest.CaptureFixture[str], + monkeypatch: pytest.MonkeyPatch, + ) -> None: + from litellm.utils import print_args_passed_to_litellm + + function: Final = getattr(litellm, function_name) + signature_name: Final = function_name[1:] if function_name in ("aspeech", "aembedding") else function_name + signature_function: Final = getattr(litellm, signature_name) + sentinel: Final = "SYNTHETIC-POSITIONAL-KEY-ONLY" + input_value: Final = "synthetic-input" if signature_name in ("speech", "embedding") else [] + bound: Final = inspect.signature(signature_function).bind_partial( + "synthetic-model", input_value, api_key=sentinel + ) + bound.apply_defaults() + args: Final = bound.args + monkeypatch.setattr(litellm, "set_verbose", verbose) + monkeypatch.setattr("litellm._logging.set_verbose", verbose) + with caplog.at_level(logging.DEBUG if not verbose else logging.WARNING, logger=verbose_logger.name): + print_args_passed_to_litellm(function, args, {"max_retries": 0}) + captured: Final = capsys.readouterr() + output: Final = caplog.text + captured.out + captured.err + assert f"litellm.{function_name}(" in output + assert "synthetic-model" in output + assert "max_retries=0" in output + assert "REDACTED" in output + assert sentinel not in output + assert sentinel in args + + @pytest.mark.parametrize("field", ["headers", "extra_headers"]) + @pytest.mark.parametrize("malformed", ["synthetic-header-value", {b"synthetic-header-value": "unused"}]) + def test_malformed_headers_do_not_reach_request_line( + self, + field: str, + malformed: object, + caplog: pytest.LogCaptureFixture, + ) -> None: + from litellm.utils import print_args_passed_to_litellm + + with caplog.at_level(logging.DEBUG, logger=verbose_logger.name): + print_args_passed_to_litellm(litellm.completion, (), {"model": "synthetic-model", field: malformed}) + assert "synthetic-header-value" not in caplog.text + assert f"{field}='REDACTED'" in caplog.text + assert "model='synthetic-model'" in caplog.text + + @pytest.mark.asyncio + @pytest.mark.parametrize("asynchronous", [False, True]) + @pytest.mark.parametrize("positional", [False, True]) + async def test_non_speech_mock_completion_preserves_result_and_context( + self, + asynchronous: bool, + positional: bool, + caplog: pytest.LogCaptureFixture, + ) -> None: + function: Final = litellm.acompletion if asynchronous else litellm.completion + params: Final = { + "model": "openai/synthetic-model", + "messages": [{"role": "user", "content": "synthetic input"}], + "api_key": self.FAKE_API_KEY, + "temperature": 0.25, + "extra_headers": {"Authorization": self.FAKE_API_KEY, "X-Request-Id": "safe-correlation"}, + } + bound: Final = inspect.signature(function).bind_partial(**params) + bound.apply_defaults() + args: Final = bound.args if positional else () + kwargs: Final = bound.kwargs if positional else params + with caplog.at_level(logging.DEBUG, logger=verbose_logger.name): + response: Final = ( + await litellm.acompletion(*args, **kwargs, mock_response="synthetic output") + if asynchronous + else litellm.completion(*args, **kwargs, mock_response="synthetic output") + ) + assert response.choices[0].message.content == "synthetic output" + assert self.FAKE_API_KEY not in caplog.text + assert "safe-correlation" in caplog.text + assert "synthetic input" in caplog.text + assert "0.25" in caplog.text + assert params["api_key"] == self.FAKE_API_KEY + assert params["extra_headers"]["Authorization"] == self.FAKE_API_KEY + + def test_unidentified_varargs_are_hidden_without_losing_keyword_context( + self, + caplog: pytest.LogCaptureFixture, + ) -> None: + from litellm.utils import print_args_passed_to_litellm + + def opaque(*args: object, **kwargs: object) -> None: + pass + + with caplog.at_level(logging.DEBUG, logger=verbose_logger.name): + print_args_passed_to_litellm(opaque, (self.FAKE_API_KEY,), {"model": "synthetic-model"}) + assert self.FAKE_API_KEY not in caplog.text + assert "litellm.opaque('REDACTED', model='synthetic-model')" in caplog.text + + def test_positional_only_and_surplus_arguments_are_sanitized( + self, + caplog: pytest.LogCaptureFixture, + ) -> None: + from litellm.utils import print_args_passed_to_litellm + + def endpoint(model: str, api_key: str, /, *, timeout: int = 10) -> None: + pass + + with caplog.at_level(logging.DEBUG, logger=verbose_logger.name): + print_args_passed_to_litellm( + endpoint, ("synthetic-model", self.FAKE_API_KEY, self.FAKE_API_KEY), {"timeout": 2} + ) + assert self.FAKE_API_KEY not in caplog.text + assert "litellm.endpoint('synthetic-model', 'REDACTED', 'REDACTED', timeout=2)" in caplog.text + def _verbose_request_line(self, capsys, monkeypatch, **kwargs) -> str: monkeypatch.setattr(litellm, "set_verbose", True) monkeypatch.setattr("litellm._logging.set_verbose", True) From 9dc30d5eed2b5f8ab10bb370564b4363cab1f699 Mon Sep 17 00:00:00 2001 From: "heng.yin" Date: Tue, 27 Oct 2026 13:07:32 +0800 Subject: [PATCH 10/11] fix: avoid recursion in request log signature detection --- litellm/utils.py | 10 +++-- .../test_text_to_speech_transformation.py | 9 +++- tests/unit/test_utils.py | 45 +++++++++++++++++++ 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/litellm/utils.py b/litellm/utils.py index a403300fe93..1923b67a648 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -8541,19 +8541,23 @@ def get_valid_models( return [] # NON-Blocking -def _request_logging_parameters(original_function: Callable[..., object]) -> tuple[str, ...]: +def _positional_request_parameter_names(original_function: Callable[..., object]) -> tuple[str, ...]: parameters: Final = inspect.signature(original_function).parameters.values() - positional: Final = tuple( + return tuple( parameter.name for parameter in parameters if parameter.kind in (inspect.Parameter.POSITIONAL_ONLY, inspect.Parameter.POSITIONAL_OR_KEYWORD) ) + + +def _request_logging_parameters(original_function: Callable[..., object]) -> tuple[str, ...]: + positional: Final = _positional_request_parameter_names(original_function) if positional or not inspect.iscoroutinefunction(original_function): return positional if original_function.__name__.startswith("a"): synchronous: Final = getattr(inspect.getmodule(original_function), original_function.__name__[1:], None) if callable(synchronous) and not inspect.iscoroutinefunction(synchronous): - return _request_logging_parameters(synchronous) + return _positional_request_parameter_names(synchronous) return () diff --git a/tests/unit/llms/flowspeech/test_text_to_speech_transformation.py b/tests/unit/llms/flowspeech/test_text_to_speech_transformation.py index c60ba4bff82..c2d5fba1340 100644 --- a/tests/unit/llms/flowspeech/test_text_to_speech_transformation.py +++ b/tests/unit/llms/flowspeech/test_text_to_speech_transformation.py @@ -232,7 +232,9 @@ def test_gateway_base_preserves_prefix_without_duplicating_endpoint(base, expect @pytest.mark.parametrize("prefix", ["/p%2Fq", "/p%2fq", "/p%252Fq", "/p%3Fq", "/p%23q"]) @pytest.mark.parametrize("suffix", ["/", "/api/v1/tts/jobs", "/api/v1/tts/jobs/"]) def test_gateway_prefix_preserves_encoded_path(prefix, suffix): - url = FlowSpeechTextToSpeechConfig().get_complete_url("flowspeech-tts", f"https://gateway.example{prefix}{suffix}", {}) + url = FlowSpeechTextToSpeechConfig().get_complete_url( + "flowspeech-tts", f"https://gateway.example{prefix}{suffix}", {} + ) assert httpx.URL(url).raw_path == f"{prefix}/api/v1/tts/jobs".encode("ascii") assert httpx.URL(url).query == b"" assert httpx.URL(url).fragment == "" @@ -426,7 +428,7 @@ async def test_positional_key_debug_controls( assert request.headers["authorization"] == f"Bearer {SECRET_SENTINEL}" return httpx.Response(400, json={"error": {"code": "synthetic", "message": "synthetic", "retryable": False}}) - with pytest.raises(litellm.BadRequestError if asynchronous else FlowSpeechException) as error: + async def call() -> None: if asynchronous: async with httpx.AsyncClient(transport=httpx.MockTransport(respond)) as client: await litellm.aspeech( @@ -437,6 +439,9 @@ async def test_positional_key_debug_controls( litellm.speech( "flowspeech/flowspeech-tts", "Hello", "Kore", SECRET_SENTINEL, client=client, max_retries=0 ) + + with pytest.raises(litellm.BadRequestError if asynchronous else FlowSpeechException) as error: + await call() captured: Final = capsys.readouterr() assert SECRET_SENTINEL not in caplog.text + captured.out + captured.err assert ("Request to litellm:" in caplog.text) is debug diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 8fc72fc748b..b6f97e64049 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -7049,6 +7049,51 @@ class TestVerboseRequestLineRedaction: assert self.FAKE_API_KEY not in caplog.text assert "litellm.opaque('REDACTED', model='synthetic-model')" in caplog.text + @pytest.mark.parametrize("async_prefix", [False, True]) + def test_async_varargs_without_sync_signature_hide_credentials( + self, + async_prefix: bool, + caplog: pytest.LogCaptureFixture, + ) -> None: + from litellm.utils import print_args_passed_to_litellm + + async def opaque_async(*args: object, **kwargs: object) -> None: + pass + + async def aopaque(*args: object, **kwargs: object) -> None: + pass + + function: Final = aopaque if async_prefix else opaque_async + with caplog.at_level(logging.DEBUG, logger=verbose_logger.name): + print_args_passed_to_litellm(function, (self.FAKE_API_KEY,), {"model": "synthetic-model"}) + assert self.FAKE_API_KEY not in caplog.text + assert f"litellm.{function.__name__}('REDACTED', model='synthetic-model')" in caplog.text + + @pytest.mark.parametrize("error_type", [TypeError, ValueError]) + def test_unavailable_signature_hides_all_positional_values( + self, + error_type: type[Exception], + caplog: pytest.LogCaptureFixture, + ) -> None: + from litellm.utils import print_args_passed_to_litellm + + class OpaqueCallable: + __name__ = "opaque" + + @property + def __signature__(self) -> inspect.Signature: + raise error_type("signature unavailable") + + def __call__(self, *args: object, **kwargs: object) -> None: + pass + + with caplog.at_level(logging.DEBUG, logger=verbose_logger.name): + print_args_passed_to_litellm( + OpaqueCallable(), (self.FAKE_API_KEY, self.FAKE_API_KEY), {"model": "synthetic-model"} + ) + assert self.FAKE_API_KEY not in caplog.text + assert "litellm.opaque('REDACTED', 'REDACTED', model='synthetic-model')" in caplog.text + def test_positional_only_and_surplus_arguments_are_sanitized( self, caplog: pytest.LogCaptureFixture, From 0c1ed05131a22f818afad7fdf209fb8d5d9aa2f2 Mon Sep 17 00:00:00 2001 From: "heng.yin" Date: Tue, 27 Oct 2026 13:16:07 +0800 Subject: [PATCH 11/11] fix: validate request logging headers without unchecked casts --- litellm/litellm_core_utils/sensitive_data_masker.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/litellm/litellm_core_utils/sensitive_data_masker.py b/litellm/litellm_core_utils/sensitive_data_masker.py index 6bc411e2319..cc753696fcd 100644 --- a/litellm/litellm_core_utils/sensitive_data_masker.py +++ b/litellm/litellm_core_utils/sensitive_data_masker.py @@ -1,9 +1,9 @@ from collections.abc import ItemsView, Mapping, Sequence from collections.abc import Set as AbstractSet from dataclasses import dataclass, field -from typing import Any, Final, cast +from typing import Any, Final -from pydantic import BaseModel +from pydantic import BaseModel, TypeAdapter from litellm.constants import DEFAULT_MAX_RECURSE_DEPTH, DEFAULT_MAX_RECURSE_DEPTH_SENSITIVE_DATA_MASKER from litellm.litellm_core_utils.secret_redaction import REDACTED @@ -274,6 +274,7 @@ _REQUEST_LOG_HEADER_NAMES: Final = frozenset( "x-correlation-id", ) ) +_REQUEST_LOG_HEADER_ADAPTER: Final = TypeAdapter(Mapping[object, object]) def _redact_request_headers(value: object) -> object: @@ -281,7 +282,7 @@ def _redact_request_headers(value: object) -> object: return None if not isinstance(value, Mapping): return REDACTED - headers: Final = cast(Mapping[object, object], value) # cast-ok: Mapping checked above, keys remain untrusted + headers: Final = _REQUEST_LOG_HEADER_ADAPTER.validate_python(value) if any( not isinstance(name, str) or name.lower() not in _REQUEST_LOG_HEADER_NAMES or not isinstance(item, str) for name, item in headers.items()