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: keep Deep Research internal calls out of the tool loop (#8424)

Daniel Handa1f829bbbda
diff --git a/studio/backend/core/research_runs.py b/studio/backend/core/research_runs.py
index f4bc1a2d6b6..1fe438fe793 100644
--- a/studio/backend/core/research_runs.py
+++ b/studio/backend/core/research_runs.py
@@ -1143,6 +1143,12 @@ class ResearchSupervisor:
"messages": messages,
"stream": True,
"stream_options": {"include_usage": True},
+ # Gathered page text lands in these prompts and research never reads tool calls
+ # back, so this hop must stay out of the tool loop. Both opt-outs are needed:
+ # --enable-tools overrides a per-request enable_tools, and an omitted
+ # enabled_tools resolves to every built-in, python and terminal included.
+ "tool_choice": "none",
+ "enabled_tools": [],
"temperature": inference.get("temperature", 0.2),
}
if inference.get("topP") is not None:
diff --git a/studio/backend/tests/test_research_internal_call_tool_gate.py b/studio/backend/tests/test_research_internal_call_tool_gate.py
new file mode 100644
index 00000000000..16722329ed4
--- /dev/null
+++ b/studio/backend/tests/test_research_internal_call_tool_gate.py
@@ -0,0 +1,148 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Deep Research's internal hop must never enter the local tool loop.
+
+Its prompts carry gathered web and document text and go back through /v1/chat/completions,
+where --enable-tools overrides a per-request enable_tools and an omitted enabled_tools
+resolves to every built-in, python and terminal included. These tests pin the opt-out at the
+route, where the decision is made, and pin that it costs an ordinary run nothing.
+"""
+
+import json
+
+import pytest
+from fastapi import FastAPI
+from fastapi.testclient import TestClient
+
+from auth.authentication import get_current_subject
+import routes.inference as inference_route
+from state.tool_policy import reset_tool_policy, set_tool_policy
+
+
[email protected](autouse = True)
+def _clean_policy():
+ reset_tool_policy()
+ yield
+ reset_tool_policy()
+
+
+class _Backend:
+ """Records which generation entry point the route picked."""
+
+ is_loaded = True
+ model_identifier = "test/model.gguf"
+ _is_audio = False
+ is_vision = False
+ supports_tools = True
+
+ def __init__(self):
+ self.calls = []
+
+ def generate_chat_completion(self, **kwargs):
+ self.calls.append(("plain", kwargs))
+ yield "the answer"
+
+ def generate_chat_completion_with_tools(self, **kwargs):
+ self.calls.append(("tool_loop", kwargs))
+ yield {"type": "content", "text": "the answer"}
+
+
+def _client(monkeypatch, backend):
+ monkeypatch.setattr(inference_route, "get_llama_cpp_backend", lambda: backend)
+ app = FastAPI()
+ app.include_router(inference_route.router)
+ app.dependency_overrides[get_current_subject] = lambda: "test-user"
+ return TestClient(app)
+
+
+def _research_payload(opt_out: bool):
+ """The payload ResearchSupervisor._stream_completion builds, with the opt-out on or off."""
+ body = {
+ "model": "test/model.gguf",
+ "messages": [
+ {"role": "user", "content": "<untrusted_web_evidence>...</untrusted_web_evidence>"}
+ ],
+ "stream": True,
+ "stream_options": {"include_usage": True},
+ "temperature": 0.2,
+ "max_tokens": 512,
+ }
+ if opt_out:
+ body["tool_choice"] = "none"
+ body["enabled_tools"] = []
+ return body
+
+
+def _entry_point(monkeypatch, *, policy, opt_out):
+ backend = _Backend()
+ if policy is not None:
+ set_tool_policy(policy)
+ response = _client(monkeypatch, backend).post(
+ "/chat/completions", json = _research_payload(opt_out)
+ )
+ assert response.status_code == 200
+ assert "the answer" in response.text
+ return backend.calls[0][0], backend.calls[0][1]
+
+
+def test_forced_tool_policy_would_reach_the_tool_loop_without_the_opt_out(monkeypatch):
+ # Guards the test below: without this, it would pass if the route ever stopped forcing
+ # tools on here, for entirely the wrong reason.
+ entry, kwargs = _entry_point(monkeypatch, policy = True, opt_out = False)
+ assert entry == "tool_loop"
+ assert {t["function"]["name"] for t in kwargs["tools"]} >= {"python", "terminal"}
+
+
[email protected]("policy", [None, True, False])
+def test_the_research_payload_never_enters_the_tool_loop(monkeypatch, policy):
+ entry, kwargs = _entry_point(monkeypatch, policy = policy, opt_out = True)
+ assert entry == "plain"
+ assert not kwargs.get("tools")
+
+
[email protected]("policy", [None, False])
+def test_the_opt_out_changes_nothing_a_default_install_does(monkeypatch, policy):
+ # Without --enable-tools the hop was already tool-free, so the two fields must not
+ # perturb what the model is handed: same entry point, same generation kwargs.
+ before_entry, before_kwargs = _entry_point(monkeypatch, policy = policy, opt_out = False)
+ reset_tool_policy()
+ after_entry, after_kwargs = _entry_point(monkeypatch, policy = policy, opt_out = True)
+
+ assert (before_entry, after_entry) == ("plain", "plain")
+ # cancel_event is a fresh object per request, so identity differences say nothing.
+ drop = {"cancel_event"}
+ assert {k: v for k, v in before_kwargs.items() if k not in drop} == {
+ k: v for k, v in after_kwargs.items() if k not in drop
+ }
+
+
+def test_json_mode_research_calls_send_llama_server_an_unchanged_body():
+ # The JSON-mode phases take the llama-server passthrough, not the loop above, so pin
+ # that wire body too: no tools means no tool_choice is forwarded, and Unsloth-only
+ # extensions never leave Studio.
+ from models.inference import ChatCompletionRequest
+
+ class _PassthroughBackend:
+ supports_tools = True
+ supports_tool_passthrough = True
+ markup_profile = None
+
+ def _request_reasoning_kwargs(self, enable_thinking, reasoning_effort, preserve):
+ return None
+
+ backend = _PassthroughBackend()
+ bodies = []
+ for opt_out in (False, True):
+ payload = ChatCompletionRequest(
+ **_research_payload(opt_out), response_format = {"type": "json_object"}
+ )
+ assert inference_route._takes_tool_passthrough(payload, backend) is True
+ bodies.append(
+ inference_route._build_openai_passthrough_body(payload, llama_backend = backend)
+ )
+
+ assert bodies[0] == bodies[1]
+ assert "tool_choice" not in bodies[1] and "tools" not in bodies[1]
+ assert "enabled_tools" not in bodies[1] and "enable_tools" not in bodies[1]
+ assert json.loads(json.dumps(bodies[1]))["response_format"] == {"type": "json_object"}
diff --git a/studio/backend/tests/test_research_runs_hardening.py b/studio/backend/tests/test_research_runs_hardening.py
index 799f8cc16cf..9a5b71f130d 100644
--- a/studio/backend/tests/test_research_runs_hardening.py
+++ b/studio/backend/tests/test_research_runs_hardening.py
@@ -728,7 +728,7 @@ def _install_fake_client(monkeypatch, responses: list) -> list:
return False
def build_request(self, method, url, **kwargs):
- return (method, url)
+ return {"method": method, "url": url, **kwargs}
async def post(self, url, **kwargs):
sent.append(url)
@@ -797,6 +797,17 @@ def _run_stream(supervisor, timeout_seconds: float = 30.0) -> tuple:
)
+def test_stream_completion_opts_out_of_the_tool_loop(monkeypatch):
+ # Gathered page text lands in these prompts, and --enable-tools would otherwise
+ # override the request and expand an omitted enabled_tools to every built-in.
+ sent = _install_fake_client(monkeypatch, [_response(200, body = _stream_body())])
+ supervisor = _make_supervisor(_noop_check_active)
+ assert _run_stream(supervisor) == ("report", "", "stop", None)
+ assert len(sent) == 1
+ assert sent[0]["json"]["tool_choice"] == "none"
+ assert sent[0]["json"]["enabled_tools"] == []
+
+
def _capture_backoff(monkeypatch) -> list:
"""Record the delays the retry loop asks for and return control immediately."""
delays: list[float] = []