Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: let --no-reasoning-preserve and a custom chat template turn off the Qwen3.8 preserve_thinking default (#15170)
Debojyoti Banerjee808515e22dfe
diff --git a/studio/backend/core/inference/llama_cpp.py b/studio/backend/core/inference/llama_cpp.pyindex 08fb0a169a8..5622f116446 100644--- a/studio/backend/core/inference/llama_cpp.py+++ b/studio/backend/core/inference/llama_cpp.py@@ -5505,6 +5505,57 @@ def _reasoning_from_env(env: Mapping[str, str]) -> Optional[bool]:return None+def _reasoning_preserve_from_args_or_env(+ extra_args: Optional[Iterable[str]], env: Mapping[str, str]+) -> Optional[bool]:+ """The user's --[no-]reasoning-preserve choice, else None: last flag wins, as in+ llama.cpp's parser, and the extras beat the inherited LLAMA_ARG_REASONING_PRESERVE."""+ choice: Optional[bool] = None+ for raw in extra_args or ():+ flag = _flag_name(str(raw))+ if flag == "--reasoning-preserve":+ choice = True+ elif flag == "--no-reasoning-preserve":+ choice = False+ if choice is not None:+ return choice+ value = env.get("LLAMA_ARG_REASONING_PRESERVE")+ if value in _LLAMA_ARG_TRUE_VALUES:+ return True+ if value in _LLAMA_ARG_FALSE_VALUES:+ return False+ return None+++def _template_preserves_thinking_by_default(template: str, context: Mapping) -> bool:+ """Whether ``template`` replays an earlier turn's reasoning with preserve_thinking+ omitted (Qwen3.8's own does, ``|default(false)`` does not); False if unrenderable."""+ from jinja2.sandbox import ImmutableSandboxedEnvironment++ def _raise(message):+ raise ValueError(message)++ marker = "unsloth-preserve-thinking-probe"+ messages = [+ {"role": "user", "content": "q1"},+ {"role": "assistant", "content": "a1", "reasoning_content": marker},+ {"role": "user", "content": "q2"},+ ]+ try:+ jinja = ImmutableSandboxedEnvironment(+ trim_blocks = True,+ lstrip_blocks = True,+ extensions = ["jinja2.ext.loopcontrols", "jinja2.ext.do"],+ )+ jinja.globals.update(raise_exception = _raise, strftime_now = lambda fmt: time.strftime(fmt))+ rendered = jinja.from_string(template).render(+ **{**context, "messages": messages, "add_generation_prompt": True}+ )+ except Exception:+ return False+ return marker in rendered++def _reasoning_effort_from_env(env: Mapping[str, str]) -> Optional[str]:value = env.get("LLAMA_ARG_REASONING_EFFORT")return None if value == "default" else value@@ -32465,7 +32516,19 @@ class LlamaCppBackend:# Qwen3.8 defaults it on; Qwen3.6, Gemma 4, and every other# supporting family keep the existing off default. The# frontend receives the same resolved value via load/status.+ # An override takes the default its own text renders, and an explicit+ # --[no-]reasoning-preserve beats both: llama.cpp applies this kwarg+ # after that flag, so it would otherwise undo it (#15115).if self._supports_preserve_thinking:+ if chat_template_override:+ self._preserve_thinking_default = (+ _template_preserves_thinking_by_default(+ chat_template_override, reasoning_kw+ )+ )+ _preserve = _reasoning_preserve_from_args_or_env(extra_args, env)+ if _preserve is not None:+ self._preserve_thinking_default = _preservereasoning_kw["preserve_thinking"] = self._preserve_thinking_default_reasoning_args = _build_launch_reasoning_args(server_caps, reasoning_kw)cmd.extend(_reasoning_args)diff --git a/studio/backend/tests/test_launch_reasoning_flag.py b/studio/backend/tests/test_launch_reasoning_flag.pyindex 16508bb43ed..9258a67bdbc 100644--- a/studio/backend/tests/test_launch_reasoning_flag.py+++ b/studio/backend/tests/test_launch_reasoning_flag.py@@ -239,6 +239,8 @@ def _launched(template,model_identifier = "test",architecture = None,+ gguf_template = None,+ extra_args = None,):"""The backend and argv the real ``load_model`` builds on one host, for one vintage."""platform, is_wsl = OSES[os_label]@@ -252,6 +254,10 @@ def _launched(memory = [] if vendor == "cpu" else [(0, 24_000, 24_000)]backend, gguf = _backend(tmp_path, vulkan = False, memory = memory)backend._architecture = architecture+ if gguf_template is not None:+ backend._read_gguf_metadata = lambda _path: _embed_template(+ backend, gguf_template, model_identifier+ )monkeypatch.setattr(LlamaCppBackend,"probe_server_capabilities",@@ -263,10 +269,22 @@ def _launched(model_identifier = model_identifier,n_ctx = 4096,chat_template_override = template,+ extra_args = tuple(extra_args) if extra_args else None,)return backend, captured["cmd"]+def _embed_template(backend, template, model_identifier):+ """What the real header read leaves behind for a GGUF that embeds ``template``."""+ backend._chat_template = template+ flags = llama_cpp.detect_reasoning_flags(template, model_identifier)+ backend._supports_reasoning = flags["supports_reasoning"]+ backend._reasoning_style = flags["reasoning_style"]+ backend._reasoning_always_on = flags["reasoning_always_on"]+ backend._supports_preserve_thinking = flags["supports_preserve_thinking"]+ backend._preserve_thinking_default = flags["preserve_thinking_default"]++def _argv_for(tmp_path, monkeypatch, **kwargs):return _launched(tmp_path, monkeypatch, **kwargs)[1]@@ -275,6 +293,7 @@ def _argv_for(tmp_path, monkeypatch, **kwargs):def _no_inherited_reasoning(monkeypatch):monkeypatch.delenv("LLAMA_ARG_REASONING", raising = False)monkeypatch.delenv("LLAMA_ARG_REASONING_EFFORT", raising = False)+ monkeypatch.delenv("LLAMA_ARG_REASONING_PRESERVE", raising = False)def _reasoning_slice(cmd: list[str]) -> list[str]:@@ -468,6 +487,100 @@ class TestAnInheritedReasoningModeIsHonoured:assert backend.reasoning_default is thinks+QWEN38 = "unsloth/Qwen3.8-27B-GGUF"+# Replays history reasoning per its own preserve_thinking default, as Qwen3.8's template does.+_HISTORY_TEMPLATE = (+ "{% if enable_thinking %}<think>{% endif %}{% for m in messages %}"+ "{% if PRESERVE %}{{ m.reasoning_content }}{% endif %}{{ m.content }}{% endfor %}"+)+KEEPS_TEMPLATE = _HISTORY_TEMPLATE.replace(+ "PRESERVE", "preserve_thinking is undefined or preserve_thinking is true"+)+DROPS_TEMPLATE = _HISTORY_TEMPLATE.replace("PRESERVE", "preserve_thinking|default(false)")+++class TestThePreserveThinkingLaunchDefault:+ """The launch-time preserve_thinking kwarg beats --[no-]reasoning-preserve inside+ llama-server, so the kwarg itself must follow the user's flag and template (#15115)."""++ def _preserve(self, tmp_path, monkeypatch, **kwargs):+ kwargs.setdefault("model_identifier", QWEN38)+ backend, cmd = _launched(+ tmp_path, monkeypatch, os_label = "linux", vendor = "nvidia", modern = True, **kwargs+ )+ kwargs_json = json.loads(_reasoning_slice(cmd)[3])+ assert kwargs_json["preserve_thinking"] is backend.preserve_thinking_default+ return backend.preserve_thinking_default++ def test_qwen38_keeps_its_on_default_with_its_own_template(self, tmp_path, monkeypatch):+ assert self._preserve(tmp_path, monkeypatch, template = None, gguf_template = THINKING_TEMPLATE)++ def test_an_override_defaulting_off_turns_the_qwen38_default_off(self, tmp_path, monkeypatch):+ assert not self._preserve(tmp_path, monkeypatch, template = DROPS_TEMPLATE)++ @pytest.mark.parametrize("model_identifier", [QWEN38, "test"])+ def test_an_override_defaulting_on_keeps_it_on(self, tmp_path, monkeypatch, model_identifier):+ assert self._preserve(+ tmp_path, monkeypatch, template = KEEPS_TEMPLATE, model_identifier = model_identifier+ )++ def test_an_unrenderable_override_gets_the_off_default(self, tmp_path, monkeypatch):+ assert not self._preserve(+ tmp_path, monkeypatch, template = THINKING_TEMPLATE + "{{ raise_exception('x') }}"+ )++ @pytest.mark.parametrize("flag", ["--no-reasoning-preserve", "--no_reasoning_preserve"])+ def test_no_reasoning_preserve_turns_the_qwen38_default_off(self, tmp_path, monkeypatch, flag):+ assert not self._preserve(+ tmp_path,+ monkeypatch,+ template = None,+ gguf_template = THINKING_TEMPLATE,+ extra_args = [flag],+ )++ def test_reasoning_preserve_turns_it_on_for_an_override(self, tmp_path, monkeypatch):+ assert self._preserve(+ tmp_path,+ monkeypatch,+ template = THINKING_TEMPLATE,+ model_identifier = "test",+ extra_args = ["--reasoning-preserve"],+ )++ def test_the_last_flag_wins(self, tmp_path, monkeypatch):+ assert not self._preserve(+ tmp_path,+ monkeypatch,+ template = None,+ gguf_template = THINKING_TEMPLATE,+ extra_args = ["--reasoning-preserve", "--no-reasoning-preserve"],+ )++ @pytest.mark.parametrize("value,expected", [("0", False), ("off", False), ("1", True)])+ def test_the_inherited_env_is_honoured(self, tmp_path, monkeypatch, value, expected):+ monkeypatch.setenv("LLAMA_ARG_REASONING_PRESERVE", value)+ assert (+ self._preserve(+ tmp_path,+ monkeypatch,+ template = THINKING_TEMPLATE,+ model_identifier = "test",+ )+ is expected+ )++ def test_the_extras_beat_the_env(self, tmp_path, monkeypatch):+ monkeypatch.setenv("LLAMA_ARG_REASONING_PRESERVE", "0")+ assert self._preserve(+ tmp_path,+ monkeypatch,+ template = None,+ gguf_template = THINKING_TEMPLATE,+ extra_args = ["--reasoning-preserve"],+ )++class TestInheritedExtrasTreatBothSpellingsAlike:"""An inherited template override is appended last and would win, so both spellings must be stripped."""