litellm/tests/unit/llms/dashscope/test_dashscope_common_utils.py
Rick d260909f04 feat(dashscope): derive protocol bases from one domain
Accept the DashScope domain root, the OpenAI-compatible base, the Anthropic
Messages base, or a native /api/v1 base and derive every surface from it
Adds a native Anthropic Messages config for /apps/anthropic so a single
deployment serves chat, messages, embeddings, rerank, image generation, and
video against the same credential

Co-Authored-By: AaronHowell <111272993+AaronHowell@users.noreply.github.com>
2026-10-21 16:36:45 +08:00

136 lines
4.6 KiB
Python

"""
Unit tests for DashScope's protocol-specific base URL derivation.
"""
import pytest
from litellm.llms.dashscope.common_utils import (
DASHSCOPE_DEFAULT_ORIGIN,
resolve_dashscope_anthropic_api_base,
resolve_dashscope_endpoints,
resolve_dashscope_family_rerank_api_base,
resolve_dashscope_openai_api_base,
)
WORKSPACE = "https://llm-abc123.cn-beijing.maas.aliyuncs.com"
@pytest.mark.parametrize(
"api_base",
[
WORKSPACE,
f"{WORKSPACE}/",
f"{WORKSPACE}/compatible-mode/v1",
f"{WORKSPACE}/compatible-mode/v1/chat/completions",
f"{WORKSPACE}/apps/anthropic",
f"{WORKSPACE}/apps/anthropic/v1/messages",
f"{WORKSPACE}/api/v1",
f"{WORKSPACE}/api/v1/services/aigc/video-generation/video-synthesis",
],
)
def test_workspace_base_derives_every_protocol(api_base: str):
endpoints = resolve_dashscope_endpoints(api_base)
assert endpoints is not None
assert endpoints.origin == WORKSPACE
assert endpoints.openai == f"{WORKSPACE}/compatible-mode/v1"
assert endpoints.anthropic == f"{WORKSPACE}/apps/anthropic"
assert endpoints.native == f"{WORKSPACE}/api/v1"
@pytest.mark.parametrize(
"origin",
[
"https://dashscope.aliyuncs.com",
"https://dashscope-intl.aliyuncs.com",
"https://dashscope-us.aliyuncs.com",
],
)
@pytest.mark.parametrize("path", ["", "/", "/compatible-mode/v1"])
def test_standard_hosts_derive_every_protocol(origin: str, path: str):
endpoints = resolve_dashscope_endpoints(f"{origin}{path}")
assert endpoints is not None
assert endpoints.openai == f"{origin}/compatible-mode/v1"
assert endpoints.anthropic == f"{origin}/apps/anthropic"
assert endpoints.native == f"{origin}/api/v1"
@pytest.mark.parametrize(
"api_base",
[
None,
"",
"not-a-url",
"https://gateway.example.com",
"https://gateway.example.com/compatible-mode/v1",
f"{WORKSPACE}/custom-path",
f"{WORKSPACE}/compatible-mode/v1?api-version=1",
"https://trial.cn-beijing.maas.aliyuncs.com",
"https://token-plan.cn-beijing.maas.aliyuncs.com",
],
)
def test_unrecognized_bases_are_not_rewritten(api_base: str | None):
assert resolve_dashscope_endpoints(api_base) is None
def test_openai_base_derives_from_the_domain(monkeypatch: pytest.MonkeyPatch):
monkeypatch.delenv("DASHSCOPE_API_BASE", raising=False)
assert resolve_dashscope_openai_api_base(WORKSPACE) == f"{WORKSPACE}/compatible-mode/v1"
assert resolve_dashscope_openai_api_base(f"{WORKSPACE}/apps/anthropic") == f"{WORKSPACE}/compatible-mode/v1"
def test_openai_base_keeps_unrecognized_hosts(monkeypatch: pytest.MonkeyPatch):
monkeypatch.delenv("DASHSCOPE_API_BASE", raising=False)
assert resolve_dashscope_openai_api_base("https://gateway.example.com/v1") == "https://gateway.example.com/v1"
def test_openai_base_defaults_to_dashscope(monkeypatch: pytest.MonkeyPatch):
monkeypatch.delenv("DASHSCOPE_API_BASE", raising=False)
assert resolve_dashscope_openai_api_base(None) == f"{DASHSCOPE_DEFAULT_ORIGIN}/compatible-mode/v1"
def test_anthropic_base_derives_from_the_domain(monkeypatch: pytest.MonkeyPatch):
monkeypatch.delenv("DASHSCOPE_API_BASE", raising=False)
assert resolve_dashscope_anthropic_api_base(WORKSPACE) == f"{WORKSPACE}/apps/anthropic"
assert resolve_dashscope_anthropic_api_base(f"{WORKSPACE}/compatible-mode/v1") == f"{WORKSPACE}/apps/anthropic"
def test_anthropic_base_keeps_unrecognized_hosts(monkeypatch: pytest.MonkeyPatch):
monkeypatch.delenv("DASHSCOPE_API_BASE", raising=False)
assert (
resolve_dashscope_anthropic_api_base("https://gateway.example.com/anthropic")
== "https://gateway.example.com/anthropic"
)
def test_rerank_base_derives_from_the_domain(monkeypatch: pytest.MonkeyPatch):
monkeypatch.delenv("DASHSCOPE_API_BASE_RERANK", raising=False)
assert (
resolve_dashscope_family_rerank_api_base(WORKSPACE, "DASHSCOPE_API_BASE_RERANK", "https://default/reranks")
== f"{WORKSPACE}/compatible-api/v1/reranks"
)
def test_rerank_base_keeps_unrecognized_hosts():
gateway = "https://gateway.example.com/v1"
assert (
resolve_dashscope_family_rerank_api_base(gateway, "DASHSCOPE_API_BASE_RERANK", "https://default/reranks")
== gateway
)
def test_rerank_base_uses_the_configured_default(monkeypatch: pytest.MonkeyPatch):
monkeypatch.delenv("DASHSCOPE_API_BASE_RERANK", raising=False)
assert (
resolve_dashscope_family_rerank_api_base(None, "DASHSCOPE_API_BASE_RERANK", "https://default/reranks")
== "https://default/reranks"
)