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, {})