mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-18 09:45:23 +00:00
Merge 0c1ed05131 into cfe0100e1f
This commit is contained in:
commit
d40261cdd2
23 changed files with 2686 additions and 6 deletions
1
.github/workflows/test-unit.yml
vendored
1
.github/workflows/test-unit.yml
vendored
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -673,6 +673,7 @@ nebius_embedding_models: Set = set()
|
|||
aiml_models: Set = set()
|
||||
deepgram_models: Set = set()
|
||||
elevenlabs_models: Set = set()
|
||||
flowspeech_models: Final[set[str]] = set() # mutable-ok: populated when the shared model catalog loads
|
||||
dashscope_models: Set = set()
|
||||
qwencloud_models: Set = set()
|
||||
qwen_ai_platform_models: Set = set()
|
||||
|
|
@ -949,6 +950,8 @@ def _populate_provider_model_sets(model_cost_map: Mapping[str, Mapping[str, obje
|
|||
deepgram_models.add(key)
|
||||
elif value.get("litellm_provider") == "elevenlabs":
|
||||
elevenlabs_models.add(key)
|
||||
elif value.get("litellm_provider") == "flowspeech":
|
||||
flowspeech_models.add(key)
|
||||
elif value.get("litellm_provider") == "heroku":
|
||||
heroku_models.add(key)
|
||||
elif value.get("litellm_provider") == "dashscope":
|
||||
|
|
@ -1131,6 +1134,7 @@ model_list = list(
|
|||
| nscale_models
|
||||
| deepgram_models
|
||||
| elevenlabs_models
|
||||
| flowspeech_models
|
||||
| dashscope_models
|
||||
| qwencloud_models
|
||||
| qwen_ai_platform_models
|
||||
|
|
@ -1247,6 +1251,7 @@ def _build_models_by_provider() -> dict:
|
|||
"featherless_ai": featherless_ai_models,
|
||||
"deepgram": deepgram_models,
|
||||
"elevenlabs": elevenlabs_models,
|
||||
"flowspeech": flowspeech_models,
|
||||
"heroku": heroku_models,
|
||||
"dashscope": dashscope_models,
|
||||
"qwencloud": qwencloud_models,
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
1
litellm/llms/flowspeech/__init__.py
Normal file
1
litellm/llms/flowspeech/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
|
||||
1
litellm/llms/flowspeech/text_to_speech/__init__.py
Normal file
1
litellm/llms/flowspeech/text_to_speech/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
|
||||
389
litellm/llms/flowspeech/text_to_speech/handler.py
Normal file
389
litellm/llms/flowspeech/text_to_speech/handler.py
Normal file
|
|
@ -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("<HIIHH", 1, 24000, 48000, 2, 16):
|
||||
raise ValueError("Invalid PCM format fields")
|
||||
if header[:4] == b"data" and size % 2:
|
||||
raise ValueError("Unaligned PCM data")
|
||||
stream.seek(end)
|
||||
|
||||
|
||||
class FlowSpeechTTS:
|
||||
MAX_AUDIO_BYTES = 100 * 1024 * 1024
|
||||
MAX_JSON_BYTES = 1024 * 1024
|
||||
POLL_INTERVAL = 5.0
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
url: str,
|
||||
headers: Mapping[str, str],
|
||||
body: Mapping[str, object],
|
||||
timeout: float | httpx.Timeout,
|
||||
max_retries: object = 2,
|
||||
*,
|
||||
clock: Callable[[], float] = time.monotonic,
|
||||
wall_clock: Callable[[], float] = time.time,
|
||||
sleep: Callable[[float], None] = time.sleep,
|
||||
async_sleep: Callable[[float], Awaitable[None]] = asyncio.sleep,
|
||||
on_completed: Callable[[FlowSpeechJob], None] | None = None,
|
||||
) -> 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()
|
||||
260
litellm/llms/flowspeech/text_to_speech/transformation.py
Normal file
260
litellm/llms/flowspeech/text_to_speech/transformation.py
Normal file
|
|
@ -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))
|
||||
|
|
@ -8879,6 +8879,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,
|
||||
|
|
|
|||
|
|
@ -27695,6 +27695,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,
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -4525,6 +4525,7 @@ class LlmProviders(str, Enum):
|
|||
INFINITY = "infinity"
|
||||
DEEPGRAM = "deepgram"
|
||||
ELEVENLABS = "elevenlabs"
|
||||
FLOWSPEECH = "flowspeech"
|
||||
NOVITA = "novita"
|
||||
AIOHTTP_OPENAI = "aiohttp_openai"
|
||||
LANGFUSE = "langfuse"
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -8532,7 +8536,42 @@ 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: tuple[object, ...], kwargs: Mapping[str, object]
|
||||
) -> None:
|
||||
if not is_debugging_on():
|
||||
return
|
||||
try:
|
||||
|
|
@ -8552,8 +8591,8 @@ def print_args_passed_to_litellm(original_function, args, kwargs):
|
|||
):
|
||||
return
|
||||
|
||||
args_str: Final = ", ".join(map(repr, args))
|
||||
redacted_kwargs: Final = redact_credentials_in_payload(kwargs)
|
||||
args_str: Final = ", ".join(map(repr, _redact_positional_request_args(original_function, args)))
|
||||
redacted_kwargs: Final = redact_credentials_in_request(kwargs)
|
||||
kwargs_str: Final = ", ".join(f"{key}={value!r}" for key, value in redacted_kwargs.items())
|
||||
print_verbose(
|
||||
"\n",
|
||||
|
|
@ -10750,6 +10789,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,
|
||||
|
|
|
|||
|
|
@ -27695,6 +27695,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,
|
||||
|
|
|
|||
|
|
@ -1014,6 +1014,24 @@
|
|||
"interactions": true
|
||||
}
|
||||
},
|
||||
"flowspeech": {
|
||||
"display_name": "FlowSpeech (`flowspeech`)",
|
||||
"url": "https://docs.litellm.ai/docs/providers/flowspeech",
|
||||
"endpoints": {
|
||||
"chat_completions": false,
|
||||
"messages": false,
|
||||
"responses": false,
|
||||
"embeddings": false,
|
||||
"image_generations": false,
|
||||
"audio_transcriptions": false,
|
||||
"audio_speech": true,
|
||||
"moderations": false,
|
||||
"batches": false,
|
||||
"rerank": false,
|
||||
"a2a": false,
|
||||
"interactions": false
|
||||
}
|
||||
},
|
||||
"exa_ai": {
|
||||
"display_name": "Exa AI (`exa_ai`)",
|
||||
"url": "https://docs.litellm.ai/docs/search/exa_ai",
|
||||
|
|
|
|||
213
tests/integration/sdk/test_flowspeech_deadline_wire.py
Normal file
213
tests/integration/sdk/test_flowspeech_deadline_wire.py
Normal file
|
|
@ -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 == []
|
||||
|
|
@ -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.
|
||||
|
|
|
|||
1
tests/unit/llms/flowspeech/__init__.py
Normal file
1
tests/unit/llms/flowspeech/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
|
||||
680
tests/unit/llms/flowspeech/test_handler.py
Normal file
680
tests/unit/llms/flowspeech/test_handler.py
Normal file
|
|
@ -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("<I", 3) + b"abc\x00" + content[12:]
|
||||
content = resized_riff(content)
|
||||
return content
|
||||
|
||||
|
||||
def resized_riff(content):
|
||||
return content[:4] + struct.pack("<I", len(content) - 8) + content[8:]
|
||||
|
||||
|
||||
def changed_wav(offset, replacement):
|
||||
content = wav_bytes()
|
||||
return content[:offset] + replacement + content[offset + len(replacement) :]
|
||||
|
||||
|
||||
def job(status="success", **overrides):
|
||||
return {
|
||||
"id": JOB_ID,
|
||||
"requestId": REQUEST_ID,
|
||||
"status": status,
|
||||
"usage": {
|
||||
"billableCharacters": 2,
|
||||
"reservedCredits": 2 if status in ("queued", "running", "merging", "settling") else 0,
|
||||
"chargedCredits": 2 if status == "success" else 0,
|
||||
},
|
||||
"result": {
|
||||
"url": AUDIO_URL,
|
||||
"mimeType": "audio/wav",
|
||||
"sampleRate": 24000,
|
||||
"channels": 1,
|
||||
"bitsPerSample": 16,
|
||||
}
|
||||
if status == "success"
|
||||
else None,
|
||||
**overrides,
|
||||
}
|
||||
|
||||
|
||||
class Clock:
|
||||
def __init__(self):
|
||||
self.now = 0
|
||||
self.sleeps = []
|
||||
|
||||
def time(self):
|
||||
return self.now
|
||||
|
||||
def sleep(self, delay):
|
||||
self.sleeps.append(delay)
|
||||
self.now += delay
|
||||
|
||||
async def asleep(self, delay):
|
||||
self.sleep(delay)
|
||||
await asyncio.sleep(0)
|
||||
|
||||
|
||||
def handler(clock, timeout=120, url=URL, **kwargs):
|
||||
return FlowSpeechTTS(
|
||||
url,
|
||||
{
|
||||
"Authorization": "Bearer test-key",
|
||||
"Idempotency-Key": REQUEST_ID,
|
||||
"Content-Type": "application/json",
|
||||
"X-Private-Api-Header": "test",
|
||||
},
|
||||
{"input": {"mode": "single", "text": "Hi", "voiceName": "Kore"}},
|
||||
timeout,
|
||||
clock=clock.time,
|
||||
sleep=clock.sleep,
|
||||
async_sleep=clock.asleep,
|
||||
wall_clock=lambda: 0,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(params=["sync", "async"])
|
||||
def invoke(request):
|
||||
def run(tts, respond):
|
||||
options = {
|
||||
"transport": httpx.MockTransport(respond),
|
||||
"headers": {"Authorization": "discard-default", "X-Client-Secret": "discard"},
|
||||
"cookies": {"session": "discard"},
|
||||
"auth": ("discard", "discard"),
|
||||
"follow_redirects": True,
|
||||
}
|
||||
if request.param == "sync":
|
||||
with httpx.Client(**options) as client:
|
||||
return tts.run(client)
|
||||
|
||||
async def async_run():
|
||||
async with httpx.AsyncClient(**options) as client:
|
||||
return await tts.arun(client)
|
||||
|
||||
return asyncio.run(async_run())
|
||||
|
||||
return run
|
||||
|
||||
|
||||
def test_all_pending_states_and_uncredentialed_download(invoke):
|
||||
clock = Clock()
|
||||
requests = []
|
||||
states = iter(["queued", "running", "merging", "settling", "success"])
|
||||
|
||||
def respond(request):
|
||||
requests.append(request)
|
||||
assert "cookie" not in request.headers
|
||||
assert "x-client-secret" not in request.headers
|
||||
if request.url.host == "audio.example.com":
|
||||
assert "authorization" not in request.headers
|
||||
assert "x-private-api-header" not in request.headers
|
||||
assert "idempotency-key" not in request.headers
|
||||
return httpx.Response(200, content=wav_bytes())
|
||||
assert request.headers["authorization"] == "Bearer test-key"
|
||||
assert request.url == (URL if request.method == "POST" else f"{URL}/{JOB_ID}")
|
||||
return httpx.Response(
|
||||
202 if request.method == "POST" else 200,
|
||||
json=job(next(states)),
|
||||
headers={"set-cookie": "session=discard-from-server"},
|
||||
)
|
||||
|
||||
result = invoke(handler(clock), respond)
|
||||
assert result.content == wav_bytes()
|
||||
assert result.response.headers["x-flowspeech-request-id"] == REQUEST_ID
|
||||
assert "set-cookie" not in result.response.headers
|
||||
assert clock.sleeps == [5, 5, 5, 5]
|
||||
assert len(requests) == 6
|
||||
assert requests[-1].extensions["timeout"]["read"] == 100
|
||||
|
||||
|
||||
@pytest.mark.parametrize("prefix", ["/p%2Fq", "/p%2fq", "/p%252Fq", "/p%3Fq", "/p%23q"])
|
||||
def test_encoded_gateway_prefix_survives_submission_and_polling(invoke, prefix):
|
||||
requests = []
|
||||
endpoint = f"{prefix}/api/v1/tts/jobs"
|
||||
url = FlowSpeechTextToSpeechConfig().get_complete_url("flowspeech-tts", f"https://gateway.example{prefix}/", {})
|
||||
|
||||
def respond(request):
|
||||
requests.append(request)
|
||||
if request.method == "POST":
|
||||
return httpx.Response(202, json=job("queued"))
|
||||
if request.url.host == "gateway.example":
|
||||
return httpx.Response(200, json=job())
|
||||
return httpx.Response(200, content=wav_bytes())
|
||||
|
||||
assert invoke(handler(Clock(), url=url), respond).content == wav_bytes()
|
||||
assert [(request.method, request.url.raw_path) for request in requests] == [
|
||||
("POST", endpoint.encode()),
|
||||
("GET", f"{endpoint}/{JOB_ID}".encode()),
|
||||
("GET", b"/recording.wav"),
|
||||
]
|
||||
assert requests[0].headers["Idempotency-Key"] == REQUEST_ID
|
||||
|
||||
|
||||
@pytest.mark.parametrize("failure", ["network", "server", "rate_limit"])
|
||||
def test_create_retry_preserves_uuid_and_body(invoke, failure):
|
||||
clock = Clock()
|
||||
requests = []
|
||||
|
||||
def respond(request):
|
||||
requests.append(request)
|
||||
if len(requests) == 1:
|
||||
if failure == "network":
|
||||
raise httpx.ReadTimeout("lost response")
|
||||
return httpx.Response(
|
||||
429 if failure == "rate_limit" else 503,
|
||||
headers={"Retry-After": "11"},
|
||||
json={"error": {"code": "busy", "message": "wait", "retryable": True}, "traceId": "trace"},
|
||||
)
|
||||
return httpx.Response(200, json=job()) if request.method == "POST" else httpx.Response(200, content=wav_bytes())
|
||||
|
||||
assert invoke(handler(clock), respond).content == wav_bytes()
|
||||
assert requests[0].content == requests[1].content
|
||||
assert requests[0].headers["idempotency-key"] == requests[1].headers["idempotency-key"] == REQUEST_ID
|
||||
assert clock.sleeps == [5 if failure == "network" else 11]
|
||||
|
||||
|
||||
def test_http_date_retry_after_and_poll_rate_limit(invoke):
|
||||
clock = Clock()
|
||||
responses = iter(
|
||||
[
|
||||
httpx.Response(
|
||||
202,
|
||||
json=job("queued"),
|
||||
headers={"Retry-After": format_datetime(datetime.fromtimestamp(12, timezone.utc))},
|
||||
),
|
||||
httpx.Response(
|
||||
429,
|
||||
json={"error": {"code": "rate", "message": "wait", "retryable": True}},
|
||||
headers={"Retry-After": "8"},
|
||||
),
|
||||
httpx.Response(200, json=job()),
|
||||
httpx.Response(200, content=wav_bytes()),
|
||||
]
|
||||
)
|
||||
invoke(handler(clock), lambda request: next(responses))
|
||||
assert clock.sleeps == [12, 8]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("status", ["failed", "cancelled"])
|
||||
def test_terminal_failure_is_not_http_success(invoke, status):
|
||||
calls = []
|
||||
completed = []
|
||||
|
||||
def respond(request):
|
||||
calls.append(request)
|
||||
return httpx.Response(
|
||||
200, json=job(status, error={"code": "stopped", "message": "stopped", "retryable": False})
|
||||
)
|
||||
|
||||
with pytest.raises(FlowSpeechException, match=f"job {status}.*{REQUEST_ID}.*{JOB_ID}"):
|
||||
invoke(handler(Clock(), on_completed=completed.append), respond)
|
||||
assert len(calls) == 1
|
||||
assert completed == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"payload,match",
|
||||
[
|
||||
(job(result=None, resultDeleted=True), "deleted"),
|
||||
(job(result=None), "no result"),
|
||||
(job(status="mystery"), "invalid job"),
|
||||
(job(id="../other"), "invalid job"),
|
||||
({"status": "queued"}, "invalid job"),
|
||||
(job(result={"url": AUDIO_URL}), "invalid job"),
|
||||
(job(result={**job()["result"], "sampleRate": 48000}), "invalid job"),
|
||||
({"code": 0, "data": {"audioBase64": "test"}}, "invalid job"),
|
||||
],
|
||||
)
|
||||
def test_bad_job_responses(invoke, payload, match):
|
||||
with pytest.raises(FlowSpeechException, match=match):
|
||||
invoke(handler(Clock()), lambda request: httpx.Response(200, json=payload))
|
||||
|
||||
|
||||
def test_rejects_changed_job_id(invoke):
|
||||
responses = iter([httpx.Response(202, json=job("queued")), httpx.Response(200, json=job(id=REQUEST_ID))])
|
||||
with pytest.raises(FlowSpeechException, match="different job ID"):
|
||||
invoke(handler(Clock()), lambda request: next(responses))
|
||||
|
||||
|
||||
@pytest.mark.parametrize("poll", [False, True])
|
||||
@pytest.mark.parametrize(
|
||||
"request_id", [None, 7, "invalid", REQUEST_ID.replace("-", ""), "{" + REQUEST_ID + "}", JOB_ID]
|
||||
)
|
||||
def test_rejects_invalid_or_mismatched_request_id_before_audio(invoke, poll, request_id):
|
||||
calls, completed = [], []
|
||||
responses = iter(([job("queued")] if poll else []) + [job(requestId=request_id)])
|
||||
|
||||
def respond(request):
|
||||
calls.append(request)
|
||||
assert request.url.host == "flowspeech.io"
|
||||
return httpx.Response(200, json=next(responses))
|
||||
|
||||
with pytest.raises(FlowSpeechException):
|
||||
invoke(handler(Clock(), on_completed=completed.append), respond)
|
||||
assert len(calls) == (2 if poll else 1) and completed == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize("poll", [False, True])
|
||||
@pytest.mark.parametrize("status", ["queued", "running", "merging", "settling", "success", "failed", "cancelled"])
|
||||
def test_identity_checked_for_every_job_state(invoke, poll, status):
|
||||
responses = iter(([job("queued")] if poll else []) + [job(status, requestId=JOB_ID)])
|
||||
with pytest.raises(FlowSpeechException, match="different request ID"):
|
||||
invoke(handler(Clock()), lambda request: httpx.Response(200, json=next(responses)))
|
||||
|
||||
|
||||
@pytest.mark.parametrize("poll", [False, True])
|
||||
@pytest.mark.parametrize("field", ["requestId", "usage"])
|
||||
def test_job_requires_identity_and_usage(invoke, poll, field):
|
||||
payload = {key: value for key, value in job().items() if key != field}
|
||||
responses = iter(([job("queued")] if poll else []) + [payload])
|
||||
with pytest.raises(FlowSpeechException, match="invalid job"):
|
||||
invoke(handler(Clock()), lambda request: httpx.Response(200, json=next(responses)))
|
||||
|
||||
|
||||
@pytest.mark.parametrize("poll", [False, True])
|
||||
@pytest.mark.parametrize("field", ["billableCharacters", "reservedCredits", "chargedCredits"])
|
||||
@pytest.mark.parametrize("value", [None, -1, True, "2", 1.5, float("nan"), float("inf")])
|
||||
def test_rejects_invalid_usage_on_every_response(invoke, poll, field, value):
|
||||
payload = job(usage={**job()["usage"], field: value})
|
||||
responses = iter(([job("queued")] if poll else []) + [payload])
|
||||
|
||||
def respond(request):
|
||||
assert request.url.host == "flowspeech.io"
|
||||
return httpx.Response(200, content=json.dumps(next(responses)))
|
||||
|
||||
with pytest.raises(FlowSpeechException, match="invalid job"):
|
||||
invoke(handler(Clock()), respond)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("usage", [None, {}, {"billableCharacters": 2, "reservedCredits": 0}])
|
||||
def test_rejects_missing_usage_counters(invoke, usage):
|
||||
with pytest.raises(FlowSpeechException, match="invalid job"):
|
||||
invoke(handler(Clock()), lambda request: httpx.Response(200, json=job(usage=usage)))
|
||||
|
||||
|
||||
def test_uppercase_response_ids_are_normalized_and_completion_follows_audio(invoke):
|
||||
completed = []
|
||||
mixed_case_id = "aa111111-2222-4333-8444-555555555555"
|
||||
responses = iter(
|
||||
[
|
||||
job("queued", requestId=REQUEST_ID.upper(), id=mixed_case_id.upper()),
|
||||
job(requestId=REQUEST_ID.upper(), id=mixed_case_id),
|
||||
]
|
||||
)
|
||||
|
||||
def respond(request):
|
||||
assert completed == []
|
||||
if request.url.host == "audio.example.com":
|
||||
return httpx.Response(200, content=wav_bytes())
|
||||
return httpx.Response(200, json=next(responses))
|
||||
|
||||
result = invoke(handler(Clock(), on_completed=completed.append), respond)
|
||||
assert result.content == wav_bytes() and len(completed) == 1
|
||||
assert completed[0].requestId == REQUEST_ID and completed[0].id == mixed_case_id
|
||||
|
||||
|
||||
def test_invalid_wav_never_calls_completion(invoke):
|
||||
completed = []
|
||||
responses = iter([httpx.Response(200, json=job()), httpx.Response(200, content=b"RIFF-invalid")])
|
||||
with pytest.raises(FlowSpeechException):
|
||||
invoke(handler(Clock(), on_completed=completed.append), lambda request: next(responses))
|
||||
assert completed == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"url",
|
||||
[
|
||||
"http://audio.example.com/a",
|
||||
"https://user:pass@audio.example.com/a",
|
||||
"file:///etc/passwd",
|
||||
"https://audio.example.com/a#fragment",
|
||||
],
|
||||
)
|
||||
def test_rejects_unsafe_download_url(invoke, url):
|
||||
with pytest.raises(FlowSpeechException, match="unsafe audio URL"):
|
||||
invoke(handler(Clock()), lambda request: httpx.Response(200, json=job(result={**job()["result"], "url": url})))
|
||||
|
||||
|
||||
@pytest.mark.parametrize("download", [False, True])
|
||||
def test_redirects_rejected_without_following(invoke, download):
|
||||
calls = []
|
||||
|
||||
def respond(request):
|
||||
calls.append(request)
|
||||
if download and len(calls) == 1:
|
||||
return httpx.Response(200, json=job())
|
||||
return httpx.Response(307, headers={"Location": "https://untrusted.example/collect"})
|
||||
|
||||
with pytest.raises(FlowSpeechException, match="redirect"):
|
||||
invoke(handler(Clock()), respond)
|
||||
assert len(calls) == (2 if download else 1)
|
||||
|
||||
|
||||
def test_download_retries_do_not_create_another_job(invoke):
|
||||
calls = []
|
||||
completed = []
|
||||
|
||||
def respond(request):
|
||||
calls.append(request)
|
||||
if len(calls) == 1:
|
||||
return httpx.Response(200, json=job())
|
||||
if len(calls) == 2:
|
||||
raise httpx.ReadError("download interrupted")
|
||||
return httpx.Response(200, content=wav_bytes())
|
||||
|
||||
assert invoke(handler(Clock(), on_completed=completed.append), respond).content == wav_bytes()
|
||||
assert [request.method for request in calls] == ["POST", "GET", "GET"]
|
||||
assert calls[1].url == calls[2].url
|
||||
assert len(completed) == 1
|
||||
|
||||
|
||||
def test_download_exhaustion_has_recovery_ids(invoke):
|
||||
calls = []
|
||||
|
||||
def respond(request):
|
||||
calls.append(request)
|
||||
if request.method == "POST":
|
||||
return httpx.Response(200, json=job())
|
||||
return httpx.Response(503)
|
||||
|
||||
with pytest.raises(FlowSpeechException, match=f"download.*{REQUEST_ID}.*{JOB_ID}"):
|
||||
invoke(handler(Clock(), max_retries=1), respond)
|
||||
assert [request.method for request in calls] == ["POST", "GET", "GET"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("status", [400, 401, 402, 403, 404, 409, 413, 415, 503])
|
||||
def test_api_errors_preserve_code_trace_and_no_retry(invoke, status):
|
||||
calls = []
|
||||
|
||||
def respond(request):
|
||||
calls.append(request)
|
||||
return httpx.Response(
|
||||
status,
|
||||
json={
|
||||
"error": {"code": "test_error", "message": "test message", "retryable": False},
|
||||
"traceId": "trace-id",
|
||||
},
|
||||
)
|
||||
|
||||
with pytest.raises(FlowSpeechException, match=f"test_error.*trace-id.*{REQUEST_ID}") as exc:
|
||||
invoke(handler(Clock()), respond)
|
||||
assert exc.value.status_code == status
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
def test_deadline_before_long_retry_after(invoke):
|
||||
clock = Clock()
|
||||
with pytest.raises(FlowSpeechException, match="deadline"):
|
||||
invoke(
|
||||
handler(clock, timeout=10),
|
||||
lambda request: httpx.Response(202, json=job("queued"), headers={"Retry-After": "30"}),
|
||||
)
|
||||
assert clock.sleeps == []
|
||||
|
||||
|
||||
def test_deadline_during_request(invoke):
|
||||
clock = Clock()
|
||||
|
||||
def respond(request):
|
||||
clock.now = 11
|
||||
return httpx.Response(202, json=job("queued"))
|
||||
|
||||
with pytest.raises(FlowSpeechException, match="deadline"):
|
||||
invoke(handler(clock, timeout=10), respond)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("stage", ["submission", "download"])
|
||||
@pytest.mark.parametrize("header_delay,body_delay,expected_reads", [(0.125, 0, 0), (0.075, 0.075, 1)])
|
||||
def test_expired_stream_is_closed_without_more_work(invoke, stage, header_delay, body_delay, expected_reads):
|
||||
clock = Clock()
|
||||
completed = []
|
||||
requests = []
|
||||
|
||||
class Stream(httpx.SyncByteStream, httpx.AsyncByteStream):
|
||||
def __init__(self):
|
||||
self.reads = 0
|
||||
self.closed = False
|
||||
|
||||
def __iter__(self):
|
||||
self.reads += 1
|
||||
clock.now += body_delay
|
||||
yield wav_bytes() if stage == "download" else json.dumps(job()).encode()
|
||||
pytest.fail("must not advance a stream after its budget expires")
|
||||
|
||||
async def __aiter__(self):
|
||||
for chunk in self:
|
||||
yield chunk
|
||||
|
||||
def close(self):
|
||||
self.closed = True
|
||||
|
||||
async def aclose(self):
|
||||
self.close()
|
||||
|
||||
stream = Stream()
|
||||
|
||||
def respond(request):
|
||||
requests.append(request)
|
||||
if stage == "download" and request.method == "POST":
|
||||
return httpx.Response(202, json=job())
|
||||
clock.now += header_delay
|
||||
return httpx.Response(200, stream=stream)
|
||||
|
||||
with pytest.raises(FlowSpeechException, match="total deadline") as error:
|
||||
invoke(handler(clock, timeout=0.1, on_completed=completed.append), respond)
|
||||
assert error.value.status_code == 408
|
||||
assert REQUEST_ID in str(error.value)
|
||||
assert f"job_id={JOB_ID if stage == 'download' else 'unknown'}" in str(error.value)
|
||||
assert stream.reads == expected_reads
|
||||
assert stream.closed
|
||||
assert [request.method for request in requests] == (["POST", "GET"] if stage == "download" else ["POST"])
|
||||
assert clock.sleeps == []
|
||||
assert completed == []
|
||||
|
||||
|
||||
def test_deadline_after_wav_validation_prevents_success_callback(invoke, monkeypatch):
|
||||
clock = Clock()
|
||||
completed = []
|
||||
content = wav_bytes()
|
||||
open_wav = wave.open
|
||||
|
||||
def delayed_open(*args, **kwargs):
|
||||
clock.now = 11
|
||||
return open_wav(*args, **kwargs)
|
||||
|
||||
monkeypatch.setattr(wave, "open", delayed_open)
|
||||
responses = iter([httpx.Response(202, json=job()), httpx.Response(200, content=content)])
|
||||
with pytest.raises(FlowSpeechException, match="total deadline"):
|
||||
invoke(handler(clock, timeout=10, on_completed=completed.append), lambda request: next(responses))
|
||||
assert completed == []
|
||||
assert clock.sleeps == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize("timeout", [0, -1, float("inf"), float("nan"), httpx.Timeout(None)])
|
||||
def test_invalid_timeout(timeout):
|
||||
with pytest.raises(ValueError, match="timeout"):
|
||||
handler(Clock(), timeout=timeout)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("retries", [-1, 11, 1.5, True])
|
||||
def test_invalid_retries(retries):
|
||||
with pytest.raises(ValueError, match="max_retries"):
|
||||
handler(Clock(), max_retries=retries)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"content,headers",
|
||||
[
|
||||
(b"not wav", {}),
|
||||
(wav_bytes(b""), {}),
|
||||
(wav_bytes()[:-2], {}),
|
||||
(wav_bytes(b"\x00" * 49), {}),
|
||||
(changed_wav(4, struct.pack("<I", 9999)), {}),
|
||||
(changed_wav(34, struct.pack("<H", 9)), {}),
|
||||
(changed_wav(32, struct.pack("<H", 4)), {}),
|
||||
(changed_wav(28, struct.pack("<I", 24000)), {}),
|
||||
(resized_riff(wav_bytes() + b"x"), {}),
|
||||
(resized_riff(wav_bytes() + b"JUNK" + struct.pack("<I", 5)), {}),
|
||||
(resized_riff(wav_bytes(b"\x00" * 49) + b"\x00"), {}),
|
||||
(resized_riff(wav_bytes()[:-2]), {}),
|
||||
(wav_bytes(), {"content-length": str(101 * 1024 * 1024)}),
|
||||
(wav_bytes(), {"content-length": "invalid"}),
|
||||
(wav_bytes(), {"content-encoding": "unknown"}),
|
||||
],
|
||||
)
|
||||
def test_rejects_invalid_or_unbounded_audio(invoke, content, headers):
|
||||
responses = iter([httpx.Response(200, json=job()), httpx.Response(200, content=content, headers=headers)])
|
||||
with pytest.raises(FlowSpeechException):
|
||||
invoke(handler(Clock()), lambda request: next(responses))
|
||||
|
||||
|
||||
def test_accepts_wav_with_padded_metadata_chunk(invoke):
|
||||
audio = wav_bytes(metadata=True)
|
||||
responses = iter([httpx.Response(200, json=job()), httpx.Response(200, content=audio)])
|
||||
assert invoke(handler(Clock()), lambda request: next(responses)).content == audio
|
||||
|
||||
|
||||
def test_stream_size_limit_without_content_length(invoke):
|
||||
tts = handler(Clock())
|
||||
tts.MAX_AUDIO_BYTES = 10
|
||||
responses = iter([httpx.Response(200, json=job()), httpx.Response(200, content=wav_bytes())])
|
||||
|
||||
def respond(request):
|
||||
response = next(responses)
|
||||
response.headers.pop("content-length", None)
|
||||
return response
|
||||
|
||||
with pytest.raises(FlowSpeechException, match="size limit"):
|
||||
invoke(tts, respond)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("asynchronous", [False, True])
|
||||
@pytest.mark.parametrize("owned", [False, True])
|
||||
@pytest.mark.parametrize("successful", [False, True])
|
||||
@pytest.mark.asyncio
|
||||
async def test_client_lifetime(monkeypatch, asynchronous, owned, successful):
|
||||
methods = []
|
||||
|
||||
def respond(request):
|
||||
methods.append(request.method)
|
||||
if request.method == "POST":
|
||||
return httpx.Response(202, json=job("success" if successful else "failed"))
|
||||
return httpx.Response(200, content=wav_bytes())
|
||||
|
||||
client_type = httpx.AsyncClient if asynchronous else httpx.Client
|
||||
client = client_type(transport=httpx.MockTransport(respond))
|
||||
|
||||
def create_client(*, trust_env):
|
||||
assert trust_env is False
|
||||
return client
|
||||
|
||||
if owned:
|
||||
if asynchronous:
|
||||
get_async_httpx_client(LlmProviders.FLOWSPEECH).client = client
|
||||
else:
|
||||
monkeypatch.setattr(httpx, "Client", create_client)
|
||||
tts = handler(Clock())
|
||||
|
||||
async def call():
|
||||
if asynchronous:
|
||||
return await tts.arun(None if owned else client)
|
||||
return tts.run(None if owned else client)
|
||||
|
||||
try:
|
||||
if successful:
|
||||
result = await call()
|
||||
assert result.content == wav_bytes()
|
||||
else:
|
||||
with pytest.raises(FlowSpeechException, match="FlowSpeech job failed"):
|
||||
await call()
|
||||
assert methods == (["POST", "GET"] if successful else ["POST"])
|
||||
assert client.is_closed is (owned and not asynchronous)
|
||||
finally:
|
||||
if asynchronous:
|
||||
await client.aclose()
|
||||
else:
|
||||
client.close()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_default_async_client_is_reused_without_download_credentials():
|
||||
requests = []
|
||||
|
||||
def respond(request):
|
||||
requests.append(request)
|
||||
if request.method == "POST":
|
||||
return httpx.Response(202, json=job())
|
||||
assert "authorization" not in request.headers
|
||||
assert "cookie" not in request.headers
|
||||
assert "x-client-secret" not in request.headers
|
||||
return httpx.Response(200, content=wav_bytes())
|
||||
|
||||
cached = get_async_httpx_client(LlmProviders.FLOWSPEECH)
|
||||
async with httpx.AsyncClient(
|
||||
transport=httpx.MockTransport(respond),
|
||||
headers={"Authorization": "discard", "X-Client-Secret": "discard"},
|
||||
cookies={"session": "discard"},
|
||||
) as client:
|
||||
cached.client = client
|
||||
for _ in range(2):
|
||||
result = await handler(Clock()).arun()
|
||||
assert result.content == wav_bytes()
|
||||
assert not client.is_closed
|
||||
assert get_async_httpx_client(LlmProviders.FLOWSPEECH).client is client
|
||||
assert [request.method for request in requests] == ["POST", "GET", "POST", "GET"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_total_deadline_interrupts_network():
|
||||
async def respond(request):
|
||||
await asyncio.sleep(1)
|
||||
return httpx.Response(202, json=job("queued"))
|
||||
|
||||
tts = FlowSpeechTTS(URL, {"Idempotency-Key": REQUEST_ID}, {"input": {}}, timeout=0.02)
|
||||
async with httpx.AsyncClient(transport=httpx.MockTransport(respond)) as client:
|
||||
with pytest.raises(FlowSpeechException, match="total deadline"):
|
||||
await tts.arun(client)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_cancellation_propagates():
|
||||
started = asyncio.Event()
|
||||
|
||||
async def respond(request):
|
||||
started.set()
|
||||
await asyncio.sleep(100)
|
||||
return httpx.Response(200, json=job())
|
||||
|
||||
async with httpx.AsyncClient(transport=httpx.MockTransport(respond)) as client:
|
||||
task = asyncio.create_task(handler(Clock()).arun(client))
|
||||
await started.wait()
|
||||
task.cancel()
|
||||
with pytest.raises(asyncio.CancelledError):
|
||||
await task
|
||||
578
tests/unit/llms/flowspeech/test_text_to_speech_transformation.py
Normal file
578
tests/unit/llms/flowspeech/test_text_to_speech_transformation.py
Normal file
|
|
@ -0,0 +1,578 @@
|
|||
import io
|
||||
import json
|
||||
import logging
|
||||
import traceback
|
||||
import wave
|
||||
from typing import Final
|
||||
from uuid import UUID
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
import litellm
|
||||
from litellm.integrations.custom_logger import CustomLogger
|
||||
from litellm.llms.custom_httpx.http_handler import HTTPHandler
|
||||
from litellm.llms.flowspeech.text_to_speech.transformation import FlowSpeechException, FlowSpeechTextToSpeechConfig
|
||||
|
||||
REQUEST_ID = "b698824b-de05-486d-8833-05d5c4522a39"
|
||||
JOB_ID = "11111111-2222-4333-8444-555555555555"
|
||||
SECRET_SENTINEL = "credential-value-must-not-appear-672911"
|
||||
|
||||
|
||||
def assert_clean_exception_chain(error: BaseException | None, seen: frozenset[int] = frozenset()) -> None:
|
||||
if error is None or id(error) in seen:
|
||||
return
|
||||
assert SECRET_SENTINEL not in repr(error.args)
|
||||
assert SECRET_SENTINEL not in repr(vars(error))
|
||||
assert SECRET_SENTINEL not in "".join(traceback.format_exception(error))
|
||||
request: Final = getattr(error, "request", None)
|
||||
if isinstance(request, httpx.Request):
|
||||
assert SECRET_SENTINEL not in str(request.url) + repr(request.headers) + repr(request.content)
|
||||
assert_clean_exception_chain(error.__cause__, seen | {id(error)})
|
||||
assert_clean_exception_chain(error.__context__, seen | {id(error)})
|
||||
|
||||
|
||||
class FailureRecorder(CustomLogger):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
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()
|
||||
|
|
@ -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"),
|
||||
[
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import base64
|
|||
import copy
|
||||
import contextlib
|
||||
import contextvars
|
||||
import inspect
|
||||
import io
|
||||
import json
|
||||
import logging
|
||||
|
|
@ -6986,6 +6987,169 @@ class TestVerboseRequestLineRedaction:
|
|||
|
||||
FAKE_API_KEY: Final = "sk-fake-lit6823-0000000000000000"
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"function_name", ["speech", "aspeech", "embedding", "aembedding", "completion", "acompletion"]
|
||||
)
|
||||
@pytest.mark.parametrize("verbose", [False, True])
|
||||
def test_positional_credentials_follow_public_signatures(
|
||||
self,
|
||||
function_name: str,
|
||||
verbose: bool,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
capsys: pytest.CaptureFixture[str],
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
from litellm.utils import print_args_passed_to_litellm
|
||||
|
||||
function: Final = getattr(litellm, function_name)
|
||||
signature_name: Final = function_name[1:] if function_name in ("aspeech", "aembedding") else function_name
|
||||
signature_function: Final = getattr(litellm, signature_name)
|
||||
sentinel: Final = "SYNTHETIC-POSITIONAL-KEY-ONLY"
|
||||
input_value: Final = "synthetic-input" if signature_name in ("speech", "embedding") else []
|
||||
bound: Final = inspect.signature(signature_function).bind_partial(
|
||||
"synthetic-model", input_value, api_key=sentinel
|
||||
)
|
||||
bound.apply_defaults()
|
||||
args: Final = bound.args
|
||||
monkeypatch.setattr(litellm, "set_verbose", verbose)
|
||||
monkeypatch.setattr("litellm._logging.set_verbose", verbose)
|
||||
with caplog.at_level(logging.DEBUG if not verbose else logging.WARNING, logger=verbose_logger.name):
|
||||
print_args_passed_to_litellm(function, args, {"max_retries": 0})
|
||||
captured: Final = capsys.readouterr()
|
||||
output: Final = caplog.text + captured.out + captured.err
|
||||
assert f"litellm.{function_name}(" in output
|
||||
assert "synthetic-model" in output
|
||||
assert "max_retries=0" in output
|
||||
assert "REDACTED" in output
|
||||
assert sentinel not in output
|
||||
assert sentinel in args
|
||||
|
||||
@pytest.mark.parametrize("field", ["headers", "extra_headers"])
|
||||
@pytest.mark.parametrize("malformed", ["synthetic-header-value", {b"synthetic-header-value": "unused"}])
|
||||
def test_malformed_headers_do_not_reach_request_line(
|
||||
self,
|
||||
field: str,
|
||||
malformed: object,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
from litellm.utils import print_args_passed_to_litellm
|
||||
|
||||
with caplog.at_level(logging.DEBUG, logger=verbose_logger.name):
|
||||
print_args_passed_to_litellm(litellm.completion, (), {"model": "synthetic-model", field: malformed})
|
||||
assert "synthetic-header-value" not in caplog.text
|
||||
assert f"{field}='REDACTED'" in caplog.text
|
||||
assert "model='synthetic-model'" in caplog.text
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("asynchronous", [False, True])
|
||||
@pytest.mark.parametrize("positional", [False, True])
|
||||
async def test_non_speech_mock_completion_preserves_result_and_context(
|
||||
self,
|
||||
asynchronous: bool,
|
||||
positional: bool,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
function: Final = litellm.acompletion if asynchronous else litellm.completion
|
||||
params: Final = {
|
||||
"model": "openai/synthetic-model",
|
||||
"messages": [{"role": "user", "content": "synthetic input"}],
|
||||
"api_key": self.FAKE_API_KEY,
|
||||
"temperature": 0.25,
|
||||
"extra_headers": {"Authorization": self.FAKE_API_KEY, "X-Request-Id": "safe-correlation"},
|
||||
}
|
||||
bound: Final = inspect.signature(function).bind_partial(**params)
|
||||
bound.apply_defaults()
|
||||
args: Final = bound.args if positional else ()
|
||||
kwargs: Final = bound.kwargs if positional else params
|
||||
with caplog.at_level(logging.DEBUG, logger=verbose_logger.name):
|
||||
response: Final = (
|
||||
await litellm.acompletion(*args, **kwargs, mock_response="synthetic output")
|
||||
if asynchronous
|
||||
else litellm.completion(*args, **kwargs, mock_response="synthetic output")
|
||||
)
|
||||
assert response.choices[0].message.content == "synthetic output"
|
||||
assert self.FAKE_API_KEY not in caplog.text
|
||||
assert "safe-correlation" in caplog.text
|
||||
assert "synthetic input" in caplog.text
|
||||
assert "0.25" in caplog.text
|
||||
assert params["api_key"] == self.FAKE_API_KEY
|
||||
assert params["extra_headers"]["Authorization"] == self.FAKE_API_KEY
|
||||
|
||||
def test_unidentified_varargs_are_hidden_without_losing_keyword_context(
|
||||
self,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
from litellm.utils import print_args_passed_to_litellm
|
||||
|
||||
def opaque(*args: object, **kwargs: object) -> None:
|
||||
pass
|
||||
|
||||
with caplog.at_level(logging.DEBUG, logger=verbose_logger.name):
|
||||
print_args_passed_to_litellm(opaque, (self.FAKE_API_KEY,), {"model": "synthetic-model"})
|
||||
assert self.FAKE_API_KEY not in caplog.text
|
||||
assert "litellm.opaque('REDACTED', model='synthetic-model')" in caplog.text
|
||||
|
||||
@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)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue