The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
chore: merge litellm_internal_staging into litellm_fix_dashscope_rerank_endpoint
mateo-berrieb700c016754
diff --cc tests/test_litellm/rerank_api/test_main.pyindex 1051198c904,2b6cfeda2c2..0992cd9bb37--- a/tests/test_litellm/rerank_api/test_main.py+++ b/tests/test_litellm/rerank_api/test_main.py@@@ -111,72 -111,99 +111,165 @@@ def test_together_rerank_honors_api_basassert mock_route.calls[0].request.headers["authorization"] == "Bearer fake-together-key"+DASHSCOPE_RERANK_BODY = {+ "object": "list",+ "results": [{"index": 0, "relevance_score": 0.95}],+ "model": "qwen3-rerank",+ "id": "rerank-mock-id",+ "usage": {"total_tokens": 10},+}+++def test_dashscope_rerank_defaults_to_live_rerank_route(respx_mock: respx.MockRouter, monkeypatch):+ """Regression for the dead default endpoint: get_llm_provider always returns the+ chat base for dashscope, which used to hijack rerank onto the dead+ /compatible-mode/v1/reranks route."""+ monkeypatch.delenv("DASHSCOPE_API_BASE", raising=False)+ monkeypatch.delenv("DASHSCOPE_API_BASE_RERANK", raising=False)++ mock_route = respx_mock.post("https://dashscope.aliyuncs.com/compatible-api/v1/reranks")+ mock_route.return_value = httpx.Response(200, json=DASHSCOPE_RERANK_BODY)++ response = litellm.rerank(+ model="dashscope/qwen3-rerank",+ query=MARKER_QUERY,+ documents=[MARKER_DOC],+ api_key="fake-dashscope-key",+ )++ assert mock_route.called+ assert response.results[0]["relevance_score"] == 0.95+++def test_dashscope_rerank_chat_env_base_keeps_host_and_rerank_route(respx_mock: respx.MockRouter, monkeypatch):+ """Regression: a chat-style DASHSCOPE_API_BASE must not hijack rerank onto the+ chat path, while its host (the region) is preserved."""+ monkeypatch.setenv("DASHSCOPE_API_BASE", "https://dashscope-intl.aliyuncs.com/compatible-mode/v1")+ monkeypatch.delenv("DASHSCOPE_API_BASE_RERANK", raising=False)++ mock_route = respx_mock.post("https://dashscope-intl.aliyuncs.com/compatible-api/v1/reranks")+ mock_route.return_value = httpx.Response(200, json=DASHSCOPE_RERANK_BODY)++ litellm.rerank(+ model="dashscope/qwen3-rerank",+ query=MARKER_QUERY,+ documents=[MARKER_DOC],+ api_key="fake-dashscope-key",+ )++ assert mock_route.called+++def test_dashscope_rerank_explicit_api_base_wins(respx_mock: respx.MockRouter, monkeypatch):+ monkeypatch.setenv("DASHSCOPE_API_BASE", "https://dashscope-intl.aliyuncs.com/compatible-mode/v1")++ mock_route = respx_mock.post("https://custom-rerank.example/v1/reranks")+ mock_route.return_value = httpx.Response(200, json=DASHSCOPE_RERANK_BODY)++ litellm.rerank(+ model="dashscope/qwen3-rerank",+ query=MARKER_QUERY,+ documents=[MARKER_DOC],+ api_key="fake-dashscope-key",+ api_base="https://custom-rerank.example/v1",+ )++ assert mock_route.called+++ DASHSCOPE_404_BODY = {+ "error": {+ "message": "The model `does-not-exist` does not exist or you do not have access to it.",+ "type": "invalid_request_error",+ "param": None,+ "code": "model_not_found",+ },+ "request_id": "mock-request-id",+ }+++ def test_rerank_error_names_provider_and_keeps_body(respx_mock: respx.MockRouter, monkeypatch):+ """Regression for the rerank error path mapping with the unresolved provider param:+ a provider 404 surfaced as 'None - ' instead of naming the provider and its error body."""+ monkeypatch.delenv("DASHSCOPE_API_BASE", raising=False)+ monkeypatch.delenv("DASHSCOPE_API_BASE_RERANK", raising=False)++ mock_route = respx_mock.post("https://dashscope.example/v1/reranks")+ mock_route.return_value = httpx.Response(404, json=DASHSCOPE_404_BODY)++ with pytest.raises(litellm.NotFoundError) as exc_info:+ litellm.rerank(+ model="dashscope/does-not-exist",+ query=MARKER_QUERY,+ documents=[MARKER_DOC],+ api_key="fake-dashscope-key",+ api_base="https://dashscope.example/v1",+ )++ assert mock_route.called+ assert "DashscopeException" in str(exc_info.value)+ assert "does not exist or you do not have access to it" in str(exc_info.value)+ assert "None - " not in str(exc_info.value)+++ @pytest.mark.asyncio+ async def test_arerank_error_is_mapped_to_litellm_exception(respx_mock: respx.MockRouter, monkeypatch):+ """Regression for arerank's bare re-raise: provider errors escaped as raw+ provider exception classes instead of the mapped litellm exception contract."""+ monkeypatch.delenv("DASHSCOPE_API_BASE", raising=False)+ monkeypatch.delenv("DASHSCOPE_API_BASE_RERANK", raising=False)+ monkeypatch.setenv("DISABLE_AIOHTTP_TRANSPORT", "True")++ mock_route = respx_mock.post("https://dashscope.example/v1/reranks")+ mock_route.return_value = httpx.Response(404, json=DASHSCOPE_404_BODY)++ with pytest.raises(litellm.NotFoundError) as exc_info:+ await litellm.arerank(+ model="dashscope/does-not-exist",+ query=MARKER_QUERY,+ documents=[MARKER_DOC],+ api_key="fake-dashscope-key",+ api_base="https://dashscope.example/v1",+ )++ assert mock_route.called+ assert "DashscopeException" in str(exc_info.value)+ assert "does not exist or you do not have access to it" in str(exc_info.value)+ assert "None - " not in str(exc_info.value)+++ @pytest.mark.asyncio+ async def test_arerank_declared_authenticating_provider_skips_resolution(monkeypatch):+ """Regression for the event-loop hazard in arerank's provider pre-resolution:+ get_llm_provider runs the blocking OAuth device flow for github_copilot/chatgpt,+ so arerank must adopt the declared provider instead of resolving it, while the+ except path still maps with that declared provider."""+ from litellm.llms.base_llm.chat.transformation import BaseLLMException++ resolution_calls = []++ def record_resolution(*args, **kwargs):+ resolution_calls.append((args, kwargs))+ return "gpt-4o", "github_copilot", None, None++ def rerank_raises_provider_error(*args, **kwargs):+ raise BaseLLMException(status_code=401, message='{"error":"bad key"}')++ monkeypatch.setattr(litellm, "get_llm_provider", record_resolution)+ monkeypatch.setattr("litellm.rerank_api.main.rerank", rerank_raises_provider_error)++ with pytest.raises(litellm.AuthenticationError) as exc_info:+ await litellm.arerank(+ model="github_copilot/gpt-4o",+ query=MARKER_QUERY,+ documents=[MARKER_DOC],+ )++ assert resolution_calls == []+ assert "Github_copilotException" in str(exc_info.value)+ assert "None - " not in str(exc_info.value)++@pytest.mark.asyncioasync def test_together_rerank_async_honors_env_api_base(respx_mock: respx.MockRouter, monkeypatch):"""Regression: TOGETHER_AI_API_BASE was honored by chat but ignored by rerank."""