From 63e17bd4eb75ac0b020b707b17ebb839f994e35e Mon Sep 17 00:00:00 2001 From: agungbesti Date: Tue, 15 Sep 2026 04:47:56 +0700 Subject: [PATCH] fix(byteplus): fix basedpyright budget violations and unused variables --- litellm/__init__.py | 3 + litellm/llms/byteplus/chat/transformation.py | 4 - litellm/llms/byteplus/common_utils.py | 10 +-- .../llms/byteplus/embedding/transformation.py | 34 ++++--- .../byteplus/text_to_speech/transformation.py | 12 +-- litellm/main.py | 89 ++++++++++--------- 6 files changed, 79 insertions(+), 73 deletions(-) diff --git a/litellm/__init__.py b/litellm/__init__.py index 26780965377..6833cecb831 100644 --- a/litellm/__init__.py +++ b/litellm/__init__.py @@ -1825,6 +1825,9 @@ if TYPE_CHECKING: from .llms.volcengine.responses.transformation import ( VolcEngineResponsesAPIConfig as VolcEngineResponsesAPIConfig, ) + from .llms.byteplus.responses.transformation import ( + BytePlusResponsesAPIConfig as BytePlusResponsesAPIConfig, + ) from .llms.manus.responses.transformation import ( ManusResponsesAPIConfig as ManusResponsesAPIConfig, ) diff --git a/litellm/llms/byteplus/chat/transformation.py b/litellm/llms/byteplus/chat/transformation.py index 32a2375cf7f..74486a1c2c2 100644 --- a/litellm/llms/byteplus/chat/transformation.py +++ b/litellm/llms/byteplus/chat/transformation.py @@ -8,10 +8,6 @@ class BytePlusChatConfig(OpenAILikeChatConfig): Reference: https://docs.byteplus.com/en/docs/ModelArk """ - @classmethod - def get_config(cls) -> "BytePlusChatConfig": - return super().get_config() - def get_supported_openai_params(self, model: str) -> list: # mutable-ok: matches BaseConfig interface return [ # mutable-ok: matches BaseConfig interface "frequency_penalty", diff --git a/litellm/llms/byteplus/common_utils.py b/litellm/llms/byteplus/common_utils.py index 02174cf967a..41251ce296d 100644 --- a/litellm/llms/byteplus/common_utils.py +++ b/litellm/llms/byteplus/common_utils.py @@ -5,14 +5,14 @@ from typing import Final import httpx -from litellm.llms.base_llm.chat.transformation import BaseLLMException +from litellm.llms.volcengine.common_utils import VolcEngineError -class BytePlusError(BaseLLMException): +class BytePlusError(VolcEngineError): def __init__(self, status_code: int, message: str, headers: httpx.Headers | None = None) -> None: - self.status_code: Final = status_code - self.message: Final = message - self.headers: Final = headers or httpx.Headers() + self.status_code = status_code + self.message = message + self.headers = headers or httpx.Headers() super().__init__(status_code=status_code, message=message, headers=self.headers) diff --git a/litellm/llms/byteplus/embedding/transformation.py b/litellm/llms/byteplus/embedding/transformation.py index 84ab7d02850..5d6a3d45b5d 100644 --- a/litellm/llms/byteplus/embedding/transformation.py +++ b/litellm/llms/byteplus/embedding/transformation.py @@ -21,16 +21,27 @@ if TYPE_CHECKING: from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj +def _extract_embedding_data(data_raw: object) -> list[object]: # mutable-ok: matches list output + if isinstance(data_raw, dict): + embedding_item: Final[dict[str, object]] = { # mutable-ok: building embedding response dict + "object": data_raw.get("object", "embedding"), + "embedding": data_raw.get("embedding", ()), + "index": 0, + } + if "sparse_embedding" in data_raw: + embedding_item["sparse_embedding"] = data_raw["sparse_embedding"] + return [embedding_item] # mutable-ok: single item data list + if isinstance(data_raw, list): + return data_raw # mutable-ok: raw list + return [] # mutable-ok: default empty list + + class BytePlusEmbeddingConfig(BaseEmbeddingConfig): """ Configuration class for BytePlus embedding models (Text and Multimodal Vision embeddings). Reference: https://docs.byteplus.com/en/docs/ModelArk """ - @classmethod - def get_config(cls) -> BytePlusEmbeddingConfig: - return super().get_config() - def get_supported_openai_params(self, model: str) -> list[str]: # mutable-ok: matches BaseEmbeddingConfig interface return [ # mutable-ok: matches BaseEmbeddingConfig interface "encoding_format", @@ -139,20 +150,7 @@ class BytePlusEmbeddingConfig(BaseEmbeddingConfig): raise ValueError(f"Failed to parse BytePlus response as JSON: {e}") data_raw: Final = response_json.get("data", ()) - - if isinstance(data_raw, dict): - embedding_item: Final[dict[str, object]] = { # mutable-ok: building embedding response dict - "object": data_raw.get("object", "embedding"), - "embedding": data_raw.get("embedding", ()), - "index": 0, - } - if "sparse_embedding" in data_raw: - embedding_item["sparse_embedding"] = data_raw["sparse_embedding"] - data_list: Final = [embedding_item] # mutable-ok: single item data list - elif isinstance(data_raw, list): - data_list = data_raw - else: - data_list = [] # mutable-ok: default empty data list + data_list: Final = _extract_embedding_data(data_raw) transformed_response: Final[dict[str, object]] = { # mutable-ok: building response dict "object": "list", diff --git a/litellm/llms/byteplus/text_to_speech/transformation.py b/litellm/llms/byteplus/text_to_speech/transformation.py index 874187886eb..dc35e8c3551 100644 --- a/litellm/llms/byteplus/text_to_speech/transformation.py +++ b/litellm/llms/byteplus/text_to_speech/transformation.py @@ -106,7 +106,7 @@ def _extract_voice( return voice.strip() if isinstance(voice, dict): for key in ("speaker", "voice", "id", "name"): - candidate: Final = voice.get(key) + candidate = voice.get(key) if isinstance(candidate, str) and candidate.strip(): return candidate.strip() if kwargs: @@ -311,29 +311,29 @@ class BytePlusTextToSpeechConfig(BaseTextToSpeechConfig): lines: Final = tuple(raw_response.text.strip().split("\n")) for line in lines: - line_str: Final = line.strip() + line_str = line.strip() if not line_str: continue try: - data: Final = json.loads(line_str) + data = json.loads(line_str) except (json.JSONDecodeError, ValueError): # noqa: BLE001 # skip invalid line continue if not isinstance(data, dict): continue - code: Final = data.get("code", 0) + code = data.get("code", 0) if code == 0 and data.get("data"): try: - chunk_audio: Final = base64.b64decode(data["data"]) + chunk_audio = 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: Final = data.get("message") or f"BytePlus TTS error code {code}" + msg = data.get("message") or f"BytePlus TTS error code {code}" raise BytePlusError( status_code=400, message=f"BytePlus TTS API Error: {msg}", diff --git a/litellm/main.py b/litellm/main.py index d2f2a69a1d0..afdf58715d0 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -6978,13 +6978,20 @@ def embedding( client=client, aembedding=aembedding, ) - elif custom_llm_provider == "volcengine": + elif custom_llm_provider in ("volcengine", "byteplus"): volcengine_key: Final = ( - api_key or litellm.api_key or get_secret_str("ARK_API_KEY") or get_secret_str("VOLCENGINE_API_KEY") + api_key + or litellm.api_key + or ( + get_secret_str("BYTEPLUS_API_KEY") + if custom_llm_provider == "byteplus" + else get_secret_str("VOLCENGINE_API_KEY") + ) + or get_secret_str("ARK_API_KEY") ) if volcengine_key is None: raise ValueError( - "Missing API key for Volcengine. Set ARK_API_KEY or VOLCENGINE_API_KEY environment variable or pass api_key parameter." + f"Missing API key for {custom_llm_provider.capitalize()}. Set ARK_API_KEY or {custom_llm_provider.upper()}_API_KEY environment variable or pass api_key parameter." ) if extra_headers is not None and isinstance(extra_headers, dict): headers = extra_headers @@ -7005,33 +7012,6 @@ def embedding( aembedding=aembedding, headers=headers, ) - elif custom_llm_provider == "byteplus": - byteplus_key: Final = ( - api_key or litellm.api_key or get_secret_str("BYTEPLUS_API_KEY") or get_secret_str("ARK_API_KEY") - ) - if byteplus_key is None: - raise ValueError( - "Missing API key for BytePlus. Set BYTEPLUS_API_KEY or ARK_API_KEY environment variable or pass api_key parameter." - ) - if extra_headers is not None and isinstance(extra_headers, dict): - headers = extra_headers # rebind-ok: same pattern as volcengine block above - else: - 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, - timeout=timeout, - custom_llm_provider=custom_llm_provider, - logging_obj=logging, - api_base=api_base, - optional_params=optional_params, - litellm_params={}, # mutable-ok: empty dict parameter for handler - model_response=EmbeddingResponse(), - api_key=byteplus_key, - client=client, - aembedding=aembedding, - headers=headers, - ) elif custom_llm_provider in ("dashscope", "qwencloud", "qwen_ai_platform"): from litellm.llms.dashscope.common_utils import ( missing_dashscope_family_key_message, @@ -8494,20 +8474,19 @@ def speech( client=client, _is_async=aspeech or False, ) - elif custom_llm_provider == "aws_polly" or custom_llm_provider == "byteplus": - from litellm.llms.base_llm.text_to_speech.transformation import ( - BaseTextToSpeechConfig, + elif custom_llm_provider == "aws_polly": + from litellm.llms.aws_polly.text_to_speech.transformation import ( + AWSPollyTextToSpeechConfig, ) + # AWS Polly Text-to-Speech if text_to_speech_provider_config is None: - raise litellm.BadRequestError( - message=f"{custom_llm_provider} Text-to-Speech configuration not found", - model=model, - llm_provider=custom_llm_provider, - ) + text_to_speech_provider_config = AWSPollyTextToSpeechConfig() - dispatch_config: Final = cast(BaseTextToSpeechConfig, text_to_speech_provider_config) - response = dispatch_config.dispatch_text_to_speech( # rebind-ok: same pattern as other TTS provider blocks + # Cast to specific AWS Polly config type to access dispatch method + aws_polly_config: Final = cast(AWSPollyTextToSpeechConfig, text_to_speech_provider_config) + + response = aws_polly_config.dispatch_text_to_speech( model=model, input=input, voice=voice, @@ -8522,6 +8501,36 @@ def speech( api_key=api_key, **kwargs, ) + elif custom_llm_provider == "byteplus": + from litellm.llms.byteplus.text_to_speech.transformation import ( + BytePlusTextToSpeechConfig, + ) + + if text_to_speech_provider_config is None: + text_to_speech_provider_config = BytePlusTextToSpeechConfig() + + if isinstance(text_to_speech_provider_config, BytePlusTextToSpeechConfig): + response = text_to_speech_provider_config.dispatch_text_to_speech( # rebind-ok: same pattern as other TTS provider blocks + model=model, + input=input, + voice=voice, + optional_params=optional_params, + litellm_params_dict=litellm_params_dict, + logging_obj=logging_obj, + timeout=timeout, + extra_headers=extra_headers, + base_llm_http_handler=base_llm_http_handler, + aspeech=aspeech or False, + api_base=api_base, + api_key=api_key, + **kwargs, + ) + else: + raise litellm.BadRequestError( + message="BytePlus Text-to-Speech configuration not found", + model=model, + llm_provider=custom_llm_provider, + ) if response is None: raise Exception(