fix(byteplus): comply with type discipline LIT001 and LIT002 rules

This commit is contained in:
agungbesti 2026-09-15 04:01:16 +07:00
parent ea446dc3c1
commit b4232ec3fd
13 changed files with 299 additions and 264 deletions

View file

@ -14,9 +14,11 @@ from .image_generation.transformation import BytePlusImageGenerationConfig
from .responses.transformation import BytePlusResponsesAPIConfig
from .text_to_speech.transformation import BytePlusTextToSpeechConfig
BytePlusConfig = BytePlusChatConfig
from typing import Final
__all__ = [
BytePlusConfig: Final = BytePlusChatConfig
__all__ = (
"BytePlusChatConfig",
"BytePlusConfig",
"BytePlusEmbeddingConfig",
@ -26,4 +28,4 @@ __all__ = [
"BytePlusTextToSpeechConfig",
"get_byteplus_base_url",
"get_byteplus_headers",
]
)

View file

@ -1,3 +1,3 @@
from .transformation import BytePlusChatConfig
__all__ = ["BytePlusChatConfig"]
__all__ = ("BytePlusChatConfig",)

View file

@ -1,3 +1,5 @@
from typing import Final
from litellm.llms.openai_like.chat.transformation import OpenAILikeChatConfig
@ -6,43 +8,14 @@ class BytePlusChatConfig(OpenAILikeChatConfig):
Reference: https://docs.byteplus.com/en/docs/ModelArk
"""
frequency_penalty: int | None = None
function_call: str | dict | None = None
functions: list | None = None
logit_bias: dict | None = None
max_tokens: int | None = None
n: int | None = None
presence_penalty: int | None = None
stop: str | list | None = None
temperature: int | None = None
top_p: int | None = None
response_format: dict | None = None
def __init__(
self,
frequency_penalty: int | None = None,
function_call: str | dict | None = None,
functions: list | None = None,
logit_bias: dict | None = None,
max_tokens: int | None = None,
n: int | None = None,
presence_penalty: int | None = None,
stop: str | list | None = None,
temperature: int | None = None,
top_p: int | None = None,
response_format: dict | None = None,
) -> None:
locals_ = locals().copy()
for key, value in locals_.items():
if key != "self" and value is not None:
setattr(self.__class__, key, value)
@classmethod
def get_config(cls) -> "BytePlusChatConfig":
return super().get_config()
def get_supported_openai_params(self, model: str) -> list:
return [
def get_supported_openai_params(
self, model: str
) -> list: # mutable-ok: matches BaseConfig interface
return [ # mutable-ok: matches BaseConfig interface
"frequency_penalty",
"logit_bias",
"logprobs",
@ -68,13 +41,13 @@ class BytePlusChatConfig(OpenAILikeChatConfig):
def map_openai_params(
self,
non_default_params: dict,
optional_params: dict,
non_default_params: dict, # mutable-ok: matches BaseConfig interface
optional_params: dict, # mutable-ok: matches BaseConfig interface
model: str,
drop_params: bool,
replace_max_completion_tokens_with_max_tokens: bool = True,
) -> dict:
mapped_params = super().map_openai_params(
) -> dict: # mutable-ok: matches BaseConfig interface
mapped_params: Final = super().map_openai_params(
non_default_params,
optional_params,
model,
@ -83,11 +56,12 @@ class BytePlusChatConfig(OpenAILikeChatConfig):
)
if "thinking" in mapped_params:
thinking_val = mapped_params.pop("thinking", None)
thinking_val: Final = mapped_params.pop("thinking", None)
if thinking_val is not None:
if isinstance(thinking_val, bool):
mapped_params["extra_body"] = {"thinking": {"type": "enabled" if thinking_val else "disabled"}}
mapped_params["extra_body"] = {"thinking": {"type": "enabled" if thinking_val else "disabled"}} # mutable-ok: extra_body payload dict
elif isinstance(thinking_val, dict):
mapped_params["extra_body"] = {"thinking": thinking_val}
mapped_params["extra_body"] = {"thinking": thinking_val} # mutable-ok: extra_body payload dict
return mapped_params

View file

@ -1,3 +1,8 @@
from __future__ import annotations
from collections.abc import Mapping
from typing import Final
import httpx
from litellm.llms.base_llm.chat.transformation import BaseLLMException
@ -5,10 +10,10 @@ from litellm.llms.base_llm.chat.transformation import BaseLLMException
class BytePlusError(BaseLLMException):
def __init__(self, status_code: int, message: str, headers: httpx.Headers | None = None) -> None:
self.status_code = status_code
self.message = message
self.headers = headers or httpx.Headers()
super().__init__(status_code=status_code, message=message, headers=dict(self.headers))
self.status_code: Final = status_code
self.message: Final = message
self.headers: Final = headers or httpx.Headers()
super().__init__(status_code=status_code, message=message, headers=self.headers)
def get_byteplus_base_url(api_base: str | None = None) -> str:
@ -17,8 +22,10 @@ def get_byteplus_base_url(api_base: str | None = None) -> str:
return "https://ark.ap-southeast.bytepluses.com"
def get_byteplus_headers(api_key: str, extra_headers: dict | None = None) -> dict:
headers = {
def get_byteplus_headers(
api_key: str, extra_headers: Mapping[str, str] | None = None
) -> dict[str, str]: # mutable-ok: required by HTTP handler signatures
headers: Final[dict[str, str]] = { # mutable-ok: mutable dictionary required by HTTP client
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}",
}
@ -27,3 +34,4 @@ def get_byteplus_headers(api_key: str, extra_headers: dict | None = None) -> dic
headers.update(extra_headers)
return headers

View file

@ -1,3 +1,3 @@
from .transformation import BytePlusEmbeddingConfig
__all__ = ["BytePlusEmbeddingConfig"]
__all__ = ("BytePlusEmbeddingConfig",)

View file

@ -1,4 +1,7 @@
from __future__ import annotations
import json
from collections.abc import Mapping, Sequence
from typing import TYPE_CHECKING, Final
import httpx
@ -25,21 +28,14 @@ class BytePlusEmbeddingConfig(BaseEmbeddingConfig):
Reference: https://docs.byteplus.com/en/docs/ModelArk
"""
def __init__(
self,
encoding_format: str | None = None,
) -> None:
locals_ = locals().copy()
for key, value in locals_.items():
if key != "self" and value is not None:
setattr(self.__class__, key, value)
@classmethod
def get_config(cls) -> "BytePlusEmbeddingConfig":
return super().get_config()
def get_supported_openai_params(self, model: str) -> list[str]:
return [
def get_supported_openai_params(
self, model: str
) -> list[str]: # mutable-ok: matches BaseEmbeddingConfig interface
return [ # mutable-ok: matches BaseEmbeddingConfig interface
"encoding_format",
"user",
"extra_headers",
@ -53,24 +49,23 @@ class BytePlusEmbeddingConfig(BaseEmbeddingConfig):
api_base: str | None,
api_key: str | None,
model: str,
optional_params: dict,
litellm_params: dict,
optional_params: dict, # mutable-ok: matches BaseEmbeddingConfig interface
litellm_params: dict, # mutable-ok: matches BaseEmbeddingConfig interface
stream: bool | None = None,
) -> str:
base_url = (
base_url: Final = (
api_base
or litellm.api_base
or get_secret_str("BYTEPLUS_API_BASE")
or get_secret_str("ARK_API_BASE")
or get_byteplus_base_url()
)
base_url = base_url.rstrip("/")
).rstrip("/")
is_multimodal = (
is_multimodal: Final = (
"vision" in model.lower() or "multimodal" in model.lower() or optional_params.get("is_multimodal", False)
)
endpoint = "/embeddings/multimodal" if is_multimodal else "/embeddings"
endpoint: Final = "/embeddings/multimodal" if is_multimodal else "/embeddings"
if base_url.endswith(endpoint):
return base_url
@ -80,48 +75,48 @@ class BytePlusEmbeddingConfig(BaseEmbeddingConfig):
def map_openai_params(
self,
non_default_params: dict,
optional_params: dict,
non_default_params: dict, # mutable-ok: matches BaseEmbeddingConfig interface
optional_params: dict, # mutable-ok: matches BaseEmbeddingConfig interface
model: str,
drop_params: bool,
) -> dict:
supported = self.get_supported_openai_params(model)
optional_params.update({k: v for k, v in non_default_params.items() if k in supported})
) -> dict: # mutable-ok: matches BaseEmbeddingConfig interface
supported: Final = frozenset(self.get_supported_openai_params(model))
optional_params.update({k: v for k, v in non_default_params.items() if k in supported}) # mutable-ok: update params dict
return optional_params
def transform_embedding_request(
self,
model: str,
input: AllEmbeddingInputValues,
optional_params: dict,
headers: dict,
) -> dict:
is_multimodal = (
optional_params: dict, # mutable-ok: matches BaseEmbeddingConfig interface
headers: dict, # mutable-ok: matches BaseEmbeddingConfig interface
) -> dict: # mutable-ok: matches BaseEmbeddingConfig interface
is_multimodal: Final = (
"vision" in model.lower() or "multimodal" in model.lower() or optional_params.get("is_multimodal", False)
)
raw_input = input if isinstance(input, list) else [input]
formatted_input: list = []
raw_input: Final = input if isinstance(input, (list, tuple)) else (input,)
formatted_input: Final[list] = [] # mutable-ok: building input list for json payload
if is_multimodal:
for item in raw_input:
if isinstance(item, str):
formatted_input.append({"type": "text", "text": item})
formatted_input.append({"type": "text", "text": item}) # mutable-ok: payload dict item
else:
formatted_input.append(item)
else:
formatted_input = list(raw_input)
formatted_input.extend(raw_input)
data: dict = {
data: Final[dict[str, object]] = { # mutable-ok: request body dictionary
"model": model,
"input": formatted_input,
}
for key in ["encoding_format", "dimensions", "instructions", "sparse_embedding", "user"]:
for key in ("encoding_format", "dimensions", "instructions", "sparse_embedding", "user"):
if key in optional_params and optional_params[key] is not None:
data[key] = optional_params[key]
if "extra_body" in optional_params and isinstance(optional_params["extra_body"], dict):
extra_body: Final = {k: v for k, v in optional_params["extra_body"].items() if k not in ("model", "input")}
extra_body: Final = {k: v for k, v in optional_params["extra_body"].items() if k not in ("model", "input")} # mutable-ok: extra_body dictionary
data.update(extra_body)
return data
@ -133,36 +128,36 @@ class BytePlusEmbeddingConfig(BaseEmbeddingConfig):
model_response: EmbeddingResponse,
logging_obj: "LiteLLMLoggingObj",
api_key: str | None,
request_data: dict,
optional_params: dict,
litellm_params: dict,
request_data: dict, # mutable-ok: matches BaseEmbeddingConfig interface
optional_params: dict, # mutable-ok: matches BaseEmbeddingConfig interface
litellm_params: dict, # mutable-ok: matches BaseEmbeddingConfig interface
) -> EmbeddingResponse:
try:
response_json = raw_response.json()
response_json: Final = raw_response.json()
except (json.JSONDecodeError, ValueError) as e:
raise ValueError(f"Failed to parse BytePlus response as JSON: {e}")
data_raw = response_json.get("data", [])
data_raw: Final = response_json.get("data", ())
if isinstance(data_raw, dict):
embedding_item: dict = {
embedding_item: Final[dict[str, object]] = { # mutable-ok: building embedding response dict
"object": data_raw.get("object", "embedding"),
"embedding": data_raw.get("embedding", []),
"embedding": data_raw.get("embedding", ()),
"index": 0,
}
if "sparse_embedding" in data_raw:
embedding_item["sparse_embedding"] = data_raw["sparse_embedding"]
data_list = [embedding_item]
data_list: Final = [embedding_item] # mutable-ok: single item data list
elif isinstance(data_raw, list):
data_list = data_raw
else:
data_list = []
data_list = [] # mutable-ok: default empty data list
transformed_response = {
transformed_response: Final[dict[str, object]] = { # mutable-ok: building response dict
"object": "list",
"data": data_list,
"model": response_json.get("model", model),
"usage": response_json.get("usage", {}),
"usage": response_json.get("usage") or {}, # mutable-ok: default empty usage dict
}
if "id" in response_json:
@ -172,14 +167,14 @@ class BytePlusEmbeddingConfig(BaseEmbeddingConfig):
def validate_environment(
self,
headers: dict,
headers: dict, # mutable-ok: matches BaseEmbeddingConfig interface
model: str,
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
messages: list[AllMessageValues], # mutable-ok: matches BaseEmbeddingConfig interface
optional_params: dict, # mutable-ok: matches BaseEmbeddingConfig interface
litellm_params: dict, # mutable-ok: matches BaseEmbeddingConfig interface
api_key: str | None = None,
api_base: str | None = None,
) -> dict:
) -> dict: # mutable-ok: matches BaseEmbeddingConfig interface
resolved_api_key: Final = (
api_key or litellm.api_key or get_secret_str("BYTEPLUS_API_KEY") or get_secret_str("ARK_API_KEY")
)
@ -187,6 +182,11 @@ class BytePlusEmbeddingConfig(BaseEmbeddingConfig):
raise ValueError("BytePlus API key is required. Set BYTEPLUS_API_KEY or ARK_API_KEY or pass api_key.")
return get_byteplus_headers(api_key=resolved_api_key, extra_headers=headers)
def get_error_class(self, error_message: str, status_code: int, headers: dict | httpx.Headers) -> BytePlusError:
typed_headers: httpx.Headers = headers if isinstance(headers, httpx.Headers) else httpx.Headers(headers or {})
def get_error_class(
self, error_message: str, status_code: int, headers: dict | httpx.Headers # mutable-ok: matches BaseEmbeddingConfig interface
) -> BytePlusError:
typed_headers: Final[httpx.Headers] = (
headers if isinstance(headers, httpx.Headers) else httpx.Headers(headers)
)
return BytePlusError(status_code=status_code, message=error_message, headers=typed_headers)

View file

@ -2,7 +2,7 @@ from litellm.llms.base_llm.image_generation.transformation import BaseImageGener
from .transformation import BytePlusImageGenerationConfig
__all__ = ["BytePlusImageGenerationConfig", "get_byteplus_image_generation_config"]
__all__ = ("BytePlusImageGenerationConfig", "get_byteplus_image_generation_config")
def get_byteplus_image_generation_config(model: str) -> BaseImageGenerationConfig:

View file

@ -1,3 +1,5 @@
from __future__ import annotations
from typing import TYPE_CHECKING, Final
import httpx
@ -27,31 +29,33 @@ class BytePlusImageGenerationConfig(BaseImageGenerationConfig):
Reference: https://docs.byteplus.com/en/docs/ModelArk/2582774
"""
def get_supported_openai_params(self, model: str) -> list[OpenAIImageGenerationOptionalParams]:
return ["n", "response_format", "size", "user", "quality"]
def get_supported_openai_params(
self, model: str
) -> list[OpenAIImageGenerationOptionalParams]: # mutable-ok: matches BaseImageGenerationConfig interface
return ["n", "response_format", "size", "user", "quality"] # mutable-ok: matches BaseImageGenerationConfig interface
def map_openai_params(
self,
non_default_params: dict,
optional_params: dict,
non_default_params: dict, # mutable-ok: matches BaseImageGenerationConfig interface
optional_params: dict, # mutable-ok: matches BaseImageGenerationConfig interface
model: str,
drop_params: bool,
) -> dict:
supported_params = self.get_supported_openai_params(model)
optional_params.update({k: v for k, v in non_default_params.items() if k in supported_params})
) -> dict: # mutable-ok: matches BaseImageGenerationConfig interface
supported_params: Final = frozenset(self.get_supported_openai_params(model))
optional_params.update({k: v for k, v in non_default_params.items() if k in supported_params}) # mutable-ok: update params dict
return optional_params
def validate_environment(
self,
headers: dict,
headers: dict, # mutable-ok: matches BaseImageGenerationConfig interface
model: str,
messages: list,
optional_params: dict,
litellm_params: dict,
messages: list, # mutable-ok: matches BaseImageGenerationConfig interface
optional_params: dict, # mutable-ok: matches BaseImageGenerationConfig interface
litellm_params: dict, # mutable-ok: matches BaseImageGenerationConfig interface
api_key: str | None = None,
api_base: str | None = None,
) -> dict:
) -> dict: # mutable-ok: matches BaseImageGenerationConfig interface
resolved_api_key: Final = (
api_key or litellm.api_key or get_secret_str("BYTEPLUS_API_KEY") or get_secret_str("ARK_API_KEY")
)
@ -64,26 +68,29 @@ class BytePlusImageGenerationConfig(BaseImageGenerationConfig):
api_base: str | None,
api_key: str | None,
model: str,
optional_params: dict,
litellm_params: dict,
optional_params: dict, # mutable-ok: matches BaseImageGenerationConfig interface
litellm_params: dict, # mutable-ok: matches BaseImageGenerationConfig interface
stream: bool | None = None,
) -> str:
base_url = (
base_url: Final = (
api_base
or litellm.api_base
or get_secret_str("BYTEPLUS_API_BASE")
or get_secret_str("ARK_API_BASE")
or get_byteplus_base_url()
)
base_url = base_url.rstrip("/")
).rstrip("/")
if base_url.endswith("/images/generations"):
return base_url
if base_url.endswith("/api/v3"):
return f"{base_url}/images/generations"
return f"{base_url}/api/v3/images/generations"
def get_error_class(self, error_message: str, status_code: int, headers: dict | httpx.Headers) -> BytePlusError:
typed_headers: httpx.Headers = headers if isinstance(headers, httpx.Headers) else httpx.Headers(headers or {})
def get_error_class(
self, error_message: str, status_code: int, headers: dict | httpx.Headers # mutable-ok: matches BaseImageGenerationConfig interface
) -> BytePlusError:
typed_headers: Final[httpx.Headers] = (
headers if isinstance(headers, httpx.Headers) else httpx.Headers(headers)
)
return BytePlusError(
status_code=status_code,
message=error_message,
@ -94,16 +101,16 @@ class BytePlusImageGenerationConfig(BaseImageGenerationConfig):
self,
model: str,
prompt: str,
optional_params: dict,
litellm_params: dict,
headers: dict,
) -> dict:
body: dict = {
optional_params: dict, # mutable-ok: matches BaseImageGenerationConfig interface
litellm_params: dict, # mutable-ok: matches BaseImageGenerationConfig interface
headers: dict, # mutable-ok: matches BaseImageGenerationConfig interface
) -> dict: # mutable-ok: matches BaseImageGenerationConfig interface
body: Final[dict[str, object]] = { # mutable-ok: request body dictionary
"model": model,
"prompt": prompt,
}
for key in [
for key in (
"n",
"response_format",
"size",
@ -113,12 +120,12 @@ class BytePlusImageGenerationConfig(BaseImageGenerationConfig):
"watermark",
"optimize_prompt_options",
"image",
]:
):
if key in optional_params:
body[key] = optional_params[key]
if "extra_body" in optional_params and isinstance(optional_params["extra_body"], dict):
extra_body: Final = {k: v for k, v in optional_params["extra_body"].items() if k not in ("model", "prompt")}
extra_body: Final = {k: v for k, v in optional_params["extra_body"].items() if k not in ("model", "prompt")} # mutable-ok: extra body dictionary
body.update(extra_body)
return body
@ -129,27 +136,28 @@ class BytePlusImageGenerationConfig(BaseImageGenerationConfig):
raw_response: httpx.Response,
model_response: ImageResponse,
logging_obj: "LiteLLMLoggingObj",
request_data: dict,
optional_params: dict,
litellm_params: dict,
request_data: dict, # mutable-ok: matches BaseImageGenerationConfig interface
optional_params: dict, # mutable-ok: matches BaseImageGenerationConfig interface
litellm_params: dict, # mutable-ok: matches BaseImageGenerationConfig interface
encoding: object = None,
api_key: str | None = None,
json_mode: bool | None = None,
) -> ImageResponse:
response = raw_response.json()
response: Final = raw_response.json()
if logging_obj:
logging_obj.post_call(
input=request_data.get("prompt", ""),
api_key=api_key,
additional_args={"complete_input_dict": request_data},
additional_args={"complete_input_dict": request_data}, # mutable-ok: logging payload dict
original_response=response,
)
image_response: ImageResponse = convert_to_model_response_object(
image_response: Final[ImageResponse] = convert_to_model_response_object(
response_object=response,
model_response_object=model_response,
response_type="image_generation",
)
return image_response

View file

@ -1,3 +1,3 @@
from .transformation import BytePlusResponsesAPIConfig
__all__ = ["BytePlusResponsesAPIConfig"]
__all__ = ("BytePlusResponsesAPIConfig",)

View file

@ -1,3 +1,5 @@
from __future__ import annotations
from typing import Final
import httpx
@ -20,21 +22,31 @@ class BytePlusResponsesAPIConfig(VolcEngineResponsesAPIConfig):
def custom_llm_provider(self) -> LlmProviders:
return LlmProviders.BYTEPLUS
def get_error_class(self, error_message: str, status_code: int, headers: dict | httpx.Headers) -> BytePlusError:
typed_headers: httpx.Headers = headers if isinstance(headers, httpx.Headers) else httpx.Headers(headers or {})
def get_error_class(
self, error_message: str, status_code: int, headers: dict | httpx.Headers # mutable-ok: matches BaseResponsesAPIConfig interface
) -> BytePlusError:
typed_headers: Final[httpx.Headers] = (
headers if isinstance(headers, httpx.Headers) else httpx.Headers(headers)
)
return BytePlusError(
status_code=status_code,
message=error_message,
headers=typed_headers,
)
def validate_environment(self, headers: dict, model: str, litellm_params: GenericLiteLLMParams | None) -> dict:
api_key_from_params: str | None = None
if litellm_params is not None:
if isinstance(litellm_params, dict):
api_key_from_params = litellm_params.get("api_key")
elif hasattr(litellm_params, "api_key"):
api_key_from_params = getattr(litellm_params, "api_key", None)
def validate_environment(
self,
headers: dict, # mutable-ok: matches BaseResponsesAPIConfig interface
model: str,
litellm_params: GenericLiteLLMParams | None,
) -> dict: # mutable-ok: matches BaseResponsesAPIConfig interface
api_key_from_params: Final[str | None] = (
litellm_params.get("api_key")
if isinstance(litellm_params, dict)
else getattr(litellm_params, "api_key", None)
if litellm_params is not None
else None
)
api_key: Final = (
api_key_from_params
@ -51,20 +63,19 @@ class BytePlusResponsesAPIConfig(VolcEngineResponsesAPIConfig):
def get_complete_url(
self,
api_base: str | None,
litellm_params: dict,
litellm_params: dict, # mutable-ok: matches BaseResponsesAPIConfig interface
) -> str:
base_url = (
base_url: Final = (
api_base
or litellm.api_base
or get_secret_str("BYTEPLUS_API_BASE")
or get_secret_str("ARK_API_BASE")
or get_byteplus_base_url()
)
base_url = base_url.rstrip("/")
).rstrip("/")
if base_url.endswith("/responses"):
return base_url
if base_url.endswith("/api/v3"):
return f"{base_url}/responses"
return f"{base_url}/api/v3/responses"

View file

@ -1,3 +1,3 @@
from .transformation import BytePlusTextToSpeechConfig
__all__ = ["BytePlusTextToSpeechConfig"]
__all__ = ("BytePlusTextToSpeechConfig",)

View file

@ -1,5 +1,9 @@
from __future__ import annotations
import base64
import json
from collections.abc import Mapping
from types import MappingProxyType
from typing import TYPE_CHECKING, ClassVar, Final
import httpx
@ -19,24 +23,26 @@ if TYPE_CHECKING:
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
_BYTEPLUS_TTS_AUTH_HEADERS: Final = frozenset(
{
(
"x-api-resource-id",
"x-api-key",
"x-api-app-id",
"x-api-access-key",
"x-api-app-key",
}
)
)
_FORMAT_CONTENT_TYPES: Final[dict[str, str]] = {
"mp3": "audio/mpeg",
"ogg": "audio/ogg",
"wav": "audio/wav",
"pcm": "audio/pcm",
"aac": "audio/aac",
"opus": "audio/opus",
"flac": "audio/flac",
}
_FORMAT_CONTENT_TYPES: Final = MappingProxyType(
{
"mp3": "audio/mpeg",
"ogg": "audio/ogg",
"wav": "audio/wav",
"pcm": "audio/pcm",
"aac": "audio/aac",
"opus": "audio/opus",
"flac": "audio/flac",
}
)
def _infer_audio_content_type(audio_bytes: bytes, requested_format: str) -> str:
@ -55,7 +61,7 @@ def _infer_audio_content_type(audio_bytes: bytes, requested_format: str) -> str:
def _extract_requested_format(
raw_response: httpx.Response,
logging_obj: "LiteLLMLoggingObj | None",
logging_obj: LiteLLMLoggingObj | None,
default_format: str,
) -> str:
req: Final[httpx.Request | None] = getattr(raw_response, "_request", None)
@ -92,71 +98,83 @@ def _extract_requested_format(
return default_format
class BytePlusTextToSpeechConfig(BaseTextToSpeechConfig):
DEFAULT_TTS_BASE_URL = "https://voice.ap-southeast-1.bytepluses.com/api/v3/tts/unidirectional"
DEFAULT_APP_KEY = "aGjiRDfUWi"
DEFAULT_SAMPLE_RATE = 24000
DEFAULT_FORMAT = "mp3"
def _extract_voice(
voice: str | dict | None, # mutable-ok: voice can be dict
kwargs: dict | None, # mutable-ok: kwargs is dict
) -> str | None:
if isinstance(voice, str) and voice.strip():
return voice.strip()
if isinstance(voice, dict):
for key in ("speaker", "voice", "id", "name"):
candidate: Final = voice.get(key)
if isinstance(candidate, str) and candidate.strip():
return candidate.strip()
if kwargs:
speaker_kwarg: Final = kwargs.pop("speaker", None)
if isinstance(speaker_kwarg, str) and speaker_kwarg.strip():
return speaker_kwarg.strip()
return None
class BytePlusTextToSpeechConfig(BaseTextToSpeechConfig):
DEFAULT_TTS_BASE_URL: Final = "https://voice.ap-southeast-1.bytepluses.com/api/v3/tts/unidirectional"
DEFAULT_APP_KEY: Final = "aGjiRDfUWi"
DEFAULT_SAMPLE_RATE: Final = 24000
DEFAULT_FORMAT: Final = "mp3"
def get_supported_openai_params(
self, model: str
) -> list: # mutable-ok: inherited provider interface returns a concrete parameter list
return [ # mutable-ok: inherited provider interface requires a concrete parameter list
"voice",
"response_format",
"speed",
]
def get_supported_openai_params(self, model: str) -> list:
return ["voice", "response_format", "speed"]
def map_openai_params(
self,
model: str,
optional_params: dict,
voice: str | dict | None = None,
optional_params: dict, # mutable-ok: inherited provider interface accepts a concrete parameter dictionary
voice: str | dict | None = None, # mutable-ok: inherited interface supports structured voice dictionaries
drop_params: bool = False,
kwargs: dict | None = None,
) -> tuple[str | None, dict]:
mapped_voice: str | None = None
if isinstance(voice, str) and voice.strip():
mapped_voice = voice.strip()
elif isinstance(voice, dict):
for key in ("speaker", "voice", "id", "name"):
candidate = voice.get(key)
if isinstance(candidate, str) and candidate.strip():
mapped_voice = candidate.strip()
break
if mapped_voice is None and kwargs:
speaker_kwarg = kwargs.pop("speaker", None)
if isinstance(speaker_kwarg, str) and speaker_kwarg.strip():
mapped_voice = speaker_kwarg.strip()
mapped_params = dict(optional_params) if optional_params else {}
kwargs: dict | None = None, # mutable-ok: inherited provider interface accepts a concrete keyword dictionary
) -> tuple[str | None, dict]: # mutable-ok: inherited provider interface returns concrete mapped parameters
mapped_voice: Final[str | None] = _extract_voice(voice=voice, kwargs=kwargs)
mapped_params: Final[dict] = dict(optional_params or {}) # mutable-ok: mapped parameters dictionary
return mapped_voice, mapped_params
VOICE_MAPPINGS: ClassVar[dict[str, str]] = {
"alloy": "en_female_stokie_uranus_bigtts",
"echo": "en_male_adam_mars_bigtts",
"fable": "en_female_skye_emo_v2_mars_bigtts",
"onyx": "id_male_han_uranus_bigtts",
"nova": "en_female_stokie_uranus_bigtts",
"shimmer": "en_female_skye_emo_v2_mars_bigtts",
}
VOICE_MAPPINGS: ClassVar[Mapping[str, str]] = MappingProxyType(
{
"alloy": "en_female_stokie_uranus_bigtts",
"echo": "en_male_adam_mars_bigtts",
"fable": "en_female_skye_emo_v2_mars_bigtts",
"onyx": "id_male_han_uranus_bigtts",
"nova": "en_female_stokie_uranus_bigtts",
"shimmer": "en_female_skye_emo_v2_mars_bigtts",
}
)
def validate_environment(
self,
headers: dict,
headers: dict, # mutable-ok: inherited provider interface accepts and updates concrete HTTP headers
model: str,
api_key: str | None = None,
api_base: str | None = None,
) -> dict:
) -> dict: # mutable-ok: inherited provider interface returns concrete HTTP headers
resolved_api_key: Final = (
api_key or litellm.api_key or get_secret_str("BYTEPLUS_API_KEY") or get_secret_str("ARK_API_KEY")
)
app_id = get_secret_str("BYTEPLUS_TTS_APP_ID")
access_key = get_secret_str("BYTEPLUS_TTS_ACCESS_KEY")
app_key = get_secret_str("BYTEPLUS_TTS_APP_KEY") or self.DEFAULT_APP_KEY
app_id: Final = get_secret_str("BYTEPLUS_TTS_APP_ID")
access_key: Final = get_secret_str("BYTEPLUS_TTS_ACCESS_KEY")
app_key: Final = get_secret_str("BYTEPLUS_TTS_APP_KEY") or self.DEFAULT_APP_KEY
resource_id = model
if resource_id.startswith("byteplus/"):
resource_id = resource_id.replace("byteplus/", "", 1)
resource_id: Final[str] = (
model.removeprefix("byteplus/") if model.startswith("byteplus/") else model
)
req_headers: dict[str, str] = {
req_headers: Final[dict[str, str]] = { # mutable-ok: building concrete request headers to update
"X-Api-Resource-Id": resource_id,
"Content-Type": "application/json",
"Connection": "keep-alive",
@ -176,25 +194,30 @@ class BytePlusTextToSpeechConfig(BaseTextToSpeechConfig):
headers.update(req_headers)
return headers
def get_error_class(self, error_message: str, status_code: int, headers: dict | Headers) -> BytePlusError:
typed_headers: httpx.Headers = headers if isinstance(headers, httpx.Headers) else httpx.Headers(headers or {})
def get_error_class(
self,
error_message: str,
status_code: int,
headers: dict | Headers, # mutable-ok: matches BaseTextToSpeechConfig interface
) -> BytePlusError:
typed_headers: Final[httpx.Headers] = (
headers if isinstance(headers, httpx.Headers) else httpx.Headers(headers or None)
)
return BytePlusError(status_code=status_code, message=error_message, headers=typed_headers)
def get_complete_url(
self,
model: str,
api_base: str | None,
litellm_params: dict,
litellm_params: dict, # mutable-ok: matches BaseTextToSpeechConfig interface
) -> str:
tts_base_env = get_secret_str("BYTEPLUS_TTS_API_BASE")
if tts_base_env:
base_url = tts_base_env
elif api_base and "voice" in api_base:
base_url = api_base
else:
base_url = self.DEFAULT_TTS_BASE_URL
base_url = base_url.rstrip("/")
tts_base_env: Final = get_secret_str("BYTEPLUS_TTS_API_BASE")
raw_base_url: Final = (
tts_base_env
if tts_base_env
else (api_base if api_base and "voice" in api_base else self.DEFAULT_TTS_BASE_URL)
)
base_url: Final = raw_base_url.rstrip("/")
if base_url.endswith("/api/v3/tts/unidirectional"):
return base_url
if base_url.endswith("/api/v3"):
@ -209,33 +232,38 @@ class BytePlusTextToSpeechConfig(BaseTextToSpeechConfig):
model: str,
input: str,
voice: str | None,
optional_params: dict,
litellm_params: dict,
headers: dict,
optional_params: dict, # mutable-ok: matches BaseTextToSpeechConfig interface
litellm_params: dict, # mutable-ok: matches BaseTextToSpeechConfig interface
headers: dict, # mutable-ok: matches BaseTextToSpeechConfig interface
) -> TextToSpeechRequestData:
params = dict(optional_params) if optional_params else {}
extra_body = params.pop("extra_body", {}) or {}
params: Final[dict] = dict(optional_params or {}) # mutable-ok: local copy of optional parameters
raw_extra_body: Final = params.pop("extra_body", None)
extra_body: Final[dict] = raw_extra_body if isinstance(raw_extra_body, dict) else {} # mutable-ok: extra body parameters
raw_speaker = voice or extra_body.get("speaker") or "en_female_stokie_uranus_bigtts"
speaker = self.VOICE_MAPPINGS.get(raw_speaker.lower(), raw_speaker)
raw_speaker: Final = voice or extra_body.get("speaker") or "en_female_stokie_uranus_bigtts"
speaker: Final = (
self.VOICE_MAPPINGS.get(raw_speaker.lower(), raw_speaker)
if isinstance(raw_speaker, str)
else "en_female_stokie_uranus_bigtts"
)
audio_format = params.get("response_format") or extra_body.get("format") or self.DEFAULT_FORMAT
sample_rate = extra_body.get("sample_rate") or self.DEFAULT_SAMPLE_RATE
speed = params.get("speed")
audio_format: Final = params.get("response_format") or extra_body.get("format") or self.DEFAULT_FORMAT
sample_rate: Final = extra_body.get("sample_rate") or self.DEFAULT_SAMPLE_RATE
speed: Final = params.get("speed")
additions_dict: dict = {
additions_dict: Final[dict[str, object]] = { # mutable-ok: building request additions payload
"disable_markdown_filter": True,
"enable_language_detector": True,
"enable_latex_tn": True,
"disable_default_bit_rate": True,
"max_length_to_filter_parenthesis": 0,
"cache_config": {"text_type": 1, "use_cache": True},
"cache_config": {"text_type": 1, "use_cache": True}, # mutable-ok: cache config dictionary
}
user_additions = extra_body.get("additions")
user_additions: Final = extra_body.get("additions")
if isinstance(user_additions, str):
try:
parsed_user_additions = json.loads(user_additions)
parsed_user_additions: Final = json.loads(user_additions)
if isinstance(parsed_user_additions, dict):
additions_dict.update(parsed_user_additions)
except (json.JSONDecodeError, ValueError): # noqa: BLE001 # ignore invalid user additions
@ -243,18 +271,18 @@ class BytePlusTextToSpeechConfig(BaseTextToSpeechConfig):
elif isinstance(user_additions, dict):
additions_dict.update(user_additions)
user_id = extra_body.get("uid") or litellm_params.get("user") or "litellm-user"
user_id: Final = extra_body.get("uid") or litellm_params.get("user") or "litellm-user"
audio_params: dict = {
audio_params: Final[dict[str, object]] = { # mutable-ok: building audio params payload
"format": audio_format,
"sample_rate": sample_rate,
}
if speed is not None:
audio_params["speed_ratio"] = float(speed)
payload = {
"user": {"uid": str(user_id)},
"req_params": {
payload: Final[dict[str, object]] = { # mutable-ok: building request payload
"user": {"uid": str(user_id)}, # mutable-ok: user subdictionary
"req_params": { # mutable-ok: req_params subdictionary
"text": input,
"speaker": speaker,
"audio_params": audio_params,
@ -264,14 +292,14 @@ class BytePlusTextToSpeechConfig(BaseTextToSpeechConfig):
return TextToSpeechRequestData(
dict_body=payload,
headers={"Content-Type": "application/json"},
headers={"Content-Type": "application/json"}, # mutable-ok: request headers dictionary
)
def transform_text_to_speech_response(
self,
model: str,
raw_response: httpx.Response,
logging_obj: "LiteLLMLoggingObj",
logging_obj: LiteLLMLoggingObj,
) -> HttpxBinaryResponseContent:
if raw_response.status_code != 200:
raise BytePlusError(
@ -280,30 +308,33 @@ class BytePlusTextToSpeechConfig(BaseTextToSpeechConfig):
headers=raw_response.headers,
)
audio_bytes = bytearray()
audio_bytes: Final = bytearray()
lines = raw_response.text.strip().split("\n")
lines: Final = tuple(raw_response.text.strip().split("\n"))
for line in lines:
line = line.strip()
if not line:
line_str: Final = line.strip()
if not line_str:
continue
try:
data = json.loads(line)
data: Final = json.loads(line_str)
except (json.JSONDecodeError, ValueError): # noqa: BLE001 # skip invalid line
continue
code = data.get("code", 0)
if not isinstance(data, dict):
continue
code: Final = data.get("code", 0)
if code == 0 and data.get("data"):
try:
chunk_audio = base64.b64decode(data["data"])
chunk_audio: Final = base64.b64decode(data["data"])
audio_bytes.extend(chunk_audio)
except (TypeError, ValueError): # noqa: BLE001 # skip invalid audio chunk
pass
elif code == 20000000:
break
elif code > 0:
msg = data.get("message") or f"BytePlus TTS error code {code}"
msg: Final = data.get("message") or f"BytePlus TTS error code {code}"
raise BytePlusError(
status_code=400,
message=f"BytePlus TTS API Error: {msg}",
@ -323,7 +354,7 @@ class BytePlusTextToSpeechConfig(BaseTextToSpeechConfig):
mock_http_response: Final = httpx.Response(
status_code=200,
content=audio_bytes_final,
headers={"content-type": content_type},
headers={"content-type": content_type}, # mutable-ok: mock response headers dictionary
)
return HttpxBinaryResponseContent(mock_http_response)
@ -331,30 +362,31 @@ class BytePlusTextToSpeechConfig(BaseTextToSpeechConfig):
self,
model: str,
input: str,
voice: str | dict | None,
optional_params: dict,
litellm_params_dict: dict,
logging_obj: "LiteLLMLoggingObj",
voice: str | dict | None, # mutable-ok: matches BaseTextToSpeechConfig interface
optional_params: dict, # mutable-ok: matches BaseTextToSpeechConfig interface
litellm_params_dict: dict, # mutable-ok: matches BaseTextToSpeechConfig interface
logging_obj: LiteLLMLoggingObj,
timeout: float | httpx.Timeout,
extra_headers: dict | None,
extra_headers: dict | None, # mutable-ok: matches BaseTextToSpeechConfig interface
base_llm_http_handler: object,
aspeech: bool,
api_base: str | None,
api_key: str | None,
**kwargs: object,
**kwargs: object, # kwargs-ok: matches BaseTextToSpeechConfig interface
) -> object:
safe_extra_headers: Final[dict | None] = (
{k: v for k, v in extra_headers.items() if k.lower() not in _BYTEPLUS_TTS_AUTH_HEADERS}
safe_extra_headers: Final[dict | None] = ( # mutable-ok: filtered request headers dictionary
{k: v for k, v in extra_headers.items() if k.lower() not in _BYTEPLUS_TTS_AUTH_HEADERS} # mutable-ok: filtered headers dict comprehension
if extra_headers
else None
)
litellm_params_dict.update(
{
{ # mutable-ok: litellm params dictionary
"api_key": api_key,
"api_base": api_base,
}
)
return base_llm_http_handler.text_to_speech_handler(
handler_func: Final = getattr(base_llm_http_handler, "text_to_speech_handler")
return handler_func(
model=model,
input=input,
voice=voice,

View file

@ -7016,7 +7016,7 @@ def embedding(
if extra_headers is not None and isinstance(extra_headers, dict):
headers = extra_headers # rebind-ok: same pattern as volcengine block above
else:
headers = {} # rebind-ok: same pattern as volcengine block above
headers = {} # mutable-ok: default empty dict # rebind-ok: conditional headers assignment
response = base_llm_http_handler.embedding( # rebind-ok: same pattern as volcengine block above
model=model,
input=input,
@ -7025,7 +7025,7 @@ def embedding(
logging_obj=logging,
api_base=api_base,
optional_params=optional_params,
litellm_params={},
litellm_params={}, # mutable-ok: empty dict parameter for handler
model_response=EmbeddingResponse(),
api_key=byteplus_key,
client=client,