Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
fix(vlm): strip <think> reasoning tags from model responses before storage (#690)
Qin Haojie7ea7f356807f
diff --git a/openviking/models/vlm/backends/litellm_vlm.py b/openviking/models/vlm/backends/litellm_vlm.pyindex 7a951d732..c64e16530 100644--- a/openviking/models/vlm/backends/litellm_vlm.py+++ b/openviking/models/vlm/backends/litellm_vlm.py@@ -224,7 +224,7 @@ class LiteLLMVLMProvider(VLMBase):response = completion(**kwargs)self._update_token_usage_from_response(response)- return response.choices[0].message.content or ""+ return self._clean_response(response.choices[0].message.content or "")async def get_completion_async(self, prompt: str, thinking: bool = False, max_retries: int = 0@@ -239,7 +239,7 @@ class LiteLLMVLMProvider(VLMBase):try:response = await acompletion(**kwargs)self._update_token_usage_from_response(response)- return response.choices[0].message.content or ""+ return self._clean_response(response.choices[0].message.content or "")except Exception as e:last_error = eif attempt < max_retries:@@ -268,7 +268,7 @@ class LiteLLMVLMProvider(VLMBase):response = completion(**kwargs)self._update_token_usage_from_response(response)- return response.choices[0].message.content or ""+ return self._clean_response(response.choices[0].message.content or "")async def get_vision_completion_async(self,@@ -289,7 +289,7 @@ class LiteLLMVLMProvider(VLMBase):response = await acompletion(**kwargs)self._update_token_usage_from_response(response)- return response.choices[0].message.content or ""+ return self._clean_response(response.choices[0].message.content or "")def _update_token_usage_from_response(self, response) -> None:"""Update token usage from response."""diff --git a/openviking/models/vlm/backends/openai_vlm.py b/openviking/models/vlm/backends/openai_vlm.pyindex de59b8dc7..68649364e 100644--- a/openviking/models/vlm/backends/openai_vlm.py+++ b/openviking/models/vlm/backends/openai_vlm.py@@ -65,7 +65,7 @@ class OpenAIVLM(VLMBase):response = client.chat.completions.create(**kwargs)self._update_token_usage_from_response(response)- return response.choices[0].message.content or ""+ return self._clean_response(response.choices[0].message.content or "")async def get_completion_async(self, prompt: str, thinking: bool = False, max_retries: int = 0@@ -83,7 +83,7 @@ class OpenAIVLM(VLMBase):try:response = await client.chat.completions.create(**kwargs)self._update_token_usage_from_response(response)- return response.choices[0].message.content or ""+ return self._clean_response(response.choices[0].message.content or "")except Exception as e:last_error = eif attempt < max_retries:@@ -168,7 +168,7 @@ class OpenAIVLM(VLMBase):response = client.chat.completions.create(**kwargs)self._update_token_usage_from_response(response)- return response.choices[0].message.content or ""+ return self._clean_response(response.choices[0].message.content or "")async def get_vision_completion_async(self,@@ -192,4 +192,4 @@ class OpenAIVLM(VLMBase):response = await client.chat.completions.create(**kwargs)self._update_token_usage_from_response(response)- return response.choices[0].message.content or ""+ return self._clean_response(response.choices[0].message.content or "")diff --git a/openviking/models/vlm/backends/volcengine_vlm.py b/openviking/models/vlm/backends/volcengine_vlm.pyindex 985025cac..7880818a4 100644--- a/openviking/models/vlm/backends/volcengine_vlm.py+++ b/openviking/models/vlm/backends/volcengine_vlm.py@@ -71,7 +71,7 @@ class VolcEngineVLM(OpenAIVLM):response = client.chat.completions.create(**kwargs)self._update_token_usage_from_response(response)- return response.choices[0].message.content or ""+ return self._clean_response(response.choices[0].message.content or "")async def get_completion_async(self, prompt: str, thinking: bool = False, max_retries: int = 0@@ -90,7 +90,7 @@ class VolcEngineVLM(OpenAIVLM):try:response = await client.chat.completions.create(**kwargs)self._update_token_usage_from_response(response)- return response.choices[0].message.content or ""+ return self._clean_response(response.choices[0].message.content or "")except Exception as e:last_error = eif attempt < max_retries:@@ -238,7 +238,7 @@ class VolcEngineVLM(OpenAIVLM):response = client.chat.completions.create(**kwargs)self._update_token_usage_from_response(response)- return response.choices[0].message.content or ""+ return self._clean_response(response.choices[0].message.content or "")async def get_vision_completion_async(self,@@ -263,4 +263,4 @@ class VolcEngineVLM(OpenAIVLM):response = await client.chat.completions.create(**kwargs)self._update_token_usage_from_response(response)- return response.choices[0].message.content or ""+ return self._clean_response(response.choices[0].message.content or "")diff --git a/openviking/models/vlm/base.py b/openviking/models/vlm/base.pyindex 66b859ba1..5d8676719 100644--- a/openviking/models/vlm/base.py+++ b/openviking/models/vlm/base.py@@ -2,6 +2,7 @@# SPDX-License-Identifier: Apache-2.0"""VLM base interface and abstract classes"""+import refrom abc import ABC, abstractmethodfrom pathlib import Pathfrom typing import Any, Dict, List, Union@@ -10,6 +11,8 @@ from openviking.utils.time_utils import format_iso8601from .token_usage import TokenUsageTracker+_THINK_TAG_RE = re.compile(r"<think>[\s\S]*?</think>")+class VLMBase(ABC):"""VLM base abstract class"""@@ -58,6 +61,10 @@ class VLMBase(ABC):"""Get vision completion asynchronously"""pass+ def _clean_response(self, content: str) -> str:+ """Strip reasoning tags (e.g. ``<think>...</think>``) from model output."""+ return _THINK_TAG_RE.sub("", content).strip()+def is_available(self) -> bool:"""Check if available"""return self.api_key is not None or self.api_base is not Nonediff --git a/tests/models/test_vlm_strip_think_tags.py b/tests/models/test_vlm_strip_think_tags.pynew file mode 100644index 000000000..fd47fa1c4--- /dev/null+++ b/tests/models/test_vlm_strip_think_tags.py@@ -0,0 +1,85 @@+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.+# SPDX-License-Identifier: Apache-2.0+"""Tests for stripping <think> reasoning tags from VLM responses."""++import pytest++from openviking.models.vlm.base import _THINK_TAG_RE, VLMBase+++class TestStripThinkTags:+ """Test _clean_response strips <think> blocks correctly."""++ @pytest.fixture()+ def vlm(self):+ """Create a minimal concrete VLMBase for testing."""++ class _Stub(VLMBase):+ def get_completion(self, prompt, thinking=False):+ return ""++ async def get_completion_async(self, prompt, thinking=False, max_retries=0):+ return ""++ def get_vision_completion(self, prompt, images, thinking=False):+ return ""++ async def get_vision_completion_async(self, prompt, images, thinking=False):+ return ""++ return _Stub({"api_key": "test"})++ def test_no_think_tags(self, vlm):+ text = "This is a normal response."+ assert vlm._clean_response(text) == "This is a normal response."++ def test_single_think_block(self, vlm):+ text = "<think>\nI need to analyze this.\n</think>\nThe actual summary."+ assert vlm._clean_response(text) == "The actual summary."++ def test_think_block_at_end(self, vlm):+ text = "Summary text.\n<think>some reasoning</think>"+ assert vlm._clean_response(text) == "Summary text."++ def test_think_block_in_middle(self, vlm):+ text = "Start.<think>reasoning here</think>End."+ assert vlm._clean_response(text) == "Start.End."++ def test_multiple_think_blocks(self, vlm):+ text = "<think>first</think>Hello<think>second</think> world"+ assert vlm._clean_response(text) == "Hello world"++ def test_multiline_think_block(self, vlm):+ text = (+ "<think>\nStep 1: analyze the document\n"+ "Step 2: summarize\nStep 3: output\n</think>\n"+ "# Directory Overview\n\nThis directory contains..."+ )+ result = vlm._clean_response(text)+ assert result.startswith("# Directory Overview")+ assert "<think>" not in result++ def test_empty_string(self, vlm):+ assert vlm._clean_response("") == ""++ def test_only_think_block(self, vlm):+ text = "<think>all reasoning, no output</think>"+ assert vlm._clean_response(text) == ""++ def test_nested_angle_brackets_preserved(self, vlm):+ text = "Use <b>bold</b> and <i>italic</i> formatting."+ assert vlm._clean_response(text) == text++ def test_json_with_think_prefix(self, vlm):+ text = '<think>let me think</think>\n{"abstract": "summary", "overview": "details"}'+ result = vlm._clean_response(text)+ assert result == '{"abstract": "summary", "overview": "details"}'+++class TestThinkTagRegex:+ """Test the compiled regex pattern directly."""++ def test_greedy_minimal(self):+ """Ensure non-greedy matching: each <think>...</think> is matched individually."""+ text = "<think>a</think>KEEP<think>b</think>"+ assert _THINK_TAG_RE.sub("", text) == "KEEP"