fix(byteplus): fix basedpyright budget violations and unused variables

This commit is contained in:
agungbesti 2026-09-15 04:47:56 +07:00
parent 5e266bf57f
commit 63e17bd4eb
6 changed files with 79 additions and 73 deletions

View file

@ -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,
)

View file

@ -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",

View file

@ -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)

View file

@ -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",

View file

@ -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}",

View file

@ -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(