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.

Studio: forward `preserve_thinking` + `reasoning_effort` on the OpenAI passthrough (#6171)

oobabooga53af33798dd7
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index 70a5a3efe7b..4cc76e7c2a7 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -5037,7 +5037,9 @@ async def _responses_stream(
detail = "Image provided but current GGUF model does not support vision.",
)
- body = _build_openai_passthrough_body(chat_req, backend_ctx = llama_backend.context_length)
+ body = _build_openai_passthrough_body(
+ chat_req, backend_ctx = llama_backend.context_length, llama_backend = llama_backend
+ )
body["stream_options"] = {"include_usage": True}
target_url = f"{llama_backend.base_url}/v1/chat/completions"
@@ -6743,7 +6745,11 @@ def _extract_response_format(payload):
return rf if isinstance(rf, dict) else None
-def _build_openai_passthrough_body(payload, backend_ctx = None) -> dict:
+def _build_openai_passthrough_body(
+ payload,
+ backend_ctx = None,
+ llama_backend = None,
+) -> dict:
"""Assemble the llama-server request body from a ChatCompletionRequest.
Only known OpenAI / llama-server fields are forwarded, so Studio-specific
@@ -6754,12 +6760,19 @@ def _build_openai_passthrough_body(payload, backend_ctx = None) -> dict:
system_prompt, _, _ = _extract_content_parts(payload.messages)
messages = _set_or_prepend_system_message(messages, system_prompt)
tool_choice = payload.tool_choice if payload.tool_choice is not None else "auto"
- # When the caller asked for a specific reasoning mode, forward it via
- # chat_template_kwargs so the Jinja template renders with (or without) the
- # reasoning preamble.
- tpl_kwargs = None
- if payload.enable_thinking is not None:
- tpl_kwargs = {"enable_thinking": bool(payload.enable_thinking)}
+ # Forward per-request reasoning fields (enable_thinking / reasoning_effort /
+ # preserve_thinking) via chat_template_kwargs so the Jinja template renders
+ # in the caller's mode, gated on the active template's capabilities exactly
+ # like the non-passthrough paths.
+ tpl_kwargs = (
+ llama_backend._request_reasoning_kwargs(
+ payload.enable_thinking,
+ payload.reasoning_effort,
+ payload.preserve_thinking,
+ )
+ if llama_backend is not None
+ else None
+ )
return _build_passthrough_payload(
messages,
payload.tools,
@@ -6794,7 +6807,9 @@ async def _openai_passthrough_stream(
the client sees a standard OpenAI response.
"""
target_url = f"{llama_backend.base_url}/v1/chat/completions"
- body = _build_openai_passthrough_body(payload, backend_ctx = llama_backend.context_length)
+ body = _build_openai_passthrough_body(
+ payload, backend_ctx = llama_backend.context_length, llama_backend = llama_backend
+ )
_cancel_keys = (payload.cancel_id, payload.session_id, completion_id)
_tracker = _TrackedCancel(cancel_event, *_cancel_keys)
@@ -6939,7 +6954,9 @@ async def _openai_passthrough_non_streaming(llama_backend, payload, model_name):
``tool_calls``, and accurate ``usage`` token counts.
"""
target_url = f"{llama_backend.base_url}/v1/chat/completions"
- body = _build_openai_passthrough_body(payload, backend_ctx = llama_backend.context_length)
+ body = _build_openai_passthrough_body(
+ payload, backend_ctx = llama_backend.context_length, llama_backend = llama_backend
+ )
try:
async with httpx.AsyncClient() as client:
diff --git a/studio/backend/tests/test_openai_tool_passthrough.py b/studio/backend/tests/test_openai_tool_passthrough.py
index db4c07cdc8c..d8a965c1f2b 100644
--- a/studio/backend/tests/test_openai_tool_passthrough.py
+++ b/studio/backend/tests/test_openai_tool_passthrough.py
@@ -654,6 +654,100 @@ class TestBuildPassthroughPayloadToolChoice:
]
+# =====================================================================
+# Passthrough reasoning kwargs — enable_thinking / reasoning_effort /
+# preserve_thinking must reach llama-server via chat_template_kwargs,
+# gated on template capabilities like the non-passthrough paths.
+# =====================================================================
+
+
+def _reasoning_backend(
+ supports_reasoning = True,
+ reasoning_style = "enable_thinking",
+ reasoning_always_on = False,
+ supports_preserve_thinking = False,
+):
+ """Bare LlamaCppBackend with just the reasoning capability flags set,
+ so _build_openai_passthrough_body exercises the real
+ _request_reasoning_kwargs gating."""
+ from core.inference.llama_cpp import LlamaCppBackend
+
+ backend = LlamaCppBackend.__new__(LlamaCppBackend)
+ backend._supports_reasoning = supports_reasoning
+ backend._reasoning_style = reasoning_style
+ backend._reasoning_always_on = reasoning_always_on
+ backend._supports_preserve_thinking = supports_preserve_thinking
+ return backend
+
+
+class TestPassthroughReasoningKwargs:
+ def _payload(self, **fields):
+ return ChatCompletionRequest(
+ model = "default",
+ messages = [{"role": "user", "content": "hi"}],
+ **fields,
+ )
+
+ def test_enable_thinking_forwarded(self):
+ body = _build_openai_passthrough_body(
+ self._payload(enable_thinking = False),
+ backend_ctx = 4096,
+ llama_backend = _reasoning_backend(),
+ )
+ assert body["chat_template_kwargs"] == {"enable_thinking": False}
+
+ def test_preserve_thinking_forwarded_when_template_supports_it(self):
+ body = _build_openai_passthrough_body(
+ self._payload(enable_thinking = True, preserve_thinking = True),
+ backend_ctx = 4096,
+ llama_backend = _reasoning_backend(supports_preserve_thinking = True),
+ )
+ assert body["chat_template_kwargs"] == {
+ "enable_thinking": True,
+ "preserve_thinking": True,
+ }
+
+ def test_preserve_thinking_dropped_when_template_lacks_it(self):
+ body = _build_openai_passthrough_body(
+ self._payload(preserve_thinking = True),
+ backend_ctx = 4096,
+ llama_backend = _reasoning_backend(supports_preserve_thinking = False),
+ )
+ assert "chat_template_kwargs" not in body
+
+ def test_reasoning_effort_forwarded_for_effort_style_models(self):
+ body = _build_openai_passthrough_body(
+ self._payload(reasoning_effort = "high"),
+ backend_ctx = 4096,
+ llama_backend = _reasoning_backend(reasoning_style = "reasoning_effort"),
+ )
+ assert body["chat_template_kwargs"] == {"reasoning_effort": "high"}
+
+ def test_enable_thinking_maps_to_effort_for_effort_style_models(self):
+ body = _build_openai_passthrough_body(
+ self._payload(enable_thinking = False),
+ backend_ctx = 4096,
+ llama_backend = _reasoning_backend(reasoning_style = "reasoning_effort"),
+ )
+ assert body["chat_template_kwargs"] == {"reasoning_effort": "low"}
+
+ def test_always_on_reasoning_skips_thinking_kwargs(self):
+ body = _build_openai_passthrough_body(
+ self._payload(enable_thinking = False),
+ backend_ctx = 4096,
+ llama_backend = _reasoning_backend(reasoning_always_on = True),
+ )
+ assert "chat_template_kwargs" not in body
+
+ def test_no_reasoning_fields_omits_chat_template_kwargs(self):
+ body = _build_openai_passthrough_body(
+ self._payload(),
+ backend_ctx = 4096,
+ llama_backend = _reasoning_backend(supports_preserve_thinking = True),
+ )
+ assert "chat_template_kwargs" not in body
+
+
# =====================================================================
# OpenAI API compatibility helpers — verified spec edge cases
# =====================================================================
diff --git a/studio/backend/tests/test_responses_tool_passthrough.py b/studio/backend/tests/test_responses_tool_passthrough.py
index f0f27142146..beeee66b777 100644
--- a/studio/backend/tests/test_responses_tool_passthrough.py
+++ b/studio/backend/tests/test_responses_tool_passthrough.py
@@ -533,6 +533,10 @@ class TestResponsesStreamAdapter:
is_vision = False,
context_length = 4096,
base_url = "http://llama.test",
+ # Non-reasoning template: the real backend returns None here.
+ _request_reasoning_kwargs = (
+ lambda enable_thinking = None, reasoning_effort = None, preserve_thinking = None: None
+ ),
),
)