Skip to content

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

fix(vlm): support OpenAI reasoning-model families (gpt-5, o1, o3, o4) (#1568)

Brian Le20d0f4d6a367
diff --git a/openviking/models/vlm/backends/openai_vlm.py b/openviking/models/vlm/backends/openai_vlm.py
index 2db58ed29..63ab0aaa5 100644
--- a/openviking/models/vlm/backends/openai_vlm.py
+++ b/openviking/models/vlm/backends/openai_vlm.py
@@ -31,6 +31,20 @@ _DASHSCOPE_HOSTS = {
}
+_REASONING_MODEL_PREFIXES = ("gpt-5", "o1", "o3", "o4")
+
+
+def _is_reasoning_model(model: Optional[str]) -> bool:
+ """OpenAI reasoning-model families reject `max_tokens` and non-default `temperature`.
+
+ They require `max_completion_tokens` and only accept `temperature=1` (server default).
+ """
+ if not model:
+ return False
+ name = model.lower()
+ return any(name.startswith(p) for p in _REASONING_MODEL_PREFIXES)
+
+
def _build_openai_client_kwargs(
provider: str,
api_key: str,
@@ -65,6 +79,7 @@ class OpenAIVLM(VLMBase):
self._sync_client = None
self._async_client = None
self.api_version = config.get("api_version")
+ self.reasoning_effort = config.get("reasoning_effort", "low")
def get_client(self):
"""Get sync client"""
@@ -260,15 +275,20 @@ class OpenAIVLM(VLMBase):
thinking: bool = False,
) -> Dict[str, Any]:
kwargs_messages = messages or [{"role": "user", "content": prompt}]
- kwargs = {
- "model": self.model or "gpt-4o-mini",
+ model = self.model or "gpt-4o-mini"
+ is_reasoning = _is_reasoning_model(model)
+ kwargs: Dict[str, Any] = {
+ "model": model,
"messages": kwargs_messages,
- "temperature": self.temperature,
"stream": self.stream,
}
+ if is_reasoning:
+ kwargs["reasoning_effort"] = self.reasoning_effort
+ else:
+ kwargs["temperature"] = self.temperature
self._apply_provider_specific_extra_body(kwargs, thinking)
if self.max_tokens is not None:
- kwargs["max_tokens"] = self.max_tokens
+ kwargs["max_completion_tokens" if is_reasoning else "max_tokens"] = self.max_tokens
if tools:
kwargs["tools"] = tools
kwargs["tool_choice"] = tool_choice or "auto"
@@ -293,15 +313,20 @@ class OpenAIVLM(VLMBase):
content.append({"type": "text", "text": prompt})
kwargs_messages = [{"role": "user", "content": content}]
- kwargs = {
- "model": self.model or "gpt-4o-mini",
+ model = self.model or "gpt-4o-mini"
+ is_reasoning = _is_reasoning_model(model)
+ kwargs: Dict[str, Any] = {
+ "model": model,
"messages": kwargs_messages,
- "temperature": self.temperature,
"stream": self.stream,
}
+ if is_reasoning:
+ kwargs["reasoning_effort"] = self.reasoning_effort
+ else:
+ kwargs["temperature"] = self.temperature
self._apply_provider_specific_extra_body(kwargs, thinking)
if self.max_tokens is not None:
- kwargs["max_tokens"] = self.max_tokens
+ kwargs["max_completion_tokens" if is_reasoning else "max_tokens"] = self.max_tokens
if tools:
kwargs["tools"] = tools
kwargs["tool_choice"] = tool_choice or "auto"
diff --git a/openviking/session/memory/utils/json_parser.py b/openviking/session/memory/utils/json_parser.py
index 6a26522a5..57f306c91 100644
--- a/openviking/session/memory/utils/json_parser.py
+++ b/openviking/session/memory/utils/json_parser.py
@@ -11,7 +11,7 @@ Layer 5: Validation Tolerance - TypeAdapter(strict=False) + list item filtering
"""
import json
-from dataclasses import is_dataclass, asdict
+from dataclasses import asdict, is_dataclass
from types import UnionType
from typing import (
Any,
@@ -26,8 +26,7 @@ from typing import (
)
import json_repair
-from pydantic import TypeAdapter, BaseModel, parse_obj_as
-
+from pydantic import BaseModel, TypeAdapter
from openviking_cli.utils import get_logger
@@ -48,23 +47,17 @@ __all__ = [
]
-
-
class PydanticEncoder(json.JSONEncoder):
def default(self, obj):
- if isinstance(obj, BaseModel) :
+ if isinstance(obj, BaseModel):
# 保存类名和属性值
- return {
- **obj.model_dump(mode='python')
- }
+ return {**obj.model_dump(mode="python")}
elif is_dataclass(obj):
return asdict(obj)
return super().default(obj)
-
class JsonUtils:
-
@staticmethod
def dumps(obj, indent=4, ensure_ascii=False):
if obj is None:
@@ -175,7 +168,7 @@ def _get_origin_type(annotation) -> Type:
if origin is Union or origin is UnionType:
args = get_args(annotation)
# Handle Optional[T] which is Union[T, None]
- if len(args) == 2 and args[1] == type(None):
+ if len(args) == 2 and args[1] is type(None):
return _get_origin_type(args[0])
elif origin is list:
return list
@@ -197,7 +190,7 @@ def _get_arg_type(annotation) -> Optional[Type]:
origin = get_origin(annotation)
if origin is Union or origin is UnionType:
args = get_args(annotation)
- if len(args) == 2 and args[1] == type(None):
+ if len(args) == 2 and args[1] is type(None):
return _get_arg_type(args[0])
elif origin is list:
args = get_args(annotation)
@@ -344,7 +337,9 @@ def parse_value_with_tolerance(value, annotation):
else:
parsed_value = value
elif origin_type is list:
- if isinstance(value, str):
+ if value is None:
+ parsed_value = []
+ elif isinstance(value, str):
parsed_value = [value]
elif isinstance(value, dict):
parsed_value = [value]
@@ -475,5 +470,3 @@ def parse_json_with_stability(
return model_class.model_validate(tolerant_data), None
except Exception as e2:
return None, f"Model validation failed even after tolerance: {e} (fallback: {e2})"
-
-
diff --git a/tests/unit/test_vlm_reasoning_models.py b/tests/unit/test_vlm_reasoning_models.py
new file mode 100644
index 000000000..1b26c46a3
--- /dev/null
+++ b/tests/unit/test_vlm_reasoning_models.py
@@ -0,0 +1,163 @@
+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.
+# SPDX-License-Identifier: AGPL-3.0
+"""Tests for reasoning-model parameter translation in the OpenAI VLM backend.
+
+OpenAI reasoning-model families (gpt-5, o1, o3, o4) reject the `max_tokens` and
+non-default `temperature` parameters. They require `max_completion_tokens` and
+only accept `temperature=1` (the server default). This module verifies that the
+OpenAI VLM backend translates parameters for reasoning models while leaving
+non-reasoning models (e.g. gpt-4o-mini) unchanged.
+"""
+
+import pytest
+
+from openviking.models.vlm.backends.openai_vlm import OpenAIVLM, _is_reasoning_model
+
+
+class TestIsReasoningModel:
+ """Pure function: reasoning-model prefix detection."""
+
+ @pytest.mark.parametrize(
+ "model",
+ [
+ "gpt-5",
+ "gpt-5-mini",
+ "gpt-5-nano-2025-08-07",
+ "GPT-5-Mini",
+ "o1",
+ "o1-preview",
+ "o1-mini",
+ "o3",
+ "o3-mini",
+ "o4-mini",
+ "o4-mini-2025-04-16",
+ ],
+ )
+ def test_reasoning_model_detected(self, model):
+ assert _is_reasoning_model(model) is True
+
+ @pytest.mark.parametrize(
+ "model",
+ [
+ "gpt-4o",
+ "gpt-4o-mini",
+ "gpt-4.1",
+ "gpt-4.1-mini",
+ "doubao-seed-2-0-pro-260215",
+ "gpt-3.5-turbo",
+ "",
+ None,
+ ],
+ )
+ def test_non_reasoning_model_not_detected(self, model):
+ assert _is_reasoning_model(model) is False
+
+
+class TestReasoningModelTextKwargs:
+ """`_build_text_kwargs` should translate params for reasoning models."""
+
+ def test_gpt5_mini_uses_max_completion_tokens(self):
+ vlm = OpenAIVLM(
+ {
+ "api_key": "sk-test",
+ "model": "gpt-5-mini",
+ "api_base": "https://api.openai.com/v1",
+ "max_tokens": 512,
+ }
+ )
+ kwargs = vlm._build_text_kwargs(prompt="hi")
+ assert kwargs["max_completion_tokens"] == 512
+ assert "max_tokens" not in kwargs
+ assert "temperature" not in kwargs
+ assert kwargs["reasoning_effort"] == "low"
+
+ def test_o3_mini_uses_max_completion_tokens(self):
+ vlm = OpenAIVLM(
+ {
+ "api_key": "sk-test",
+ "model": "o3-mini",
+ "api_base": "https://api.openai.com/v1",
+ "max_tokens": 256,
+ }
+ )
+ kwargs = vlm._build_text_kwargs(prompt="hi")
+ assert kwargs["max_completion_tokens"] == 256
+ assert "max_tokens" not in kwargs
+ assert "temperature" not in kwargs
+ assert kwargs["reasoning_effort"] == "low"
+
+ def test_reasoning_effort_overridable_via_config(self):
+ vlm = OpenAIVLM(
+ {
+ "api_key": "sk-test",
+ "model": "gpt-5-mini",
+ "api_base": "https://api.openai.com/v1",
+ "max_tokens": 512,
+ "reasoning_effort": "high",
+ }
+ )
+ kwargs = vlm._build_text_kwargs(prompt="hi")
+ assert kwargs["reasoning_effort"] == "high"
+
+ def test_gpt4o_mini_keeps_max_tokens_and_temperature(self):
+ vlm = OpenAIVLM(
+ {
+ "api_key": "sk-test",
+ "model": "gpt-4o-mini",
+ "api_base": "https://api.openai.com/v1",
+ "max_tokens": 512,
+ "temperature": 0.5,
+ }
+ )
+ kwargs = vlm._build_text_kwargs(prompt="hi")
+ assert kwargs["max_tokens"] == 512
+ assert "max_completion_tokens" not in kwargs
+ assert kwargs["temperature"] == 0.5
+ assert "reasoning_effort" not in kwargs
+
+ def test_reasoning_model_without_max_tokens_omits_both(self):
+ vlm = OpenAIVLM(
+ {
+ "api_key": "sk-test",
+ "model": "gpt-5",
+ "api_base": "https://api.openai.com/v1",
+ }
+ )
+ kwargs = vlm._build_text_kwargs(prompt="hi")
+ assert "max_tokens" not in kwargs
+ assert "max_completion_tokens" not in kwargs
+ assert "temperature" not in kwargs
+
+
+class TestReasoningModelVisionKwargs:
+ """`_build_vision_kwargs` should apply the same translation."""
+
+ def test_gpt5_mini_vision_uses_max_completion_tokens(self):
+ vlm = OpenAIVLM(
+ {
+ "api_key": "sk-test",
+ "model": "gpt-5-mini",
+ "api_base": "https://api.openai.com/v1",
+ "max_tokens": 1024,
+ }
+ )
+ kwargs = vlm._build_vision_kwargs(prompt="describe this")
+ assert kwargs["max_completion_tokens"] == 1024
+ assert "max_tokens" not in kwargs
+ assert "temperature" not in kwargs
+ assert kwargs["reasoning_effort"] == "low"
+
+ def test_gpt4o_vision_keeps_max_tokens_and_temperature(self):
+ vlm = OpenAIVLM(
+ {
+ "api_key": "sk-test",
+ "model": "gpt-4o",
+ "api_base": "https://api.openai.com/v1",
+ "max_tokens": 1024,
+ "temperature": 0.2,
+ }
+ )
+ kwargs = vlm._build_vision_kwargs(prompt="describe this")
+ assert kwargs["max_tokens"] == 1024
+ assert "max_completion_tokens" not in kwargs
+ assert kwargs["temperature"] == 0.2