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: don't re-prompt a finished code answer on safetensors and MLX models (#12309)

Nilay4f9785e782bb
diff --git a/studio/backend/core/inference/safetensors_agentic.py b/studio/backend/core/inference/safetensors_agentic.py
index e79fcb6bb6b..ad7af3b295a 100644
--- a/studio/backend/core/inference/safetensors_agentic.py
+++ b/studio/backend/core/inference/safetensors_agentic.py
@@ -20,6 +20,7 @@ from typing import Callable, Generator, Optional
from loggers import get_logger
+from core.inference.llama_cpp import _has_answer_artifact
from core.inference.tool_call_parser import (
_GEMMA_BARE_TC_PREFIX_RE,
_balanced_brace_end,
@@ -356,24 +357,29 @@ def _status_for_tool(tool_name: str, arguments: dict) -> str:
return status_for_tool(tool_name, arguments)
-def _reprompt_intent_text(text: str, *, reasoning_prefilled: bool = False) -> str:
+def _reprompt_intent_text(
+ text: str,
+ *,
+ reasoning_prefilled: bool = False,
+ visible_only: bool = False,
+) -> str:
"""Return visible answer text for the plan-without-action classifier.
Safetensors reasoning shares the cumulative text channel with the answer.
Forward-looking phrases inside ``<think>`` / ``[THINK]`` are private
planning, not a user-visible promise to call a tool. Match GGUF's behavior:
classify visible content when present and fall back to reasoning only for a
- reasoning-only stall.
+ reasoning-only stall. ``visible_only`` drops that fallback and returns "" instead.
"""
prefilled_reasoning = ""
if reasoning_prefilled:
close = _THINK_CLOSE_RE.search(text)
if close is None:
- return text.strip()
+ return "" if visible_only else text.strip()
prefilled_reasoning = text[: close.end()].strip()
text = text[close.end() :].strip()
if not text:
- return prefilled_reasoning
+ return "" if visible_only else prefilled_reasoning
spans = _think_spans_outside_tool_markup(text)
if not spans:
@@ -390,7 +396,7 @@ def _reprompt_intent_text(text: str, *, reasoning_prefilled: bool = False) -> st
visible_text = "".join(visible).strip()
reasoning_text = "".join(reasoning).strip()
- if visible_text:
+ if visible_text or visible_only:
return visible_text
return "\n".join(part for part in (prefilled_reasoning, reasoning_text) if part).strip()
@@ -1210,6 +1216,18 @@ def run_safetensors_tool_loop(
and not any(record.executed for record in tool_controller.history)
and not is_reprompt_repeat(intent_text, last_reprompt_text)
and is_short_intent_without_action(intent_text)
+ # Markup stripped first: a call fenced inside <tool_call> is not an answer.
+ and not _has_answer_artifact(
+ strip_tool_markup(
+ _reprompt_intent_text(
+ content_accum,
+ reasoning_prefilled = reasoning_prefilled,
+ visible_only = True,
+ ),
+ final = True,
+ enabled_tool_names = _enabled_tool_names,
+ )
+ )
):
reprompt_count += 1
last_reprompt_text = intent_text
diff --git a/studio/backend/tests/test_safetensors_tool_loop.py b/studio/backend/tests/test_safetensors_tool_loop.py
index afe5eba4076..958e43c1107 100644
--- a/studio/backend/tests/test_safetensors_tool_loop.py
+++ b/studio/backend/tests/test_safetensors_tool_loop.py
@@ -3292,6 +3292,99 @@ class TestLoopRePrompt:
contents = [e for e in events if e["type"] == "content"]
assert contents and contents[-1]["text"].strip() == "4"
+ @pytest.mark.parametrize(
+ "reasoning, answer, reasoning_prefilled",
+ [
+ pytest.param(
+ "",
+ "I'll write a small Python function that reverses a string.\n"
+ "```python\ndef reverse(s):\n return s[::-1]\n```\n",
+ False,
+ id = "plain",
+ ),
+ pytest.param(
+ "",
+ "Let me run this:\n```python\nprint(1)\n```\n",
+ False,
+ id = "run_intent_with_code",
+ ),
+ pytest.param(
+ "Plan it.</think>",
+ "I'll write it.\n```python\ndef f(): pass\n```\n",
+ True,
+ id = "prefilled_then_answer",
+ ),
+ ],
+ )
+ def test_finished_code_answer_is_not_reprompted(self, reasoning, answer, reasoning_prefilled):
+ loop, exec_fn = _make_loop(
+ turns = [[reasoning + answer], ["No tool is needed. Here it is again."]],
+ nudge_tool_calls = True,
+ reasoning_prefilled = reasoning_prefilled,
+ )
+ events = _collect_events(loop)
+ statuses = [e["text"] for e in events if e["type"] == "status"]
+ contents = [e["text"] for e in events if e["type"] == "content"]
+ assert NUDGE_TOOL_CALLS_STATUS not in statuses
+ assert exec_fn.calls == []
+ assert contents[-1] == reasoning + answer
+
+ @pytest.mark.parametrize(
+ "chunk, reasoning_prefilled",
+ [
+ pytest.param(
+ "<think>I'll write it.\n```python\ndef f(): pass\n```\n</think>",
+ False,
+ id = "think_block_only",
+ ),
+ pytest.param(
+ "I'll write it.\n```python\ndef f(): pass\n```\n",
+ True,
+ id = "prefilled_unclosed",
+ ),
+ pytest.param(
+ "I'll write it.\n```python\ndef f(): pass\n```\n</think>",
+ True,
+ id = "prefilled_closed_empty_answer",
+ ),
+ ],
+ )
+ def test_code_only_in_reasoning_is_still_reprompted(self, chunk, reasoning_prefilled):
+ loop, exec_fn = _make_loop(
+ turns = [[chunk], ["Done."]],
+ nudge_tool_calls = True,
+ reasoning_prefilled = reasoning_prefilled,
+ )
+ events = _collect_events(loop)
+ statuses = [e["text"] for e in events if e["type"] == "status"]
+ assert NUDGE_TOOL_CALLS_STATUS in statuses
+
+ @pytest.mark.parametrize(
+ "chunk",
+ [
+ pytest.param(
+ 'Let me run it.\n<tool_call>\n```json\n{"name": "python", "arguments": '
+ '{"code": "print(1)"}}\n```\n</tool_call>',
+ id = "fenced_json_call",
+ ),
+ pytest.param(
+ "Let me run it.\n<tool_call>\n```python\nprint(1)\n```\n</tool_call>",
+ id = "fenced_code_call",
+ ),
+ ],
+ )
+ def test_unparsed_call_with_a_fence_is_still_reprompted(self, chunk):
+ call = '<tool_call>\n{"name": "python", "arguments": {"code": "print(1)"}}\n</tool_call>'
+ loop, exec_fn = _make_loop(
+ turns = [[chunk], [call], ["1"]],
+ exec_results = ["1"],
+ nudge_tool_calls = True,
+ )
+ events = _collect_events(loop)
+ statuses = [e["text"] for e in events if e["type"] == "status"]
+ assert NUDGE_TOOL_CALLS_STATUS in statuses
+ assert exec_fn.calls == [("python", {"code": "print(1)"})]
+
def test_max_reprompts_capped(self):
# Model keeps stalling with intent -- after MAX_ACT_REPROMPTS re-prompts
# the loop must give up rather than burn forever.