Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(providers): force temperature=1 for the whole Kimi K-series

sambazhu03adce85a350
diff --git a/agent/src/providers/llm.py b/agent/src/providers/llm.py
index c4524a333..17a774d4f 100644
--- a/agent/src/providers/llm.py
+++ b/agent/src/providers/llm.py
@@ -4,6 +4,7 @@ from __future__ import annotations
import logging
import os
+import re
from collections.abc import Sequence
from importlib import import_module
from importlib.metadata import PackageNotFoundError, version
@@ -298,6 +299,11 @@ _ENV_CANDIDATES = [
# constants only.
_ENV_LABELS = ("~/.vibe-trading/.env", "<AGENT_DIR>/.env", "<CWD>/.env")
+# Kimi reasoning models (K-series: kimi-k2*, kimi-k3, …, and the
+# kimi-for-coding alias) reject any temperature other than 1 with
+# "invalid temperature: only 1 is allowed for this model".
+_KIMI_FORCED_TEMPERATURE_RE = re.compile(r"kimi-(k\d+|for-coding)", re.IGNORECASE)
+
logger = logging.getLogger(__name__)
_dotenv_loaded: bool = False
@@ -625,7 +631,7 @@ def build_llm(*, model_name: Optional[str] = None, callbacks: Any = None) -> Any
# ("invalid temperature: only 1 is allowed for this model").
if (
caps.name in {"moonshot", "kimi-coding"}
- and name.lower().startswith(("kimi-k2", "kimi-for-coding"))
+ and _KIMI_FORCED_TEMPERATURE_RE.match(name)
and temperature != 1.0
):
logger.info("Forcing temperature=1.0 for %s (provider requirement)", name)
diff --git a/agent/tests/test_llm.py b/agent/tests/test_llm.py
index 1fb35c277..e06195d0d 100644
--- a/agent/tests/test_llm.py
+++ b/agent/tests/test_llm.py
@@ -263,6 +263,53 @@ class TestMinimaxTemperature:
assert captured["temperature"] == 0.7
+# ---------------------------------------------------------------------------
+# Kimi K-series temperature forcing
+# ---------------------------------------------------------------------------
+
+
+class TestKimiTemperature:
+ """Kimi reasoning models reject any temperature other than 1."""
+
+ def _capture_temperature(self, model: str, configured_temp: str) -> float:
+ import src.providers.llm as llm_mod
+ llm_mod._dotenv_loaded = True
+
+ captured: dict[str, float] = {}
+
+ class _FakeChatOpenAI:
+ def __init__(self, **kwargs: object) -> None:
+ captured["temperature"] = float(kwargs.get("temperature", -1))
+
+ env = {
+ "LANGCHAIN_PROVIDER": "moonshot",
+ "MOONSHOT_API_KEY": "moonshot-key",
+ "MOONSHOT_BASE_URL": "https://api.kimi.com/coding/v1",
+ "LANGCHAIN_MODEL_NAME": model,
+ "LANGCHAIN_TEMPERATURE": configured_temp,
+ }
+ with patch.dict(os.environ, env, clear=True):
+ with patch.object(llm_mod, "ChatOpenAIWithReasoning", _FakeChatOpenAI):
+ build_llm()
+ return captured["temperature"]
+
+ def test_kimi_k3_temperature_forced_to_one(self) -> None:
+ """kimi-k3 must be forced to 1.0 (API rejects other values)."""
+ assert self._capture_temperature("kimi-k3", "0.0") == 1.0
+
+ def test_kimi_k2_temperature_forced_to_one(self) -> None:
+ """Regression: kimi-k2.x keeps the existing forcing behavior."""
+ assert self._capture_temperature("kimi-k2.6", "0.0") == 1.0
+
+ def test_kimi_for_coding_temperature_forced_to_one(self) -> None:
+ """Regression: kimi-for-coding alias keeps the existing behavior."""
+ assert self._capture_temperature("kimi-for-coding", "0.5") == 1.0
+
+ def test_non_k_series_temperature_preserved(self) -> None:
+ """Non-reasoning Moonshot models keep the configured temperature."""
+ assert self._capture_temperature("moonshot-v1-8k", "0.0") == 0.0
+
+
class TestReasoningEffortPassthrough:
"""LANGCHAIN_REASONING_EFFORT is forwarded as extra_body.reasoning.effort
to the underlying OpenAI-compatible client. Used for OpenRouter-style