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
fix(studio): support preserve thinking for llama.cpp connections (#11706)
Lee Jackson4fabb25e07dc
diff --git a/studio/backend/core/inference/external_provider.py b/studio/backend/core/inference/external_provider.pyindex 7a078ba3688..9a5c3cdd706 100644--- a/studio/backend/core/inference/external_provider.py+++ b/studio/backend/core/inference/external_provider.py@@ -1465,6 +1465,7 @@ class ExternalProviderClient:continue_final_message: Optional[bool] = None,response_format: Optional[dict[str, Any]] = None,stream: bool = True,+ preserve_thinking: Optional[bool] = None,) -> AsyncGenerator[str, None]:"""Yield OpenAI-format SSE lines from the external provider. OpenAI-compatible providersforward lines verbatim; for Anthropic the native Messages API SSE is translated.@@ -1634,6 +1635,9 @@ class ExternalProviderClient:for field in provider_info.get("body_omit", ()):body.pop(field, None)+ if self.provider_type == "llama_cpp" and preserve_thinking is not None:+ body["chat_template_kwargs"] = {"preserve_thinking": preserve_thinking}+# Kimi thinking is a top-level body field. kimi-k2-thinking is always on (ignore the toggle); kimi-k2.6# defaults on, can be disabled. `keep: all` preserves every chunk for the UI panel.if self.provider_type == "kimi" and enable_thinking is not None:diff --git a/studio/backend/core/inference/external_tool_transport.py b/studio/backend/core/inference/external_tool_transport.pyindex 5a815178577..6817e52bdf6 100644--- a/studio/backend/core/inference/external_tool_transport.py+++ b/studio/backend/core/inference/external_tool_transport.py@@ -62,6 +62,9 @@ class OAICompatTransport:self._model = modelself._continue_final_message = continue_final_messageself._request_kwargs = request_kwargs+ self.preserves_reasoning = (+ client.provider_type == "llama_cpp" and request_kwargs.get("preserve_thinking") is True+ )def stream(self,diff --git a/studio/backend/core/inference/studio_tool_loop.py b/studio/backend/core/inference/studio_tool_loop.pyindex fd6faf43304..e6e6df11dde 100644--- a/studio/backend/core/inference/studio_tool_loop.py+++ b/studio/backend/core/inference/studio_tool_loop.py@@ -548,6 +548,7 @@ class _Turn:round: int = 0healed: list[dict[str, Any]] = field(default_factory = list)text: list[str] = field(default_factory = list)+ reasoning: list[str] = field(default_factory = list)reasoning_extra: dict[str, Any] | None = Nonefinish_reason: str | None = None# Results from tools the PROVIDER ran this turn, keyed by call id so a repeated end event cannot record the same@@ -1363,6 +1364,9 @@ async def stream_with_studio_tools(delta = choice.get("delta")delta = delta if isinstance(delta, dict) else {}+ reasoning = delta.get("reasoning_content")+ if getattr(transport, "preserves_reasoning", False) and isinstance(reasoning, str):+ turn.reasoning.append(reasoning)content = delta.get("content")raw_calls = delta.get("tool_calls")extra = delta.get("extra_content")@@ -1871,6 +1875,8 @@ async def stream_with_studio_tools(if assistant_message["content"]else hosted_text)+ if turn.reasoning:+ assistant_message["reasoning_content"] = "".join(turn.reasoning)if turn.reasoning_extra:assistant_message["extra_content"] = turn.reasoning_extraif assistant_tool_calls:diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.pyindex dee15877cfb..2f24fec742e 100644--- a/studio/backend/routes/inference.py+++ b/studio/backend/routes/inference.py@@ -22733,6 +22733,11 @@ def _build_external_messages(result = []for msg in messages:+ reasoning = (+ {"reasoning_content": msg.reasoning_content}+ if provider_type == "llama_cpp" and msg.role == "assistant" and msg.reasoning_content+ else {}+ )# Drop role=tool messages whose matching server-builtin tool_call was# filtered above. An orphan tool_result with no matching tool_call is# rejected by OpenAI Responses and Anthropic.@@ -22742,19 +22747,24 @@ def _build_external_messages(and msg.tool_call_id in dropped_server_builtin_tool_call_ids):continue- if isinstance(msg.content, str):+ if isinstance(msg.content, str) or (msg.content is None and reasoning):# Drop bare assistant messages with no content AND no tool_calls# (some providers reject empty assistant turns). Preserve assistant# turns whose only payload is tool_calls so multi-turn# function-call loops round-trip.- if msg.role == "assistant" and not msg.content.strip() and not msg.tool_calls:+ if (+ msg.role == "assistant"+ and not (msg.content or "").strip()+ and not msg.tool_calls+ and not reasoning+ ):continue- out: dict[str, Any] = {"role": msg.role, "content": msg.content}+ out: dict[str, Any] = {"role": msg.role, "content": msg.content or "", **reasoning}if msg.role == "assistant" and msg.tool_calls:_tcs = _filter_tool_calls(msg.tool_calls)if _tcs:out["tool_calls"] = _tcs- elif not msg.content.strip():+ elif not (msg.content or "").strip() and not reasoning:# Every tool_call was a dropped synthetic provider card;# the turn would be an empty# `{"role":"assistant","content":""}` that some providers@@ -22829,17 +22839,17 @@ def _build_external_messages(# `compaction` block; every other provider would 400 on# the unknown part, so gate by provider_type.parts.append({"type": "compaction", "content": part.content})- entry: dict[str, Any] = {"role": msg.role, "content": parts}+ entry: dict[str, Any] = {"role": msg.role, "content": parts, **reasoning}if msg.role == "assistant" and msg.tool_calls:_tcs = _filter_tool_calls(msg.tool_calls)if _tcs:entry["tool_calls"] = _tcs- elif not parts:+ elif not parts and not reasoning:# All tool_calls were synthetic and dropped, and no# content parts survived. Skip rather than forward an# empty assistant turn that downstream providers reject.continue- elif msg.role == "assistant" and not parts:+ elif msg.role == "assistant" and not parts and not reasoning:continueif msg.role == "tool":if msg.tool_call_id:@@ -22866,14 +22876,14 @@ def _build_external_messages(preserved.append(_rp)elif p.type == "compaction" and anthropic:preserved.append({"type": "compaction", "content": p.content})- if msg.role == "assistant" and not preserved:+ if msg.role == "assistant" and not preserved and not reasoning:continueif len(preserved) == 1 and preserved[0]["type"] == "text":# Single text part collapses to a string for providers that# don't accept content arrays.- entry = {"role": msg.role, "content": preserved[0]["text"]}+ entry = {"role": msg.role, "content": preserved[0]["text"], **reasoning}else:- entry = {"role": msg.role, "content": preserved}+ entry = {"role": msg.role, "content": preserved, **reasoning}if msg.role == "assistant" and msg.tool_calls:_tcs = _filter_tool_calls(msg.tool_calls)if _tcs:@@ -22885,7 +22895,7 @@ def _build_external_messages(_has_text = (isinstance(_entry_content, str) and _entry_content.strip()) or (isinstance(_entry_content, list) and len(_entry_content) > 0)- if not _has_text:+ if not _has_text and not reasoning:continueif msg.role == "tool":if msg.tool_call_id:@@ -23753,6 +23763,7 @@ async def _proxy_to_external_provider(repetition_penalty = _repetition_penalty_explicit,enable_thinking = payload.enable_thinking,reasoning_effort = payload.reasoning_effort,+ preserve_thinking = payload.preserve_thinking,enable_prompt_caching = payload.enable_prompt_caching,openai_code_exec_container_id = payload.openai_code_exec_container_id,anthropic_code_exec_container_id = payload.anthropic_code_exec_container_id,diff --git a/studio/backend/tests/test_external_confirm_gate_and_saved_keys.py b/studio/backend/tests/test_external_confirm_gate_and_saved_keys.pyindex afeeab3c622..ea369029c0a 100644--- a/studio/backend/tests/test_external_confirm_gate_and_saved_keys.py+++ b/studio/backend/tests/test_external_confirm_gate_and_saved_keys.py@@ -34,6 +34,7 @@ class FakeExternalClient:def __init__(self, **kwargs):FakeExternalClient.last = {"ctor": kwargs, "passthrough": None}+ self.provider_type = kwargs.get("provider_type")def stream_chat_completion(self, **kwargs):FakeExternalClient.last["passthrough"] = kwargs@@ -404,6 +405,8 @@ def test_transport_cancellation_is_wired_through_the_loop():from core.inference.external_tool_transport import OAICompatTransportclass _Stalling:+ provider_type = "custom"+def __init__(self):self.torn_down = Falseself.released = asyncio.Event()diff --git a/studio/backend/tests/test_external_hosted_tool_passthrough.py b/studio/backend/tests/test_external_hosted_tool_passthrough.pyindex 36d8f9ae1c7..3a105a096d0 100644--- a/studio/backend/tests/test_external_hosted_tool_passthrough.py+++ b/studio/backend/tests/test_external_hosted_tool_passthrough.py@@ -49,6 +49,7 @@ class FakeExternalClient:def __init__(self, **kwargs):FakeExternalClient.last = {"ctor": kwargs, "passthrough": None}+ self.provider_type = kwargs.get("provider_type")def stream_chat_completion(self, **kwargs):FakeExternalClient.last["passthrough"] = kwargsdiff --git a/studio/backend/tests/test_external_hosted_tool_selection.py b/studio/backend/tests/test_external_hosted_tool_selection.pyindex 3c483c178fd..c262d9b479f 100644--- a/studio/backend/tests/test_external_hosted_tool_selection.py+++ b/studio/backend/tests/test_external_hosted_tool_selection.py@@ -34,6 +34,7 @@ class _FakeExternalClient:def __init__(self, **kwargs):_FakeExternalClient.last = {"ctor": kwargs}+ self.provider_type = kwargs.get("provider_type")def stream_chat_completion(self, **kwargs):async def gen():diff --git a/studio/backend/tests/test_external_preserve_thinking.py b/studio/backend/tests/test_external_preserve_thinking.pynew file mode 100644index 00000000000..978b8268874--- /dev/null+++ b/studio/backend/tests/test_external_preserve_thinking.py@@ -0,0 +1,65 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++import pytest++from models.inference import ChatMessage+from routes.inference import _build_external_messages+from core.inference.external_provider import ExternalProviderClient+from core.inference.external_tool_transport import OAICompatTransport++[email protected]("content", ["answer", "", None, [], [{"type": "text", "text": "answer"}]])[email protected]("vision", [True, False])[email protected]("with_tool", [True, False])+def test_llama_history_keeps_reasoning_and_tool_calls(content, vision, with_tool):+ calls = [+ {"id": "call_1", "type": "function", "function": {"name": "lookup", "arguments": "{}"}}+ ]+ message = ChatMessage(+ role = "assistant",+ content = content,+ reasoning_content = "prior thought",+ tool_calls = calls if with_tool else None,+ )+ result = _build_external_messages([message], vision, provider_type = "llama_cpp")+ assert len(result) == 1+ assert result[0]["reasoning_content"] == "prior thought"+ if with_tool:+ assert result[0]["tool_calls"][0]["function"]["name"] == "lookup"++[email protected]("content", ["", None, []])[email protected]("vision", [True, False])+def test_llama_reasoning_survives_dropped_server_tool_cards(content, vision):+ card = {+ "id": "srv_1",+ "type": "function",+ "function": {"name": "web_search", "arguments": '{"_server_tool": true}'},+ }+ message = ChatMessage(+ role = "assistant", content = content, reasoning_content = "prior thought", tool_calls = [card]+ )+ result = _build_external_messages([message], vision, provider_type = "llama_cpp")+ assert len(result) == 1+ assert result[0]["reasoning_content"] == "prior thought"+ assert "tool_calls" not in result[0]+++ "provider", ["custom", "openai", "vllm", "ollama", "anthropic", "openrouter"]+)+def test_other_providers_keep_existing_reasoning_policy(provider):+ message = ChatMessage(role = "assistant", content = "answer", reasoning_content = "prior thought")+ result = _build_external_messages([message], False, provider_type = provider)+ assert "reasoning_content" not in result[0]++[email protected]("provider", ["llama_cpp", "custom", "vllm"])[email protected]("value", [True, False, None])+def test_tool_transport_reasoning_policy(provider, value):+ client = ExternalProviderClient(+ provider_type = provider, base_url = "http://localhost:8080/v1", api_key = ""+ )+ transport = OAICompatTransport(client, model = "test", preserve_thinking = value)+ assert transport.preserves_reasoning is (provider == "llama_cpp" and value is True)diff --git a/studio/backend/tests/test_external_provider_sampling_forwarding.py b/studio/backend/tests/test_external_provider_sampling_forwarding.pyindex 31ea9af8f2b..b7753b9c884 100644--- a/studio/backend/tests/test_external_provider_sampling_forwarding.py+++ b/studio/backend/tests/test_external_provider_sampling_forwarding.py@@ -164,3 +164,23 @@ def test_the_registry_strips_the_three_for_providers_that_cannot_take_them(proviassert "repetition_penalty" not in bodyassert body["temperature"] == 0.31assert body["presence_penalty"] == 0.11++[email protected]("value", [True, False, None])+def test_llama_preserve_thinking_template_option(value):+ body = _capture_body("llama_cpp", preserve_thinking = value)+ if value is None:+ assert "chat_template_kwargs" not in body+ else:+ assert body["chat_template_kwargs"] == {"preserve_thinking": value}+++def test_llama_preserve_thinking_keeps_enable_thinking():+ body = _capture_body("llama_cpp", preserve_thinking = True, enable_thinking = False)+ assert body["chat_template_kwargs"] == {"preserve_thinking": True, "enable_thinking": False}++[email protected]("provider", ["custom", "vllm", "ollama", "openrouter"])+def test_preserve_thinking_does_not_leak_to_other_providers(provider):+ body = _capture_body(provider, preserve_thinking = True)+ assert "preserve_thinking" not in body.get("chat_template_kwargs", {})diff --git a/studio/backend/tests/test_external_provider_sampling_over_the_wire.py b/studio/backend/tests/test_external_provider_sampling_over_the_wire.pyindex 211ecc78b62..d4572ec1f31 100644--- a/studio/backend/tests/test_external_provider_sampling_over_the_wire.py+++ b/studio/backend/tests/test_external_provider_sampling_over_the_wire.py@@ -152,7 +152,13 @@ def _client_capture(provider_type: str, **kwargs) -> dict:return server.sampling()-def _route_capture(**payload_fields) -> dict:+def _route_capture(+ *,+ provider_type = "vllm",+ messages = None,+ full_body = False,+ **payload_fields,+) -> dict:"""POST through `_proxy_to_external_provider`, not around it."""import routes.inference as rifrom starlette.requests import Request@@ -162,9 +168,9 @@ def _route_capture(**payload_fields) -> dict:with _Server() as server:payload = ChatCompletionRequest(- provider_type = "vllm",+ provider_type = provider_type,provider_base_url = server.base_url,- messages = [{"role": "user", "content": "hi"}],+ messages = messages or [{"role": "user", "content": "hi"}],model = "a-model",stream = True,**payload_fields,@@ -193,7 +199,7 @@ def _route_capture(**payload_fields) -> dict:_run(go)assert server.bodies, "the route never reached the provider"- return server.sampling()+ return server.bodies[-1] if full_body else server.sampling()def test_a_request_that_never_mentioned_them_forwards_nothing():@@ -366,3 +372,20 @@ def test_a_stale_frontend_bundle_does_not_start_400ing_a_custom_gateway():_run(go)assert server.sampling() == {}, "custom leaked an extension the gateway rejects"assert "Unrecognized request argument" not in "".join(lines)++[email protected]("value", [True, False, None])+def test_preserve_thinking_reaches_llama_server_through_the_route(value):+ messages = [+ {"role": "user", "content": "one"},+ {"role": "assistant", "content": "answer", "reasoning_content": "prior thought"},+ {"role": "user", "content": "two"},+ ]+ body = _route_capture(+ provider_type = "llama_cpp", messages = messages, full_body = True, preserve_thinking = value+ )+ assert body["messages"][-2]["reasoning_content"] == "prior thought"+ if value is None:+ assert "chat_template_kwargs" not in body+ else:+ assert body["chat_template_kwargs"] == {"preserve_thinking": value}diff --git a/studio/backend/tests/test_external_tool_transport_cancel.py b/studio/backend/tests/test_external_tool_transport_cancel.pyindex 7776e28e357..967d0e8f9da 100644--- a/studio/backend/tests/test_external_tool_transport_cancel.py+++ b/studio/backend/tests/test_external_tool_transport_cancel.py@@ -18,6 +18,8 @@ from core.inference.external_tool_transport import OAICompatTransportclass _StallingClient:"""One chunk, then silence: an upstream mid-answer or still in prefill."""+ provider_type = "custom"+def __init__(self) -> None:self.torn_down = Falseself.released = asyncio.Event()diff --git a/studio/backend/tests/test_studio_tool_loop.py b/studio/backend/tests/test_studio_tool_loop.pyindex 4fa391ea649..014d664d1d7 100644--- a/studio/backend/tests/test_studio_tool_loop.py+++ b/studio/backend/tests/test_studio_tool_loop.py@@ -1337,3 +1337,39 @@ def test_mcp_images_are_not_sent_to_a_text_only_provider(mcp_image_result):follow_up = transport.requests[1]["messages"]assert follow_up[-1]["role"] == "tool"assert "__MCP_IMAGES__" not in follow_up[-1]["content"]++[email protected]("preserve", [True, False])+def test_reasoning_replay_during_tool_rounds_is_opt_in(executed, preserve):+ transport = FakeTransport(+ [+ [+ _sse({"reasoning_content": "First "}),+ _sse({"reasoning_content": "thought."}),+ _sse(+ {+ "tool_calls": [+ {+ "index": 0,+ "id": "call_1",+ "type": "function",+ "function": {+ "name": "web_search",+ "arguments": '{"query":"unsloth"}',+ },+ }+ ]+ }+ ),+ _sse(finish = "tool_calls"),+ _DONE,+ ],+ [_sse({"content": "Done."}), _sse(finish = "stop"), _DONE],+ ]+ )+ transport.preserves_reasoning = preserve+ _run(transport)+ assistant = transport.requests[1]["messages"][-2]+ assert assistant.get("reasoning_content") == ("First thought." if preserve else None)+ assert assistant["tool_calls"][0]["function"]["name"] == "web_search"+ assert transport.requests[1]["messages"][-1]["role"] == "tool"diff --git a/studio/frontend/src/components/assistant-ui/thread.tsx b/studio/frontend/src/components/assistant-ui/thread.tsxindex 86c069a553b..27d1b337479 100644--- a/studio/frontend/src/components/assistant-ui/thread.tsx+++ b/studio/frontend/src/components/assistant-ui/thread.tsx@@ -5805,7 +5805,8 @@ const ReasoningToggle: FC<{ side?: "top" | "bottom" }> = ({: reasoningEffort;const effectiveReasoningVisualEnabled =effectiveReasoningEnabled && displayedEffort !== "none";- const disabled = !(modelLoaded && effectiveSupportsReasoning);+ const disabled =+ !modelLoaded || !(effectiveSupportsReasoning || supportsPreserveThinking);const formatEffortLabel = (level: typeof reasoningEffort): string => {if (level !== "xhigh")return level.charAt(0).toUpperCase() + level.slice(1);@@ -5820,8 +5821,8 @@ const ReasoningToggle: FC<{ side?: "top" | "bottom" }> = ({};const effortLabel = formatEffortLabel(displayedEffort);- // Only rendered for models that can reason.- if (!effectiveSupportsReasoning) {+ // A connection may support history preservation without a generation toggle.+ if (!effectiveSupportsReasoning && !supportsPreserveThinking) {return null;}@@ -5832,9 +5833,11 @@ const ReasoningToggle: FC<{ side?: "top" | "bottom" }> = ({effectiveReasoningStyle === "enable_thinking_effort";// Dropdown when there are effort levels or preserve-thinking; else a toggle.const useDropdown = isEffort || supportsPreserveThinking;- const activeLook = isEffort- ? reasoningLockedOn || (effectiveReasoningVisualEnabled && !disabled)- : reasoningLockedOn || (effectiveReasoningEnabled && !disabled);+ const activeLook = !effectiveSupportsReasoning+ ? preserveThinking && !disabled+ : isEffort+ ? reasoningLockedOn || (effectiveReasoningVisualEnabled && !disabled)+ : reasoningLockedOn || (effectiveReasoningEnabled && !disabled);if (useDropdown) {return (@@ -5927,6 +5930,7 @@ const ReasoningToggle: FC<{ side?: "top" | "bottom" }> = ({))}</>) : (+ effectiveSupportsReasoning &&effectiveSupportsReasoningOff &&!reasoningLockedOn && (<DropdownMenuItem@@ -5960,8 +5964,8 @@ const ReasoningToggle: FC<{ side?: "top" | "bottom" }> = ({e.preventDefault();const next = !preserveThinking;setPreserveThinking(next);- // Preserve thinking requires thinking on.- if (next) {+ // Only local models couple this setting to generation controls.+ if (next && externalSelection === null) {setReasoningEnabled(true);applyQwenThinkingParams(true);}diff --git a/studio/frontend/src/features/chat/api/chat-adapter.ts b/studio/frontend/src/features/chat/api/chat-adapter.tsindex 43b988d7c46..43efec3ebd7 100644--- a/studio/frontend/src/features/chat/api/chat-adapter.ts+++ b/studio/frontend/src/features/chat/api/chat-adapter.ts@@ -186,6 +186,7 @@ import {getGroundedExternalMaxOutputTokens,getExternalMinOutputTokens,getExternalReasoningCapabilities,+ providerSupportsPreserveThinking,getProviderCapabilities,isGeminiCustomOpenAICompatBase,providerHostsCodeExecution,@@ -1502,8 +1503,8 @@ function isAbandonedAssistantTurn(): boolean {if (message.role !== "assistant") return false;if (assistantTurnCarriesPayload(message)) return false;- // A turn that finished on reasoning alone is a reply, even though external requests strip- // reasoning and serialise it empty.+ // A turn that finished on reasoning alone is a reply, even when the selected provider+ // omits reasoning and serialises it empty.if (!assistantTurnEndedEarly(message) &&(message.content ?? []).some((part) => part.type === "reasoning")@@ -4952,14 +4953,15 @@ export function createOpenAIStreamAdapter(readsImages: targetReadsImages,localMarkers: mcpImagesLocalMarkers,});- const survivingMessages = pruneOutboundHistory(- messages,- !isExternalRequest,+ const replayReasoning = !isExternalRequest || (+ providerSupportsPreserveThinking(externalProvider?.providerType) &&+ runtime.preserveThinking);+ const survivingMessages = pruneOutboundHistory(messages, replayReasoning);// toOpenAIMessages emits assistant tool_calls plus role="tool" follow-ups; the backend Gemini// translator rebuilds the functionCall/functionResponse parts.let outboundMessages = survivingMessages- .flatMap((message) => toOpenAIMessages(message, !isExternalRequest))+ .flatMap((message) => toOpenAIMessages(message, replayReasoning)).filter((message): message is NonNullable<typeof message> =>Boolean(message),);@@ -6226,6 +6228,9 @@ export function createOpenAIStreamAdapter(return {model: externalSelection.modelId,messages: outboundMessages,+ ...(providerSupportsPreserveThinking(externalProvider?.providerType)+ ? { preserve_thinking: runtime.preserveThinking }+ : {}),stream: true,stream_options: { include_usage: true },// Never forwarded upstream (the proxy sends an explicit field list); the trailing assistantdiff --git a/studio/frontend/src/features/chat/chat-page.tsx b/studio/frontend/src/features/chat/chat-page.tsxindex 445b13a6f2d..5ffe17fcb6b 100644--- a/studio/frontend/src/features/chat/chat-page.tsx+++ b/studio/frontend/src/features/chat/chat-page.tsx@@ -179,6 +179,7 @@ import {import {externalReasoningTakesEffort,getExternalReasoningCapabilities,+ providerSupportsPreserveThinking,getProviderCapabilities,modelCatalogVersion,providerHostsCodeExecution,@@ -222,6 +223,7 @@ import {reconcilePinnedReasoningEffort,takeEffortDisplacedByPin,threadScopedOverride,+ resolvePreserveThinkingOnLoad,useChatRuntimeStore,} from "./stores/chat-runtime-store";import { wantsDownloadManagerStaging } from "./utils/model-download-staging";@@ -2630,7 +2632,10 @@ export function ChatPage({: state.reasoningEnabled: true: state.reasoningEnabled,- supportsPreserveThinking: false,+ supportsPreserveThinking: providerSupportsPreserveThinking(provider?.providerType),+ preserveThinking: resolvePreserveThinkingOnLoad({+ supports_preserve_thinking: providerSupportsPreserveThinking(provider?.providerType),+ }),supportsTools: supportsStudioToolsHere,supportsBuiltinWebSearch,supportsBuiltinCodeExecution,@@ -3327,7 +3332,10 @@ export function ChatPage({: store.reasoningEnabled: true: store.reasoningEnabled,- supportsPreserveThinking: false,+ supportsPreserveThinking: providerSupportsPreserveThinking(selectedProvider?.providerType),+ preserveThinking: resolvePreserveThinkingOnLoad({+ supports_preserve_thinking: providerSupportsPreserveThinking(selectedProvider?.providerType),+ }),supportsTools: supportsStudioToolsHere,supportsBuiltinWebSearch,supportsBuiltinCodeExecution,diff --git a/studio/frontend/src/features/chat/provider-capabilities.ts b/studio/frontend/src/features/chat/provider-capabilities.tsindex a5f4f055426..79171e81608 100644--- a/studio/frontend/src/features/chat/provider-capabilities.ts+++ b/studio/frontend/src/features/chat/provider-capabilities.ts@@ -1387,3 +1387,10 @@ export function reasoningFieldsAfterCatalogRefresh(caps.supportsReasoning && !caps.supportsReasoningOff ? true : current.reasoningEnabled,};}++/** Sent as a llama-server template kwarg, so the model's template must still read it. */+export function providerSupportsPreserveThinking(+ providerType: string | null | undefined,+): boolean {+ return providerType === "llama_cpp";+}diff --git a/studio/frontend/src/features/chat/shared-composer.tsx b/studio/frontend/src/features/chat/shared-composer.tsxindex d914dcceaa2..708fd3e9eb0 100644--- a/studio/frontend/src/features/chat/shared-composer.tsx+++ b/studio/frontend/src/features/chat/shared-composer.tsx@@ -797,9 +797,12 @@ export function SharedComposer({: reasoningEffort;const effectiveReasoningVisualEnabled =effectiveReasoningEnabled && displayedEffort !== "none";- const reasoningDisabled = !modelLoaded || !effectiveSupportsReasoning;+ const reasoningDisabled =+ !modelLoaded || !(effectiveSupportsReasoning || supportsPreserveThinking);const showReasoningControl =- effectiveSupportsReasoning || effectiveReasoningAlwaysOn;+ effectiveSupportsReasoning ||+ effectiveReasoningAlwaysOn ||+ supportsPreserveThinking;// enable_thinking_effort (GLM-5.2: high|max + disable) reuses the effort dropdown; it just also// carries an Off row via supportsReasoningOff.const isEffort =@@ -810,9 +813,11 @@ export function SharedComposer({const narrowEffortMenu =effectiveReasoningStyle === "enable_thinking_effort" &&!supportsPreserveThinking;- const thinkingActiveLook = isEffort- ? reasoningLockedOn || (effectiveReasoningVisualEnabled && !reasoningDisabled)- : reasoningLockedOn || (effectiveReasoningEnabled && !reasoningDisabled);+ const thinkingActiveLook = !effectiveSupportsReasoning+ ? preserveThinking && !reasoningDisabled+ : isEffort+ ? reasoningLockedOn || (effectiveReasoningVisualEnabled && !reasoningDisabled)+ : reasoningLockedOn || (effectiveReasoningEnabled && !reasoningDisabled);// Search can use Unsloth tools or provider web search independently of Code.// Code follows the provider's sandbox placement and never falls back to local// execution when a hosted model lacks code support.@@ -2779,6 +2784,7 @@ export function SharedComposer({))}</>) : (+ effectiveSupportsReasoning &&effectiveSupportsReasoningOff &&!reasoningLockedOn && (<DropdownMenuItem@@ -2812,8 +2818,8 @@ export function SharedComposer({e.preventDefault();const next = !preserveThinking;setPreserveThinking(next);- // Preserve thinking requires thinking on.- if (next) {+ // Only local models couple this setting to generation controls.+ if (next && !isExternalModel) {setReasoningEnabled(true);applyQwenThinkingParams(true);}diff --git a/studio/frontend/tests/cancelled-turn-history-prune.test.ts b/studio/frontend/tests/cancelled-turn-history-prune.test.tsindex 7efca12844d..41b9a1c5217 100644--- a/studio/frontend/tests/cancelled-turn-history-prune.test.ts+++ b/studio/frontend/tests/cancelled-turn-history-prune.test.ts@@ -142,6 +142,6 @@ test("the send and token-count paths prune through the same helper", () => {);assert.match(adapter,- /const survivingMessages = pruneOutboundHistory\(\s*messages,\s*!isExternalRequest,\s*\);/,+ /const survivingMessages = pruneOutboundHistory\(\s*messages,\s*replayReasoning\);/,);});diff --git a/studio/frontend/tests/external-preserve-thinking.test.ts b/studio/frontend/tests/external-preserve-thinking.test.tsnew file mode 100644index 00000000000..9bfcc988ada--- /dev/null+++ b/studio/frontend/tests/external-preserve-thinking.test.ts@@ -0,0 +1,48 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++import assert from "node:assert/strict";+import { readFileSync } from "node:fs";+import test from "node:test";+import ts from "typescript";+import { registerBundlerResolver } from "./helpers/kit.ts";++registerBundlerResolver();+const { providerSupportsPreserveThinking } = await import("../src/features/chat/provider-capabilities.ts");+const source = readFileSync(new URL("../src/features/chat/api/chat-adapter.ts", import.meta.url), "utf8");+const tree = ts.createSourceFile("adapter.ts", source, ts.ScriptTarget.Latest, true);+let replayExpression = "";+let payloadExpression = "";+function visit(node: ts.Node): void {+ if (ts.isVariableDeclaration(node) && node.name.getText(tree) === "replayReasoning") {+ replayExpression = node.initializer!.getText(tree);+ }+ if (ts.isSpreadAssignment(node) && node.getText(tree).includes("providerSupportsPreserveThinking")) {+ payloadExpression = node.expression.getText(tree);+ }+ ts.forEachChild(node, visit);+}+visit(tree);+assert.ok(replayExpression && payloadExpression);+// Evaluates the real send-path expressions rather than a copied policy.+const evaluate = new Function("providerSupportsPreserveThinking", "externalProvider", "runtime", "isExternalRequest",+ `return { replay: ${replayExpression}, fields: ${payloadExpression} };`) as+ (supports: typeof providerSupportsPreserveThinking, provider: {providerType: string}, runtime: {preserveThinking: boolean}, external: boolean) =>+ {replay: boolean; fields: {preserve_thinking?: boolean}};++test("llama.cpp sends the selected value and replays reasoning only when enabled", () => {+ for (const enabled of [true, false]) {+ const result = evaluate(providerSupportsPreserveThinking, {providerType: "llama_cpp"}, {preserveThinking: enabled}, true);+ assert.deepEqual(result, {replay: enabled, fields: {preserve_thinking: enabled}});+ }+ assert.match(source, /pruneOutboundHistory\(messages, replayReasoning\)/);+ assert.match(source, /toOpenAIMessages\(message, replayReasoning\)/);+});++test("switching providers never leaks a retained enabled preference", () => {+ for (const providerType of ["custom", "openai", "vllm", "ollama", "anthropic"]) {+ assert.deepEqual(evaluate(providerSupportsPreserveThinking, {providerType}, {preserveThinking: true}, true),+ {replay: false, fields: {}});+ }+ assert.equal(evaluate(providerSupportsPreserveThinking, {providerType: ""}, {preserveThinking: false}, false).replay, true);+});diff --git a/studio/frontend/tests/mcp-images.test.ts b/studio/frontend/tests/mcp-images.test.tsindex 3d3b8216de1..6dc6d7ad8d2 100644--- a/studio/frontend/tests/mcp-images.test.ts+++ b/studio/frontend/tests/mcp-images.test.ts@@ -424,7 +424,7 @@ test("the send path bounds the run's own results before serializing them", () =>);assert.match(adapter,- /const messages = boundMcpImageResults\(rawMessages, \{\n\s*readsImages: targetReadsImages,\n\s*localMarkers: mcpImagesLocalMarkers,\n\s*\}\);\n\s*const survivingMessages = pruneOutboundHistory\(\n\s*messages,/,+ /const messages = boundMcpImageResults\(rawMessages, \{\n\s*readsImages: targetReadsImages,\n\s*localMarkers: mcpImagesLocalMarkers,\n\s*\}\);\n\s*const replayReasoning = [\s\S]*?;\n\s*const survivingMessages = pruneOutboundHistory\(messages, replayReasoning\);/,);assert.doesNotMatch(adapter, /boundMcpImageEnvelopes\(outboundMessages/);assert.doesNotMatch(adapter, /stripMcpImageEnvelopes\(outboundMessages/);diff --git a/studio/frontend/tests/provider-capabilities-current-models.test.ts b/studio/frontend/tests/provider-capabilities-current-models.test.tsindex 5e18bed1bcc..78992561b23 100644--- a/studio/frontend/tests/provider-capabilities-current-models.test.ts+++ b/studio/frontend/tests/provider-capabilities-current-models.test.ts@@ -21,6 +21,7 @@ const {providerSupportsBuiltinWebSearch,providerSupportsFastMode,+ providerSupportsPreserveThinking,} = await import("../src/features/chat/provider-capabilities.ts");const {@@ -508,3 +509,11 @@ test("hosted Code is only claimed where the backend registry hosts code_executio}}});+++test("preserve thinking is available only for explicit llama.cpp connections", () => {+ assert.equal(providerSupportsPreserveThinking("llama_cpp"), true);+ for (const provider of [undefined, null, "custom", "vllm", "ollama", "openai", "anthropic", "openrouter"]) {+ assert.equal(providerSupportsPreserveThinking(provider), false);+ }+});diff --git a/tests/studio/test_cancelled_turn_history_prune.py b/tests/studio/test_cancelled_turn_history_prune.pyindex 74999645803..c2ce5e46d48 100644--- a/tests/studio/test_cancelled_turn_history_prune.py+++ b/tests/studio/test_cancelled_turn_history_prune.py@@ -92,13 +92,13 @@ def _send_path_slice() -> str:"""body = slice_between(read(ADAPTER),- " const survivingMessages = pruneOutboundHistory(\n"- " messages,\n"- " !isExternalRequest,\n",+ " const survivingMessages = pruneOutboundHistory(messages, replayReasoning);","if (selectedImageEditReference) {",)return ("export function buildSendPathOutbound(messages: any, isExternalRequest: boolean) {\n"+ # The provider-dependent flag is covered by external-preserve-thinking.test.ts.+ + " const replayReasoning = !isExternalRequest;\n"+ body+ " return outboundMessages;\n}\n")