diff --git a/litellm/llms/cohere/rerank_v2/transformation.py b/litellm/llms/cohere/rerank_v2/transformation.py index 7c68a431a90..a4e73d4d838 100644 --- a/litellm/llms/cohere/rerank_v2/transformation.py +++ b/litellm/llms/cohere/rerank_v2/transformation.py @@ -19,11 +19,12 @@ class CohereRerankV2Config(CohereRerankConfig): optional_params: dict | None = None, ) -> str: if api_base: - # Remove trailing slashes and ensure clean base URL - api_base = api_base.rstrip("/") - if not api_base.endswith("/v2/rerank"): - api_base = f"{api_base}/v2/rerank" - return api_base + base = api_base.rstrip("/") + if base.endswith("/v2/rerank"): + return base + if base.endswith("/v2"): + return f"{base}/rerank" + return f"{base}/v2/rerank" return "https://api.cohere.ai/v2/rerank" def get_supported_cohere_rerank_params(self, model: str) -> list: diff --git a/tests/test_litellm/llms/cohere/rerank_v2/test_transformation.py b/tests/test_litellm/llms/cohere/rerank_v2/test_transformation.py new file mode 100644 index 00000000000..ee5cafa1f1d --- /dev/null +++ b/tests/test_litellm/llms/cohere/rerank_v2/test_transformation.py @@ -0,0 +1,24 @@ +import pytest + +from litellm.llms.cohere.rerank_v2.transformation import CohereRerankV2Config + + +@pytest.mark.parametrize( + "api_base, expected", + [ + # versioned root must not duplicate the version segment (#31167) + ("https://api.cohere.ai/v2", "https://api.cohere.ai/v2/rerank"), + ("https://api.cohere.ai/v2/", "https://api.cohere.ai/v2/rerank"), + # bare host gets the full v2 rerank path + ("https://api.cohere.ai", "https://api.cohere.ai/v2/rerank"), + # already-complete url is left untouched + ("https://api.cohere.ai/v2/rerank", "https://api.cohere.ai/v2/rerank"), + # self-hosted gateway exposing a versioned root + ("https://gateway.internal/cohere/v2", "https://gateway.internal/cohere/v2/rerank"), + # no api_base falls back to the public endpoint + (None, "https://api.cohere.ai/v2/rerank"), + ], +) +def test_get_complete_url_does_not_duplicate_version(api_base, expected): + config = CohereRerankV2Config() + assert config.get_complete_url(api_base=api_base, model="rerank-v3.5") == expected