From ebf84c423719dadf6253a0e79ca64c8979e6bd30 Mon Sep 17 00:00:00 2001 From: Ritika shrestha <87307821+ritsth@users.noreply.github.com> Date: Sun, 28 Jun 2026 14:27:03 -0700 Subject: [PATCH] fix(cohere): avoid duplicate /v2 in rerank v2 url for versioned api_base When api_base already ends in /v2 (the documented Cohere v2 root), the rerank v2 url builder appended /v2/rerank again, producing /v2/v2/rerank. Normalize so a versioned root resolves to /v2/rerank and a bare host still gets /v2/rerank. Fixes #31167 --- .../llms/cohere/rerank_v2/transformation.py | 11 +++++---- .../cohere/rerank_v2/test_transformation.py | 24 +++++++++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 tests/test_litellm/llms/cohere/rerank_v2/test_transformation.py 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