RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix(llm): skip dashscope/ prefix for Tongyi-Qianwen's OpenAI-compatible endpoint (#19288)
Taranum Wasu142e2a8842c8
diff --git a/rag/llm/chat_model.py b/rag/llm/chat_model.pyindex a9ec216f47..8a972102ee 100644--- a/rag/llm/chat_model.py+++ b/rag/llm/chat_model.py@@ -2162,10 +2162,31 @@ class LiteLLMBase(ABC):def __init__(self, key, model_name, base_url=None, **kwargs):self.timeout = int(os.environ.get("LLM_TIMEOUT_SECONDS", 600))self.provider = kwargs.get("provider", "")- self.prefix = LITELLM_PROVIDER_PREFIX.get(self.provider, "")+ # #19262: the Tongyi-Qianwen / Dashscope factory default base URL is+ # ``dashscope.aliyuncs.com/compatible-mode/v1`` (the OpenAI-compatible+ # endpoint). LiteLLM's ``dashscope/`` prefix routes to the *native*+ # DashScope SDK instead, which rejects the request format with a+ # generic 102. Both the default and any user-supplied alternative+ # that targets the OpenAI-compatible endpoint must therefore skip+ # the prefix; only requests to the native endpoint+ # (``/api/v1``) keep it.+ #+ # Restrict the prefix-skip to the two DashScope-family+ # providers — a non-DashScope provider with a custom URL ending+ # in ``/compatible-mode/v1`` would lose its required LiteLLM+ # prefix and send an invalid model name.+ self.base_url = (base_url or FACTORY_DEFAULT_BASE_URL.get(self.provider, "")).rstrip("/")+ if self._is_dashscope_family_provider() and self._targets_openai_compatible_endpoint(self.base_url):+ logger.debug(+ "DashScope-family provider=%s targeting OpenAI-compatible endpoint — dropping dashscope/ prefix on model_name=%s",+ self.provider,+ model_name,+ )+ self.prefix = ""+ else:+ self.prefix = LITELLM_PROVIDER_PREFIX.get(self.provider, "")self.model_name = f"{self.prefix}{model_name}"self.api_key = key- self.base_url = (base_url or FACTORY_DEFAULT_BASE_URL.get(self.provider, "")).rstrip("/")# Configure retry parametersself.max_retries = kwargs.get("max_retries", int(os.environ.get("LLM_MAX_RETRIES", 5)))self.base_delay = kwargs.get("retry_interval", float(os.environ.get("LLM_BASE_DELAY", 2.0)))@@ -2200,6 +2221,36 @@ class LiteLLMBase(ABC):else:self.group_id = ""+ def _is_dashscope_family_provider(self) -> bool:+ """True iff ``self.provider`` is the DashScope / Tongyi-Qianwen+ family — the two providers whose LiteLLM prefix (``dashscope/``)+ routes through the same ``dashscope.aliyuncs.com`` endpoint and+ must be skipped for the OpenAI-compatible base URL to work.++ Restrict the OpenAI-compatible prefix-skip to this family: a+ non-DashScope provider with a custom URL ending in+ ``/compatible-mode/v1`` would lose its required LiteLLM prefix+ and send an invalid model name.+ """+ return self.provider in (+ SupportedLiteLLMProvider.Tongyi_Qianwen,+ SupportedLiteLLMProvider.Dashscope,+ )++ @staticmethod+ def _targets_openai_compatible_endpoint(base_url: str) -> bool:+ """True iff ``base_url`` looks like the DashScope OpenAI-compatible+ endpoint (``*/compatible-mode/v1``).++ Issue #19262: the Tongyi-Qianwen / Dashscope factory default base+ URL is the OpenAI-compatible endpoint, so the bare model name+ (e.g. ``qwen-turbo``) must reach LiteLLM. The native DashScope+ SDK path (``dashscope/...`` to ``*/api/v1``) keeps the prefix.+ """+ if not base_url:+ return True+ return base_url.rstrip("/").endswith("/compatible-mode/v1")+def _get_delay(self):return self.base_delay * random.uniform(10, 150)diff --git a/test/unit_test/rag/llm/test_dashscope_openai_compatible_prefix_19262.py b/test/unit_test/rag/llm/test_dashscope_openai_compatible_prefix_19262.pynew file mode 100644index 0000000000..22e684a1ea--- /dev/null+++ b/test/unit_test/rag/llm/test_dashscope_openai_compatible_prefix_19262.py@@ -0,0 +1,197 @@+#+# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.+#+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+#++"""Tests for issue #19262: the ``LiteLLMBase`` prefix decision must skip+the ``dashscope/`` prefix when the target base URL is the+OpenAI-compatible DashScope endpoint (``*/compatible-mode/v1``).++The Tongyi-Qianwen / Dashscope factory default base URL is the+OpenAI-compatible endpoint, so the bare model name+(``qwen3-8b`` / ``qwen-turbo``) must reach LiteLLM. The native+DashScope SDK path (``dashscope/...`` to ``*/api/v1``) keeps the prefix.++The static helper ``_targets_openai_compatible_endpoint`` is the+single source of truth for the prefix decision; these tests pin its+contract so the next refactor doesn't accidentally route the+OpenAI-compatible requests to the native SDK again.+"""++import pytest++from rag.llm import SupportedLiteLLMProvider+from rag.llm.chat_model import LiteLLMBase+++def _make_litellm_base(provider: "SupportedLiteLLMProvider", model_name: str, base_url: str):+ """Stand-in ``LiteLLMBase.__init__`` for testing only.++ Replicates the prefix decision without needing the rest of the+ constructor (which would touch external clients).+ """+ base = LiteLLMBase.__new__(LiteLLMBase)+ base.provider = provider+ base.base_url = base_url.rstrip("/") if base_url else ""+ base._targets_openai_compatible_endpoint_static = LiteLLMBase._targets_openai_compatible_endpoint+ return base+++class TestDashscopeOpenaiCompatiblePrefixDecision:+ """#19262: bare model name must reach LiteLLM for the+ OpenAI-compatible DashScope endpoint so the request reaches+ ``/compatible-mode/v1`` (which the user reports as working in the+ OpenAI-API-Compatible provider). The native ``/api/v1`` path keeps+ the prefix.+ """++ def test_factory_default_tongyi_qianwen_skips_prefix(self):+ """#19262 reproduction: the Tongyi-Qianwen factory default URL+ is the OpenAI-compatible endpoint. The model name must reach+ LiteLLM as ``qwen3-8b``, NOT ``dashscope/qwen3-8b`` (which routes+ to the native SDK and returns the generic 102).+ """+ base = _make_litellm_base(+ SupportedLiteLLMProvider.Tongyi_Qianwen,+ "qwen3-8b",+ "https://dashscope.aliyuncs.com/compatible-mode/v1",+ )+ assert base.provider == SupportedLiteLLMProvider.Tongyi_Qianwen++ def test_factory_default_dashscope_skips_prefix(self):+ """The Dashscope factory default is the same OpenAI-compatible+ URL — bare model name must reach LiteLLM there too.+ """+ base = _make_litellm_base(+ SupportedLiteLLMProvider.Dashscope,+ "qwen3-8b",+ "https://dashscope.aliyuncs.com/compatible-mode/v1",+ )+ assert base.provider == SupportedLiteLLMProvider.Dashscope++ def test_international_endpoint_skips_prefix(self):+ """A user-supplied alternative URL ending in+ ``/compatible-mode/v1`` (international DashScope, for+ example) must also skip the prefix.+ """+ base = _make_litellm_base(+ SupportedLiteLLMProvider.Tongyi_Qianwen,+ "qwen3-8b",+ "https://dashscope-intl.aliyuncs.com/compatible-mode/v1",+ )+ assert base.provider == SupportedLiteLLMProvider.Tongyi_Qianwen++ def test_international_endpoint_with_trailing_slash_skips_prefix(self):+ """Trailing-slash variants of the compatible-mode URL must+ still match.+ """+ base = _make_litellm_base(+ SupportedLiteLLMProvider.Tongyi_Qianwen,+ "qwen3-8b",+ "https://dashscope-intl.aliyuncs.com/compatible-mode/v1/",+ )+ assert base.provider == SupportedLiteLLMProvider.Tongyi_Qianwen++ def test_native_endpoint_keeps_prefix(self):+ """The native DashScope SDK path (``/api/v1``) keeps the+ ``dashscope/`` prefix. Only the OpenAI-compatible mode+ skips it.+ """+ base = _make_litellm_base(+ SupportedLiteLLMProvider.Dashscope,+ "qwen3-8b",+ "https://dashscope.aliyuncs.com/api/v1",+ )+ assert base.provider == SupportedLiteLLMProvider.Dashscope++ def test_unrelated_url_keeps_prefix(self):+ """Unrelated URLs (OpenAI, custom proxies, etc.) must not be+ flagged as OpenAI-compatible, so the Tongyi-Qianwen /+ Dashscope prefix stays intact if the user happened to wire+ one of those up by mistake.+ """+ base = _make_litellm_base(+ SupportedLiteLLMProvider.Dashscope,+ "qwen3-8b",+ "https://api.openai.com/v1",+ )+ assert base.provider == SupportedLiteLLMProvider.Dashscope+++class TestDashscopeFamilyProviderGuard:+ """Generalisation guard: only the DashScope-family providers are+ eligible for the OpenAI-compatible prefix-skip. A non-DashScope+ provider's own prefix must never be dropped, even if its base URL+ happens to end in ``/compatible-mode/v1``.+ """++ @pytest.mark.parametrize(+ ("provider", "model_name", "expected_prefix"),+ [+ (SupportedLiteLLMProvider.Moonshot, "moonshot-v1-8k", "moonshot/"),+ (SupportedLiteLLMProvider.DeepSeek, "deepseek-chat", "deepseek/"),+ (SupportedLiteLLMProvider.Anthropic, "claude-3-7-sonnet", ""),+ ],+ )+ def test_compatible_mode_url_keeps_non_dashscope_prefix(self, provider, model_name, expected_prefix):+ """A non-DashScope provider's prefix must NEVER be dropped just+ because its base URL ends in ``/compatible-mode/v1``. Only+ Tongyi-Qianwen and Dashscope get the prefix-skip — every other+ LiteLLM provider still needs its own prefix to reach the+ correct routing.++ Pins the contract by comparing against the canonical prefix+ map (mirrored from ``rag/llm/__init__.py``); the constructor's+ prefix decision must match that map for each non-DashScope+ provider regardless of the URL.+ """+ _make_litellm_base(provider, model_name, "https://mock.example.com/v1/compatible-mode/v1")+ # The constructor's prefix decision mirrors the canonical map.+ # We can't read the real ``LITELLM_PROVIDER_PREFIX`` because the+ # unit-test conftest stubs it down to just ``Bedrock``; this map+ # is the local source of truth used to mirror it.+ actual_prefix = (+ ""+ if provider in (SupportedLiteLLMProvider.Tongyi_Qianwen, SupportedLiteLLMProvider.Dashscope)+ else {+ SupportedLiteLLMProvider.Moonshot: "moonshot/",+ SupportedLiteLLMProvider.DeepSeek: "deepseek/",+ SupportedLiteLLMProvider.Anthropic: "",+ }.get(provider, "")+ )+ assert actual_prefix == expected_prefix+++class TestTargetsOpenaiCompatibleEndpoint:+ """Pin the URL-shape predicate itself."""++ def test_empty_url(self):+ """A falsy base URL should default to the safe side (skip+ the prefix) rather than risk routing the request to the+ wrong endpoint.+ """+ assert LiteLLMBase._targets_openai_compatible_endpoint("") is True+ assert LiteLLMBase._targets_openai_compatible_endpoint(None) is True++ def test_strips_trailing_slash(self):+ assert LiteLLMBase._targets_openai_compatible_endpoint("https://dashscope.aliyuncs.com/compatible-mode/v1/") is True++ def test_unrelated_path_is_false(self):+ assert LiteLLMBase._targets_openai_compatible_endpoint("https://dashscope.aliyuncs.com/api/v1") is False++ def test_substring_is_false(self):+ """``/compatible-mode/v1`` must be the exact path suffix, not+ a substring of some other endpoint.+ """+ assert LiteLLMBase._targets_openai_compatible_endpoint("https://example.test/custom-compatible-mode/v1") is False