Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(vlm): pass thinking flag to dashscope openai backend (#939)

sacloudb4a49def30d4
diff --git a/openviking/models/vlm/backends/openai_vlm.py b/openviking/models/vlm/backends/openai_vlm.py
index fdf35f025..31f871315 100644
--- a/openviking/models/vlm/backends/openai_vlm.py
+++ b/openviking/models/vlm/backends/openai_vlm.py
@@ -8,6 +8,7 @@ import json
import logging
import time
from pathlib import Path
+from urllib.parse import urlparse
from typing import Any, Dict, List, Optional, Union
from ..base import VLMBase, VLMResponse, ToolCall
@@ -16,6 +17,11 @@ from ..registry import DEFAULT_AZURE_API_VERSION
logger = logging.getLogger(__name__)
+_DASHSCOPE_HOSTS = {
+ "dashscope.aliyuncs.com",
+ "dashscope-intl.aliyuncs.com",
+}
+
def _build_openai_client_kwargs(
provider: str,
api_key: str,
@@ -82,6 +88,31 @@ class OpenAIVLM(VLMBase):
self._async_client = openai.AsyncOpenAI(**kwargs)
return self._async_client
+ def _supports_enable_thinking(self) -> bool:
+ """Return True for OpenAI-compatible DashScope endpoints that accept enable_thinking."""
+ if self.provider != "openai":
+ return False
+
+ if isinstance(self.model, str) and self.model.lower().startswith("dashscope/"):
+ return True
+
+ if not self.api_base:
+ return False
+
+ try:
+ host = urlparse(self.api_base).hostname or ""
+ except ValueError:
+ return False
+
+ return host.lower() in _DASHSCOPE_HOSTS
+
+ def _apply_provider_specific_extra_body(
+ self, kwargs: Dict[str, Any], thinking: bool
+ ) -> None:
+ """Attach provider-specific raw body parameters understood by compatible APIs."""
+ if self._supports_enable_thinking():
+ kwargs["extra_body"] = {"enable_thinking": bool(thinking)}
+
def _update_token_usage_from_response(
self, response, duration_seconds: float = 0.0,
):
@@ -247,6 +278,7 @@ class OpenAIVLM(VLMBase):
"temperature": self.temperature,
"stream": self.stream,
}
+ self._apply_provider_specific_extra_body(kwargs, thinking)
if self.max_tokens is not None:
kwargs["max_tokens"] = self.max_tokens
@@ -292,6 +324,7 @@ class OpenAIVLM(VLMBase):
"temperature": self.temperature,
"stream": self.stream,
}
+ self._apply_provider_specific_extra_body(kwargs, thinking)
if self.max_tokens is not None:
kwargs["max_tokens"] = self.max_tokens
@@ -409,6 +442,7 @@ class OpenAIVLM(VLMBase):
"temperature": self.temperature,
"stream": self.stream,
}
+ self._apply_provider_specific_extra_body(kwargs, thinking)
if self.max_tokens is not None:
kwargs["max_tokens"] = self.max_tokens
@@ -460,6 +494,7 @@ class OpenAIVLM(VLMBase):
"temperature": self.temperature,
"stream": self.stream,
}
+ self._apply_provider_specific_extra_body(kwargs, thinking)
if self.max_tokens is not None:
kwargs["max_tokens"] = self.max_tokens
@@ -481,4 +516,4 @@ class OpenAIVLM(VLMBase):
self._update_token_usage_from_response(response, duration_seconds=elapsed)
content = self._extract_content_from_response(response)
- return self._clean_response(content)
\ No newline at end of file
+ return self._clean_response(content)
diff --git a/tests/unit/test_extra_headers_vlm.py b/tests/unit/test_extra_headers_vlm.py
index 13735ac87..bcc8e85af 100644
--- a/tests/unit/test_extra_headers_vlm.py
+++ b/tests/unit/test_extra_headers_vlm.py
@@ -2,7 +2,7 @@
# SPDX-License-Identifier: Apache-2.0
"""Tests for VLM extra_headers support."""
-from unittest.mock import MagicMock, patch
+from unittest.mock import AsyncMock, MagicMock, patch
import pytest
@@ -99,6 +99,110 @@ class TestVLMExtraHeaders:
# Empty dict is falsy, so default_headers should not be set
assert "default_headers" not in call_kwargs
+ @patch("openviking.models.vlm.backends.openai_vlm.openai.OpenAI")
+ def test_dashscope_text_completion_passes_enable_thinking_in_extra_body(
+ self, mock_openai_class
+ ):
+ """DashScope-compatible OpenAI backends should pass thinking via extra_body."""
+ mock_client = MagicMock()
+ mock_openai_class.return_value = mock_client
+ mock_response = MagicMock()
+ mock_response.choices = [MagicMock(message=MagicMock(content="ok"), finish_reason="stop")]
+ mock_response.usage = None
+ mock_client.chat.completions.create.return_value = mock_response
+
+ vlm = OpenAIVLM(
+ {
+ "api_key": "sk-test",
+ "api_base": "https://dashscope.aliyuncs.com/compatible-mode/v1",
+ "model": "dashscope/qwen3.5-plus",
+ }
+ )
+
+ vlm.get_completion("hello", thinking=False)
+
+ call_kwargs = mock_client.chat.completions.create.call_args.kwargs
+ assert call_kwargs["extra_body"] == {"enable_thinking": False}
+
+ @patch("openviking.models.vlm.backends.openai_vlm.openai.AsyncOpenAI")
+ async def test_dashscope_async_vision_completion_passes_enable_thinking_in_extra_body(
+ self, mock_async_openai_class
+ ):
+ """DashScope-compatible async vision calls should pass thinking via extra_body."""
+ mock_client = MagicMock()
+ mock_async_openai_class.return_value = mock_client
+ mock_response = MagicMock()
+ mock_response.choices = [MagicMock(message=MagicMock(content="ok"), finish_reason="stop")]
+ mock_response.usage = None
+ mock_client.chat.completions.create = AsyncMock(return_value=mock_response)
+
+ vlm = OpenAIVLM(
+ {
+ "api_key": "sk-test",
+ "api_base": "https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
+ "model": "qwen3.5-flash",
+ }
+ )
+
+ await vlm.get_vision_completion_async(
+ prompt="describe",
+ images=[b"\x89PNG\r\n\x1a\n0000"],
+ thinking=True,
+ )
+
+ call_kwargs = mock_client.chat.completions.create.call_args.kwargs
+ assert call_kwargs["extra_body"] == {"enable_thinking": True}
+
+ @patch("openviking.models.vlm.backends.openai_vlm.openai.OpenAI")
+ def test_official_openai_text_completion_does_not_set_enable_thinking(
+ self, mock_openai_class
+ ):
+ """Official OpenAI API should not receive DashScope-specific extra_body flags."""
+ mock_client = MagicMock()
+ mock_openai_class.return_value = mock_client
+ mock_response = MagicMock()
+ mock_response.choices = [MagicMock(message=MagicMock(content="ok"), finish_reason="stop")]
+ mock_response.usage = None
+ mock_client.chat.completions.create.return_value = mock_response
+
+ vlm = OpenAIVLM(
+ {
+ "api_key": "sk-test",
+ "api_base": "https://api.openai.com/v1",
+ "model": "gpt-4o-mini",
+ }
+ )
+
+ vlm.get_completion("hello", thinking=False)
+
+ call_kwargs = mock_client.chat.completions.create.call_args.kwargs
+ assert "extra_body" not in call_kwargs
+
+ @patch("openviking.models.vlm.backends.openai_vlm.openai.AzureOpenAI")
+ def test_azure_text_completion_does_not_set_enable_thinking(self, mock_azure_openai_class):
+ """Azure OpenAI should not receive DashScope-specific extra_body flags."""
+ mock_client = MagicMock()
+ mock_azure_openai_class.return_value = mock_client
+ mock_response = MagicMock()
+ mock_response.choices = [MagicMock(message=MagicMock(content="ok"), finish_reason="stop")]
+ mock_response.usage = None
+ mock_client.chat.completions.create.return_value = mock_response
+
+ vlm = OpenAIVLM(
+ {
+ "provider": "azure",
+ "api_key": "sk-test",
+ "api_base": "https://example-resource.openai.azure.com",
+ "api_version": "2025-01-01-preview",
+ "model": "gpt-4o-mini",
+ }
+ )
+
+ vlm.get_completion("hello", thinking=False)
+
+ call_kwargs = mock_client.chat.completions.create.call_args.kwargs
+ assert "extra_body" not in call_kwargs
+
class TestVLMBaseExtraHeaders:
"""Test VLMBase extracts extra_headers from config."""