Skip to content

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.

Sample Qwen3 models for the turn's thinking mode in unsloth chat (#14388)

Nilayb36868652037
diff --git a/unsloth_cli/_inference.py b/unsloth_cli/_inference.py
index 07a80304b30..13065d81bfa 100644
--- a/unsloth_cli/_inference.py
+++ b/unsloth_cli/_inference.py
@@ -430,6 +430,38 @@ class ChatBackend:
model_id = getattr(
self._backend, "model_identifier" if self._kind == "gguf" else "active_model_name", None
)
+ if self._kind == "gguf":
+ from core.inference.llama_cpp import _extra_args_set_any_flag
+
+ # These pass-throughs render a template the flags were not read from.
+ supports_reasoning = getattr(
+ self._backend, "supports_reasoning", False
+ ) and not _extra_args_set_any_flag(
+ getattr(self._backend, "extra_args", None),
+ {"--chat-template", "--chat-template-file", "--no-jinja"},
+ )
+ style = getattr(self._backend, "reasoning_style", None)
+ always_on = getattr(self._backend, "reasoning_always_on", False)
+ else:
+ from core.inference.chat_template_helpers import _selected_template_strings_from_value
+ from core.inference.llama_cpp import detect_reasoning_flags
+
+ info = (getattr(self._backend, "models", {}).get(model_id) or {}).get(
+ "chat_template_info"
+ ) or {}
+ selected = _selected_template_strings_from_value(info.get("template"))
+ flags = detect_reasoning_flags(
+ selected[0] if selected else None, model_id, log_level = "debug"
+ )
+ supports_reasoning = flags["supports_reasoning"]
+ style = flags["reasoning_style"]
+ always_on = flags["reasoning_always_on"]
+ # An effort-only template has no off switch: thinking off still renders a low effort.
+ always_on = always_on or style == "reasoning_effort"
+ thinking = (always_on or enable_thinking) if supports_reasoning else None
+ # Instruct-2507 never reasons (its template still parses <think>); -GGUF ids miss its defaults.
+ if re.search(r"qwen3[-_].*instruct[-_]2507", (model_id or "").lower()):
+ thinking = False
sampling = resolve_effective_sampling(
model_id,
dict(
@@ -438,6 +470,7 @@ class ChatBackend:
top_k = top_k,
repetition_penalty = repetition_penalty,
),
+ thinking = thinking,
)
if self._kind == "gguf":
# llama-server takes the system prompt as the first message.
diff --git a/unsloth_cli/tests/test_inference_chat.py b/unsloth_cli/tests/test_inference_chat.py
index 97743471993..2c98d465150 100644
--- a/unsloth_cli/tests/test_inference_chat.py
+++ b/unsloth_cli/tests/test_inference_chat.py
@@ -378,6 +378,189 @@ def test_chatbackend_gguf_resolves_unset_sampling_but_keeps_explicit_values(monk
}
+_QWEN_THINKING_ON = {"temperature": 0.6, "top_p": 0.95, "top_k": 20, "min_p": 0.0}
+_QWEN_THINKING_OFF = {"temperature": 0.7, "top_p": 0.8, "top_k": 20, "min_p": 0.0}
+_QWEN3_TEMPLATE = (
+ "{%- if enable_thinking is defined and enable_thinking is false %}"
+ "<think>\n\n</think>\n\n{%- endif %}"
+)
+_ALWAYS_THINKING_TEMPLATE = "{{- '<|im_start|>assistant\n<think>\n' }}{# </think> #}"
+
+
+def _qwen_backend(
+ kind,
+ model_id,
+ template = None,
+ **flags,
+):
+ if kind == "gguf":
+ fake = _FakeGgufBackend()
+ fake.model_identifier = model_id
+ for name, value in flags.items():
+ setattr(fake, name, value)
+ return fake
+ fake = _FakeBackend()
+ fake.active_model_name = model_id
+ fake.models = {model_id: {"chat_template_info": {"template": template}}}
+ return fake
+
+
+def _qwen_sampling(monkeypatch, kind, fake, enable_thinking, **explicit):
+ _clear_sampling_pins(monkeypatch)
+ list(
+ ChatBackend(kind, fake).stream(
+ [{"role": "user", "content": "x"}],
+ **{**_STREAM_KWARGS, **_UNSET_SAMPLING, **explicit, "enable_thinking": enable_thinking},
+ )
+ )
+ call = fake.calls[0] if kind == "gguf" else fake.calls[0][2]
+ return {field: call[field] for field in _QWEN_THINKING_ON}
+
+
+_QWEN_REASONING_BACKENDS = [
+ ("gguf", "unsloth/Qwen3-4B-GGUF", "unsloth/Qwen3.5-4B-GGUF", {"supports_reasoning": True}),
+ ("unsloth", "unsloth/Qwen3-4B", "unsloth/Qwen3.5-4B", {"template": _QWEN3_TEMPLATE}),
+]
+
+
[email protected]("kind, qwen3, qwen35, flags", _QWEN_REASONING_BACKENDS)
+ "family, enable_thinking, expected",
+ [
+ ("qwen3", False, _QWEN_THINKING_OFF),
+ ("qwen3", True, _QWEN_THINKING_ON),
+ ("qwen35", True, _QWEN_THINKING_ON),
+ ("qwen35", False, _QWEN_THINKING_OFF),
+ ],
+)
+def test_chatbackend_samples_qwen3_for_the_thinking_mode_of_the_turn(
+ monkeypatch, kind, qwen3, qwen35, flags, family, enable_thinking, expected
+):
+ fake = _qwen_backend(kind, {"qwen3": qwen3, "qwen35": qwen35}[family], **flags)
+ assert _qwen_sampling(monkeypatch, kind, fake, enable_thinking) == expected
+
+
[email protected]("kind, qwen3, _qwen35, flags", _QWEN_REASONING_BACKENDS)
+def test_chatbackend_explicit_temperature_keeps_the_rest_of_the_thinking_table(
+ monkeypatch, kind, qwen3, _qwen35, flags
+):
+ fake = _qwen_backend(kind, qwen3, **flags)
+ sampling = _qwen_sampling(monkeypatch, kind, fake, False, temperature = 0.3)
+ assert sampling == {**_QWEN_THINKING_OFF, "temperature": 0.3}
+
+
+ "kind, model_id, flags",
+ [
+ (
+ "gguf",
+ "unsloth/Qwen3.5-4B-GGUF",
+ {"supports_reasoning": True, "reasoning_always_on": True},
+ ),
+ ("unsloth", "unsloth/Qwen3.5-4B", {"template": _ALWAYS_THINKING_TEMPLATE}),
+ ],
+)
+def test_chatbackend_always_on_reasoning_samples_as_thinking(monkeypatch, kind, model_id, flags):
+ fake = _qwen_backend(kind, model_id, **flags)
+ assert _qwen_sampling(monkeypatch, kind, fake, False) == _QWEN_THINKING_ON
+
+
+ "kind, model_id, flags",
+ [
+ (
+ "gguf",
+ "unsloth/Qwen3.5-4B-GGUF",
+ {"supports_reasoning": True, "reasoning_style": "reasoning_effort"},
+ ),
+ (
+ "unsloth",
+ "unsloth/Qwen3.5-4B",
+ {
+ "template": "{%- if reasoning_effort == 'low' %}low{%- elif reasoning_effort == 'high' %}high{%- endif %}"
+ },
+ ),
+ ],
+)
+def test_chatbackend_effort_only_reasoning_samples_as_thinking(monkeypatch, kind, model_id, flags):
+ fake = _qwen_backend(kind, model_id, **flags)
+ assert _qwen_sampling(monkeypatch, kind, fake, False) == _QWEN_THINKING_ON
+
+
[email protected]("enable_thinking", [False, True])
+ "kind, model_id, flags",
+ [
+ (
+ "gguf",
+ "unsloth/Qwen3-4B-Instruct-2507-GGUF",
+ {"supports_reasoning": True, "reasoning_always_on": True},
+ ),
+ ("unsloth", "unsloth/Qwen3-4B-Instruct-2507", {"template": _ALWAYS_THINKING_TEMPLATE}),
+ ],
+)
+def test_chatbackend_instruct_2507_samples_as_non_thinking(
+ monkeypatch, kind, model_id, flags, enable_thinking
+):
+ fake = _qwen_backend(kind, model_id, **flags)
+ assert _qwen_sampling(monkeypatch, kind, fake, enable_thinking) == _QWEN_THINKING_OFF
+
+
+ "kind, model_id", [("gguf", "unsloth/Qwen3.5-4B-GGUF"), ("unsloth", "unsloth/Qwen3.5-4B")]
+)
+def test_chatbackend_without_reasoning_keeps_the_model_recommendation(monkeypatch, kind, model_id):
+ fake = _qwen_backend(kind, model_id, template = "{{ messages }}")
+ assert _qwen_sampling(monkeypatch, kind, fake, True) == _QWEN_THINKING_OFF
+
+
+ "template",
+ [
+ {"default": _QWEN3_TEMPLATE, "tool_use": "{{ messages }}"},
+ [{"name": "default", "template": _QWEN3_TEMPLATE}],
+ ],
+)
+def test_chatbackend_reads_reasoning_from_the_selected_named_template(monkeypatch, template):
+ fake = _qwen_backend("unsloth", "unsloth/Qwen3.5-4B", template = template)
+ assert _qwen_sampling(monkeypatch, "unsloth", fake, True) == _QWEN_THINKING_ON
+
+
+ "extra_args",
+ [
+ ["--chat-template", "chatml"],
+ ["--chat-template-file=/tmp/custom.jinja"],
+ ["--chat_template", "chatml"],
+ ["--chat_template_file", "/tmp/custom.jinja"],
+ ["--no-jinja"],
+ ],
+)
+def test_chatbackend_gguf_template_override_keeps_the_model_recommendation(monkeypatch, extra_args):
+ fake = _qwen_backend(
+ "gguf", "unsloth/Qwen3.5-4B-GGUF", supports_reasoning = True, extra_args = extra_args
+ )
+ assert _qwen_sampling(monkeypatch, "gguf", fake, True) == _QWEN_THINKING_OFF
+
+
+def test_chat_think_toggle_switches_the_sampling_table(monkeypatch):
+ _clear_sampling_pins(monkeypatch)
+ fake = _qwen_backend("gguf", "unsloth/Qwen3-4B-GGUF", supports_reasoning = True)
+ monkeypatch.setattr(chatmod, "resolve_model_config", lambda *a, **k: _FakeConfig())
+ monkeypatch.setattr(chatmod, "_compare_needs_second_model", lambda: False)
+ monkeypatch.setattr(chatmod, "load_chat_backend", lambda *a, **k: ChatBackend("gguf", fake))
+
+ result = CliRunner().invoke(
+ _chat_app(), ["fake-model", "--no-server"], input = "q1\n/think\nq2\n/exit\n"
+ )
+
+ assert result.exit_code == 0, result.output
+ assert [{field: call[field] for field in _QWEN_THINKING_ON} for call in fake.calls] == [
+ _QWEN_THINKING_OFF,
+ _QWEN_THINKING_ON,
+ ]
+
+
class _FakeStatsBackend:
def __init__(self, stats):
self._stats = stats