From 5813929e437ef4a2f4340812dc438beef149bd09 Mon Sep 17 00:00:00 2001 From: parijat1222q Date: Thu, 22 Oct 2026 04:21:07 +0530 Subject: [PATCH] fix: satisfy strict ruff gate (BLE001, C901) - catch pydantic ValidationError instead of blind Exception in OpenRouter rerank error-message extraction - keep get_provider_rerank_config at base complexity by collapsing the cohere branch into a tuple check with a conditional expression --- litellm/llms/openrouter/rerank/transformation.py | 4 ++-- litellm/utils.py | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/litellm/llms/openrouter/rerank/transformation.py b/litellm/llms/openrouter/rerank/transformation.py index 7f8c5f0ad9b..669d0ec33e0 100644 --- a/litellm/llms/openrouter/rerank/transformation.py +++ b/litellm/llms/openrouter/rerank/transformation.py @@ -13,7 +13,7 @@ from collections.abc import Mapping from typing import Final import httpx -from pydantic import ConfigDict, TypeAdapter +from pydantic import ConfigDict, TypeAdapter, ValidationError from litellm._uuid import uuid from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj @@ -201,7 +201,7 @@ class OpenRouterRerankConfig(BaseRerankConfig): # OpenRouter errors are OpenAI-shaped: {"error": {"message": "...", "code": ...}} try: parsed: Final = _JSON_OBJECT.validate_json(error_message) - except Exception: + except ValidationError: return error_message error_obj: Final = parsed.get("error") if isinstance(error_obj, Mapping): diff --git a/litellm/utils.py b/litellm/utils.py index 0a483bd38b3..e21ccf6deda 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -9567,11 +9567,12 @@ class ProviderConfigManager: api_base: str | None, present_version_params: list[str], ) -> BaseRerankConfig: - if litellm.LlmProviders.COHERE == provider or litellm.LlmProviders.COHERE_CHAT == provider: - if should_use_cohere_v1_client(api_base, present_version_params): - return litellm.CohereRerankConfig() - else: - return litellm.CohereRerankV2Config() + if provider in (litellm.LlmProviders.COHERE, litellm.LlmProviders.COHERE_CHAT): + return ( + litellm.CohereRerankConfig() + if should_use_cohere_v1_client(api_base, present_version_params) + else litellm.CohereRerankV2Config() + ) elif litellm.LlmProviders.AZURE_AI == provider: return litellm.AzureAIRerankConfig() elif litellm.LlmProviders.INFINITY == provider: