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
This commit is contained in:
Ritika shrestha 2026-06-28 14:27:03 -07:00
parent b443037783
commit ebf84c4237
2 changed files with 30 additions and 5 deletions

View file

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

View file

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