diff --git a/.github/workflows/test-unit.yml b/.github/workflows/test-unit.yml index b4b7afb9a09..b22022cbfd9 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/__init__.py b/litellm/__init__.py index 41e3fa34323..60d04701b61 100644 --- a/litellm/__init__.py +++ b/litellm/__init__.py @@ -674,6 +674,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() @@ -950,6 +951,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": @@ -1132,6 +1135,7 @@ model_list = list( | nscale_models | deepgram_models | elevenlabs_models + | flowspeech_models | dashscope_models | qwencloud_models | qwen_ai_platform_models @@ -1248,6 +1252,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/litellm_core_utils/sensitive_data_masker.py b/litellm/litellm_core_utils/sensitive_data_masker.py index 8c402fe9b44..cc753696fcd 100644 --- a/litellm/litellm_core_utils/sensitive_data_masker.py +++ b/litellm/litellm_core_utils/sensitive_data_masker.py @@ -3,7 +3,7 @@ from collections.abc import Set as AbstractSet from dataclasses import dataclass, field 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 @@ -255,6 +255,51 @@ 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", + ) +) +_REQUEST_LOG_HEADER_ADAPTER: Final = TypeAdapter(Mapping[object, object]) + + +def _redact_request_headers(value: object) -> object: + if value is None: + return None + if not isinstance(value, Mapping): + return REDACTED + 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() + ): + 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/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/handler.py b/litellm/llms/flowspeech/text_to_speech/handler.py new file mode 100644 index 00000000000..297d35eba4d --- /dev/null +++ b/litellm/llms/flowspeech/text_to_speech/handler.py @@ -0,0 +1,389 @@ +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.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, + 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") + end = stream.tell() + size + size % 2 + 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), + 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) + 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) + except ValueError: + payload = None + retryable = response.status_code == 429 or ( + 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 = ( + 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) + job_id = job.id + 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 + 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)) + 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) + return self.completed_response(job, audio) + yield self.delay(response) + 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: + 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: + 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 + ) + finally: + 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 + ) + 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) + 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: + 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) + except httpx.TransportError as exc: + result = exc + action = flow.send(result) + 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 # pyright: ignore[reportUnknownVariableType] # StopIteration erases the response generic + finally: + flow.close() + + async def arun(self, client: httpx.AsyncClient | None = None) -> HttpxBinaryResponseContent: + 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: + 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) + except httpx.TransportError as exc: + result = exc + action = flow.send(result) + 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 # pyright: ignore[reportUnknownVariableType] # StopIteration erases the response generic + finally: + flow.close() 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..7aac647e4dd --- /dev/null +++ b/litellm/llms/flowspeech/text_to_speech/transformation.py @@ -0,0 +1,260 @@ +from collections.abc import Mapping +from types import MappingProxyType +from typing import TYPE_CHECKING, Final, Literal +from uuid import UUID, uuid4 + +import httpx +from pydantic import BaseModel, ConfigDict, Field, StrictStr, TypeAdapter, ValidationError, 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.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] | httpx.Headers | None = None, # mutable-ok: upstream exception interface + ) -> None: + super().__init__( # pyright: ignore[reportUnknownMemberType] # upstream exception has untyped dictionary fields + status_code=status_code, message=message, headers=headers + ) + + +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 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 + + +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" + DEFAULT_VOICE = "Kore" + + def get_supported_openai_params(self, model: str) -> list[str]: # mutable-ok: base provider interface + return ["voice", "response_format"] + + def map_openai_params( + self, + model: str, + optional_params: Mapping[str, object], + voice: str | Mapping[str, object] | None = None, + drop_params: bool = False, + 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), {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: + return self.DEFAULT_VOICE + if isinstance(voice, str) and voice.strip(): + return voice.strip() + if isinstance(voice, Mapping): + candidate: Final = next( + (voice[key] for key in ("voice_name", "voiceName", "id", "name") if key in voice), None + ) + if isinstance(candidate, str) and candidate.strip(): + return candidate.strip() + raise ValueError("FlowSpeech voice must be a nonempty voice name") + + def validate_environment( + 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 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.") + 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 checked_headers.items() + if name.lower() not in ("authorization", "content-type") + }, + "Authorization": f"Bearer {key}", + "Content-Type": "application/json", + } + + def get_error_class( + self, + error_message: str, + status_code: int, + headers: dict[str, str] | httpx.Headers, # mutable-ok: upstream exception interface + ) -> BaseLLMException: + 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 { + "mode": "multi", + "segments": [part.model_dump() for part in segments], + } + if not input.strip(): + raise ValueError("FlowSpeech input must contain text") + return { + "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: Mapping[str, object], + litellm_params: Mapping[str, object], + headers: Mapping[str, str], + ) -> TextToSpeechRequestData: + 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={"input": body}, + headers={"Idempotency-Key": request_id}, + ) + + def transform_text_to_speech_response( + self, + model: str, + raw_response: httpx.Response, + logging_obj: "LiteLLMLoggingObj", + ) -> "HttpxBinaryResponseContent": + from litellm.types.llms.openai import HttpxBinaryResponseContent + + 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) + + 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 b"?" in url.raw_path: + raise ValueError("FlowSpeech api_base must not contain a query") + if url.path.rstrip("/").endswith("/api/ai/text-to-speech"): + raise ValueError("FlowSpeech api_base must not use the legacy text-to-speech 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 9f412e1f24b..e83fdaab9b4 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -8881,6 +8881,83 @@ def speech( client=client, _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, + ) + + 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 + + flowspeech_params: Final = TypeAdapter(Mapping[str, object]).validate_python(litellm_params_dict) + flowspeech_headers: Final = flowspeech_config.validate_environment( + TypeAdapter(object).validate_python(extra_headers or headers or MappingProxyType({})), + model, + flowspeech_params.get("api_key"), + 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=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, flowspeech_params) + + def flowspeech_post_call(job: FlowSpeechJob) -> None: + 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"}), + 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"} + ), + **TypeAdapter(Mapping[str, str]).validate_python(flowspeech_request.get("headers"), strict=True), + } + ), + 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( # 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 = 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/model_prices_and_context_window_backup.json b/litellm/model_prices_and_context_window_backup.json index 5774e699ad7..88a72d451a2 100644 --- a/litellm/model_prices_and_context_window_backup.json +++ b/litellm/model_prices_and_context_window_backup.json @@ -27697,6 +27697,14 @@ "output_cost_per_token": 0.0, "source": "https://fireworks.ai/pricing" }, + "flowspeech/flowspeech-tts": { + "litellm_provider": "flowspeech", + "mode": "audio_speech", + "source": "https://flowspeech.io/", + "supported_endpoints": [ + "/v1/audio/speech" + ] + }, "friendliai/zai-org/GLM-5.3-Flash": { "litellm_provider": "friendliai", "max_input_tokens": 1048576, diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index 3b36ea5afce..35858b52c13 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -13378,10 +13378,26 @@ async def audio_speech( response_format=requested_format if isinstance(requested_format, str) else None, ) + 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 d96f2976508..9cfd54a6328 100644 --- a/litellm/proxy/public_endpoints/provider_create_fields.json +++ b/litellm/proxy/public_endpoints/provider_create_fields.json @@ -1589,6 +1589,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/litellm/types/utils.py b/litellm/types/utils.py index 2869f9b2577..af2a8532ebc 100644 --- a/litellm/types/utils.py +++ b/litellm/types/utils.py @@ -4525,6 +4525,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 1e095966a43..b490eda175d 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 @@ -1900,7 +1904,15 @@ def client(original_function): return result # Prints Exactly what was passed to litellm function - don't execute any logic here - it should just print - print_args_passed_to_litellm(original_function, args, kwargs) + if is_debugging_on(): + try: + print_args_passed_to_litellm( + original_function, + TypeAdapter(tuple[object, ...]).validate_python(args, strict=True), + TypeAdapter(Mapping[str, object]).validate_python(kwargs, strict=True), + ) + except (TypeError, ValueError): + print_verbose("Skipped malformed request debug logging") start_time: Final = datetime.datetime.now() result = None logging_obj: LiteLLMLoggingObject | None = kwargs.get("litellm_logging_obj", None) @@ -2169,7 +2181,15 @@ def client(original_function): @wraps(original_function) async def wrapper_async(*args, **kwargs): - print_args_passed_to_litellm(original_function, args, kwargs) + if is_debugging_on(): + try: + print_args_passed_to_litellm( + original_function, + TypeAdapter(tuple[object, ...]).validate_python(args, strict=True), + TypeAdapter(Mapping[str, object]).validate_python(kwargs, strict=True), + ) + except (TypeError, ValueError): + print_verbose("Skipped malformed request debug logging") start_time: Final = datetime.datetime.now() call_type: Final = _get_call_type_from_public_name(original_function.__name__) result = None @@ -8532,28 +8552,63 @@ def get_valid_models( return [] # NON-Blocking -def print_args_passed_to_litellm(original_function, args, kwargs): +def _positional_request_parameter_names(original_function: Callable[..., object]) -> tuple[str, ...]: + parameters: Final = inspect.signature(original_function).parameters.values() + 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 _positional_request_parameter_names(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: object, kwargs: object) -> None: if not is_debugging_on(): return try: + request_args: Final = TypeAdapter(tuple[object, ...]).validate_python(args, strict=True) + request_kwargs: Final = TypeAdapter(Mapping[str, object]).validate_python(kwargs, strict=True) # we've already printed this for acompletion, don't print for completion if ( - "acompletion" in kwargs - and kwargs["acompletion"] is True + "acompletion" in request_kwargs + and request_kwargs["acompletion"] is True and original_function.__name__ == "completion" - or "aembedding" in kwargs - and kwargs["aembedding"] is True + or "aembedding" in request_kwargs + and request_kwargs["aembedding"] is True and original_function.__name__ == "embedding" or ( - "aimg_generation" in kwargs - and kwargs["aimg_generation"] is True + "aimg_generation" in request_kwargs + and request_kwargs["aimg_generation"] is True and original_function.__name__ == "img_generation" ) ): 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, request_args))) + redacted_kwargs: Final = redact_credentials_in_request(request_kwargs) kwargs_str: Final = ", ".join(f"{key}={value!r}" for key, value in redacted_kwargs.items()) print_verbose( "\n", @@ -8561,11 +8616,11 @@ def print_args_passed_to_litellm(original_function, args, kwargs): print_verbose( "\033[92mRequest to litellm:\033[0m", ) - if args and kwargs: + if request_args and request_kwargs: print_verbose(f"\033[92mlitellm.{original_function.__name__}({args_str}, {kwargs_str})\033[0m") - elif args: + elif request_args: print_verbose(f"\033[92mlitellm.{original_function.__name__}({args_str})\033[0m") - elif kwargs: + elif request_kwargs: print_verbose(f"\033[92mlitellm.{original_function.__name__}({kwargs_str})\033[0m") else: print_verbose(f"\033[92mlitellm.{original_function.__name__}()\033[0m") @@ -10752,6 +10807,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 5774e699ad7..88a72d451a2 100644 --- a/model_prices_and_context_window.json +++ b/model_prices_and_context_window.json @@ -27697,6 +27697,14 @@ "output_cost_per_token": 0.0, "source": "https://fireworks.ai/pricing" }, + "flowspeech/flowspeech-tts": { + "litellm_provider": "flowspeech", + "mode": "audio_speech", + "source": "https://flowspeech.io/", + "supported_endpoints": [ + "/v1/audio/speech" + ] + }, "friendliai/zai-org/GLM-5.3-Flash": { "litellm_provider": "friendliai", "max_input_tokens": 1048576, diff --git a/provider_endpoints_support.json b/provider_endpoints_support.json index 9ba6ed342ea..95c47725676 100644 --- a/provider_endpoints_support.json +++ b/provider_endpoints_support.json @@ -1452,6 +1452,24 @@ "generateContent": false } }, + "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", 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/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/__init__.py b/tests/unit/llms/flowspeech/__init__.py new file mode 100644 index 00000000000..8b137891791 --- /dev/null +++ b/tests/unit/llms/flowspeech/__init__.py @@ -0,0 +1 @@ + diff --git a/tests/unit/llms/flowspeech/test_handler.py b/tests/unit/llms/flowspeech/test_handler.py new file mode 100644 index 00000000000..2a42f9210da --- /dev/null +++ b/tests/unit/llms/flowspeech/test_handler.py @@ -0,0 +1,680 @@ +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.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, FlowSpeechTextToSpeechConfig +from litellm.types.utils import LlmProviders + +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(" 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__() + 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(): + output = io.BytesIO() + with wave.open(output, "wb") as audio: + audio.setparams((1, 2, 24000, 0, "NONE", "not compressed")) + audio.writeframes(b"\x00\x00" * 24) + return output.getvalue() + + +def success(request_id=REQUEST_ID): + return { + "id": JOB_ID, + "requestId": request_id, + "status": "success", + "usage": {"billableCharacters": 5, "reservedCredits": 0, "chargedCredits": 5}, + "result": { + "url": "https://audio.example.com/recording.wav", + "mimeType": "audio/wav", + "sampleRate": 24000, + "channels": 1, + "bitsPerSample": 16, + }, + } + + +def test_maps_single_voice_and_uuid(): + config = FlowSpeechTextToSpeechConfig() + voice, params = config.map_openai_params("flowspeech-tts", {"response_format": "wav"}, {"voiceName": "Aoede"}) + request = config.transform_text_to_speech_request("flowspeech-tts", "Hello", voice, params, {}, {}) + assert request["dict_body"] == {"input": {"mode": "single", "text": "Hello", "voiceName": "Aoede"}} + assert UUID(request["headers"]["Idempotency-Key"]).version == 4 + assert config.get_supported_openai_params("flowspeech-tts") == ["voice", "response_format"] + + +def test_maps_multi_and_recovery_id(): + config = FlowSpeechTextToSpeechConfig() + segments = [{"speaker": "Alice", "text": "Hi", "voiceName": "Kore"}] + voice, params = config.map_openai_params( + "flowspeech-tts", {}, kwargs={"segments": segments, "request_id": REQUEST_ID} + ) + request = config.transform_text_to_speech_request("flowspeech-tts", "", voice, params, {}, {}) + assert request["dict_body"] == {"input": {"mode": "multi", "segments": segments}} + assert request["headers"]["Idempotency-Key"] == REQUEST_ID + + +@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) + + +@pytest.mark.parametrize("key", ["source", "format", "speakers", "originalText", "prompt", "stream", "extra_body"]) +def test_rejects_legacy_and_unsupported_kwargs(key): + with pytest.raises(ValueError, match="Unsupported"): + FlowSpeechTextToSpeechConfig().map_openai_params("flowspeech-tts", {}, kwargs={key: "unused"}) + + +@pytest.mark.parametrize("voice", ["", " ", {}, {"voiceName": ""}, {"voiceName": 7}]) +def test_rejects_invalid_voice(voice): + 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,message", + [ + ({"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, message): + with pytest.raises(ValueError, match=message): + FlowSpeechTextToSpeechConfig().transform_text_to_speech_request("flowspeech-tts", text, None, params, {}, {}) + + +def test_conflicting_idempotency_keys(): + with pytest.raises(ValueError, match="must agree"): + FlowSpeechTextToSpeechConfig().transform_text_to_speech_request( + "flowspeech-tts", "Hi", None, {"request_id": REQUEST_ID}, {}, {"idempotency-key": JOB_ID} + ) + + +def test_rejects_multi_with_voice_or_unknown_model(): + with pytest.raises(ValueError, match="segment"): + FlowSpeechTextToSpeechConfig().map_openai_params("flowspeech-tts", {}, "Kore", kwargs={"segments": []}) + with pytest.raises(ValueError, match="route"): + FlowSpeechTextToSpeechConfig().map_openai_params("unsupported", {}) + + +def test_bearer_header_overrides_case_insensitively(): + headers = FlowSpeechTextToSpeechConfig().validate_environment( + {"authorization": "discard", "content-type": "discard", "X-Correlation-Id": "test"}, + "flowspeech-tts", + "test-key", + ) + assert httpx.Headers(headers)["authorization"] == "Bearer test-key" + assert len(httpx.Headers(headers).get_list("authorization")) == 1 + assert headers["Content-Type"] == "application/json" + + +@pytest.mark.parametrize("key", ["", " ", "two tokens", "bad\nkey", "bad\x00key"]) +def test_rejects_invalid_key(key): + with pytest.raises(ValueError, match="API key"): + FlowSpeechTextToSpeechConfig().validate_environment({}, "flowspeech-tts", key) + + +@pytest.mark.parametrize("name", ["Cookie", "Proxy-Authorization", "Host", "content-length"]) +def test_rejects_unsafe_api_headers(name): + 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") + + +def test_missing_key(monkeypatch): + monkeypatch.setattr(litellm, "api_key", None) + monkeypatch.delenv("FLOWSPEECH_API_KEY", raising=False) + with pytest.raises(ValueError, match="API key"): + FlowSpeechTextToSpeechConfig().validate_environment({}, "flowspeech-tts") + + +@pytest.mark.parametrize("base", ["https://flowspeech.io", "https://flowspeech.io/api/v1/tts/jobs/"]) +def test_complete_url(base): + assert ( + FlowSpeechTextToSpeechConfig().get_complete_url("flowspeech-tts", base, {}) + == "https://flowspeech.io/api/v1/tts/jobs" + ) + + +@pytest.mark.parametrize( + "base,message", + [ + ("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 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", + "FlowSpeech api_base must not use the legacy text-to-speech endpoint", + ), + ("", "FlowSpeech requires an HTTPS URL without credentials or fragment"), + ], +) +def test_rejects_invalid_base(base, message): + with pytest.raises(ValueError, match=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 + + +@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("positional", [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"), + ({"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, + positional, + 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} + 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(*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(*args, **call_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 + 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}}) + + async def call() -> None: + 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 + ) + + 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 + assert calls == ["POST"] + assert_clean_exception_chain(error.value) + + +def test_registers_provider(): + from litellm.utils import ProviderConfigManager + + model, provider, _, _ = litellm.get_llm_provider("flowspeech/flowspeech-tts") + config = ProviderConfigManager.get_provider_text_to_speech_config(model, litellm.LlmProviders.FLOWSPEECH) + 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]) +@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"}] + + 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, + client=HTTPHandler(client=http_client), + logger_fn=log_event, + **timeout_args, + **({"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) + + +@pytest.mark.asyncio +@pytest.mark.parametrize("timeout_args", [{}, {"timeout": None}, {"timeout": 60}]) +async def test_aspeech_dispatch(timeout_args): + 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, + logger_fn=log_event, + **timeout_args, + ) + 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) + + +@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/proxy_server/test_routes_audio.py b/tests/unit/proxy/proxy_server/test_routes_audio.py index de76c7257cf..82032bd0362 100644 --- a/tests/unit/proxy/proxy_server/test_routes_audio.py +++ b/tests/unit/proxy/proxy_server/test_routes_audio.py @@ -172,6 +172,46 @@ 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): + 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( ("patched_speech", "response_format", "expected_content_type"), [ diff --git a/tests/unit/proxy/public_endpoints/test_public_endpoints.py b/tests/unit/proxy/public_endpoints/test_public_endpoints.py index f60c2254b41..45ead329c95 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 diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 38287b25169..1a7378971b6 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 @@ -22,6 +23,7 @@ import httpx import pytest import respx from jsonschema import Draft7Validator, validate +from pydantic import ValidationError import litellm from litellm.exceptions import MidStreamFallbackError @@ -6986,6 +6988,344 @@ class TestVerboseRequestLineRedaction: FAKE_API_KEY: Final = "sk-fake-lit6823-0000000000000000" + @pytest.mark.parametrize("verbose", [False, True]) + @pytest.mark.parametrize( + "args, kwargs", + [ + (["synthetic-model", "SYNTHETIC-BOUNDARY-SECRET"], {"api_key": "SYNTHETIC-BOUNDARY-SECRET"}), + ("SYNTHETIC-BOUNDARY-SECRET", {"model": "synthetic-model"}), + (None, {"api_key": "SYNTHETIC-BOUNDARY-SECRET"}), + ((), [("api_key", "SYNTHETIC-BOUNDARY-SECRET")]), + ((), {123: "SYNTHETIC-BOUNDARY-SECRET"}), + ((), {b"api_key": "SYNTHETIC-BOUNDARY-SECRET"}), + ], + ) + def test_malformed_request_containers_do_not_emit_or_raise( + self, + args: object, + kwargs: object, + verbose: bool, + caplog: pytest.LogCaptureFixture, + capsys: pytest.CaptureFixture[str], + monkeypatch: pytest.MonkeyPatch, + ) -> None: + from litellm.utils import print_args_passed_to_litellm + + 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(litellm.completion, args, kwargs) + captured: Final = capsys.readouterr() + output: Final = caplog.text + captured.out + captured.err + assert "litellm.completion(" not in output + assert "SYNTHETIC-BOUNDARY-SECRET" not in output + + def test_disabled_logging_does_not_validate_or_emit_request_containers( + self, + caplog: pytest.LogCaptureFixture, + capsys: pytest.CaptureFixture[str], + monkeypatch: pytest.MonkeyPatch, + ) -> None: + from litellm.utils import print_args_passed_to_litellm + + adapter: Final = Mock(side_effect=RuntimeError("Disabled logging constructed a validator")) + monkeypatch.setattr(litellm, "set_verbose", False) + monkeypatch.setattr("litellm._logging.set_verbose", False) + with monkeypatch.context() as validation: + validation.setattr("litellm.utils.TypeAdapter", adapter) + with caplog.at_level(logging.WARNING, logger=verbose_logger.name): + print_args_passed_to_litellm( + litellm.completion, (self.FAKE_API_KEY,), {"api_key": self.FAKE_API_KEY} + ) + captured: Final = capsys.readouterr() + output: Final = caplog.text + captured.out + captured.err + assert adapter.call_count == 0 + assert "litellm.completion(" not in output + assert self.FAKE_API_KEY not in output + + @pytest.mark.asyncio + @pytest.mark.parametrize("asynchronous", [False, True]) + @pytest.mark.parametrize("verbose", [False, True]) + @pytest.mark.parametrize("stage", ["args", "kwargs"]) + @pytest.mark.parametrize("error_type", [TypeError, ValueError, ValidationError]) + async def test_shape_validation_failure_keeps_mock_operation_and_drops_error_details( + self, + asynchronous: bool, + verbose: bool, + stage: str, + error_type: type[Exception], + caplog: pytest.LogCaptureFixture, + capsys: pytest.CaptureFixture[str], + monkeypatch: pytest.MonkeyPatch, + ) -> None: + from pydantic import TypeAdapter + + rendered: Final = Mock() + + class UnrenderedInput: + def __repr__(self) -> str: + rendered() + return "SYNTHETIC-INPUT-REPR" + + signed_url: Final = "https://audio.example.test/result.wav?sig=SYNTHETIC-URL-SECRET" + diagnostic: Final = f"SYNTHETIC-INPUT-REPR {self.FAKE_API_KEY} {signed_url}" + unrendered: Final = UnrenderedInput() + + def fail_validation(value: object, *, strict: bool) -> None: + assert strict is True + assert isinstance(value, tuple if stage == "args" else dict) + if error_type is ValidationError: + TypeAdapter(int).validate_python({"diagnostic": diagnostic, "input": unrendered}, strict=True) + pytest.fail("Expected a real Pydantic validation failure") + raise error_type(diagnostic, unrendered) + + broken: Final = Mock() + broken.validate_python.side_effect = fail_validation + + def adapter_factory(annotation: object) -> object: + if annotation == (tuple[object, ...] if stage == "args" else Mapping[str, object]): + return broken + return TypeAdapter(annotation) + + monkeypatch.setattr(litellm, "set_verbose", verbose) + monkeypatch.setattr("litellm._logging.set_verbose", verbose) + with monkeypatch.context() as validation: + validation.setattr("litellm.utils.TypeAdapter", adapter_factory) + with caplog.at_level(logging.DEBUG if not verbose else logging.WARNING, logger=verbose_logger.name): + response: Final = ( + await litellm.acompletion( + model="openai/synthetic-model", + messages=[{"role": "user", "content": "synthetic input"}], + api_key=self.FAKE_API_KEY, + mock_response="synthetic output", + ) + if asynchronous + else litellm.completion( + model="openai/synthetic-model", + messages=[{"role": "user", "content": "synthetic input"}], + api_key=self.FAKE_API_KEY, + mock_response="synthetic output", + ) + ) + captured: Final = capsys.readouterr() + output: Final = caplog.text + captured.out + captured.err + assert response.choices[0].message.content == "synthetic output" + assert broken.validate_python.call_count >= 1 + assert rendered.call_count == 0 + assert "Skipped malformed request debug logging" in output + assert self.FAKE_API_KEY not in output + assert signed_url not in output + assert "SYNTHETIC-INPUT-REPR" not in output + assert all(record.exc_info is None for record in caplog.records) + + @pytest.mark.asyncio + @pytest.mark.parametrize("asynchronous", [False, True]) + async def test_disabled_wrapper_logging_does_not_construct_shape_validators( + self, + asynchronous: bool, + caplog: pytest.LogCaptureFixture, + capsys: pytest.CaptureFixture[str], + monkeypatch: pytest.MonkeyPatch, + ) -> None: + from pydantic import TypeAdapter + + constructed: Final = Mock() + + def adapter_factory(annotation: object) -> object: + if annotation in (tuple[object, ...], Mapping[str, object]): + constructed(annotation) + return TypeAdapter(annotation) + + monkeypatch.setattr(litellm, "set_verbose", False) + monkeypatch.setattr("litellm._logging.set_verbose", False) + with monkeypatch.context() as validation: + validation.setattr("litellm.utils.TypeAdapter", adapter_factory) + with caplog.at_level(logging.WARNING, logger=verbose_logger.name): + response: Final = ( + await litellm.acompletion( + model="openai/synthetic-model", + messages=[{"role": "user", "content": "synthetic input"}], + api_key=self.FAKE_API_KEY, + mock_response="synthetic output", + ) + if asynchronous + else litellm.completion( + model="openai/synthetic-model", + messages=[{"role": "user", "content": "synthetic input"}], + api_key=self.FAKE_API_KEY, + mock_response="synthetic output", + ) + ) + captured: Final = capsys.readouterr() + output: Final = caplog.text + captured.out + captured.err + assert response.choices[0].message.content == "synthetic output" + assert constructed.call_count == 0 + assert "Skipped malformed request debug logging" not in output + assert self.FAKE_API_KEY not in output + + @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 + + @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, + ) -> 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)