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 earlier tool calls in chat history on safetensors and MLX models (#12574)

Nilayfaaca35debc7
diff --git a/studio/backend/core/inference/chat_template_helpers.py b/studio/backend/core/inference/chat_template_helpers.py
index 16bb5d270e3..bc8bec0dc33 100644
--- a/studio/backend/core/inference/chat_template_helpers.py
+++ b/studio/backend/core/inference/chat_template_helpers.py
@@ -2647,7 +2647,7 @@ def _normalize_tool_call_arguments(messages: list) -> list:
if isinstance(args, str):
try:
parsed = json.loads(args)
- except (ValueError, TypeError):
+ except (ValueError, TypeError, RecursionError):
parsed = None
if isinstance(parsed, dict):
call = {**call, "function": {**fn, "arguments": parsed}}
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index 5b8b7e35542..3e3aa1321e9 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -24393,6 +24393,8 @@ def _extract_content_parts(
# A reasoning-only turn has no visible content, but still needs a
# message for templates that consume reasoning_content.
combined_text = ""
+ elif msg.role == "assistant" and msg.tool_calls:
+ combined_text = ""
if combined_text is None:
continue
@@ -24407,9 +24409,7 @@ def _extract_content_parts(
# Carried through: promote_history reads it to decide whether an envelope
# came from an MCP server, and dropping it here made an unnamed tool
# message that bypasses the check entirely. Resolved from the call when the
- # result itself is unnamed: this rebuild drops tool_call_id and the calls,
- # so the correlation has to happen here or the local path cannot run the
- # provenance gate at all.
+ # result itself is unnamed.
if msg.name:
chat_message["name"] = msg.name
if msg.role == "tool":
@@ -24418,13 +24418,22 @@ def _extract_content_parts(
chat_message["name"] = _tool_name
if msg.role == "assistant" and msg.reasoning_content:
chat_message["reasoning_content"] = msg.reasoning_content
+ if msg.tool_calls:
+ chat_message["tool_calls"] = msg.tool_calls
+ if msg.tool_call_id:
+ chat_message["tool_call_id"] = msg.tool_call_id
chat_messages.append(chat_message)
+ # Gated so a history without tool calls renders exactly as before.
+ if any(m.get("tool_calls") for m in chat_messages):
+ chat_messages = _strip_provider_synthetic_tool_history(chat_messages)
+
# A user's own attachment outranks an assistant-generated one, as the frontend's
# legacy image_base64 field does. An assistant-only history still falls back.
return (
"\n\n".join(p for p in system_parts if p),
- chat_messages,
+ # Mappings, not JSON strings: Qwen3.5's template renders string arguments as nothing.
+ _structured_tool_history_for_local_template(chat_messages),
served_images if structured else (latest_user_image_b64 or latest_image_b64),
)
@@ -40829,7 +40838,7 @@ def _structured_tool_history_for_local_template(messages: list[dict]) -> list[di
if isinstance(args, str):
try:
parsed = json.loads(args)
- except ValueError:
+ except (ValueError, RecursionError):
parsed = None
if isinstance(parsed, dict):
tc = {**tc, "function": {**fn, "arguments": parsed}}
diff --git a/studio/backend/tests/test_chat_template_tool_arguments.py b/studio/backend/tests/test_chat_template_tool_arguments.py
index 276f8b48ce5..e337aa75f48 100644
--- a/studio/backend/tests/test_chat_template_tool_arguments.py
+++ b/studio/backend/tests/test_chat_template_tool_arguments.py
@@ -116,6 +116,12 @@ def test_lenient_template_receives_original_string_untouched():
assert tok.seen_arguments == '{"query": "x"}'
+def test_deeply_nested_arguments_are_left_as_a_string():
+ arguments = '{"x":' + "[" * 10000 + "0" + "]" * 10000 + "}"
+ conv = _conv(arguments)
+ assert _normalize_tool_call_arguments(conv) is conv
+
+
def test_messages_without_tool_calls_pass_through_unchanged():
conv = [{"role": "user", "content": "hi"}]
assert _normalize_tool_call_arguments(conv) is conv
diff --git a/studio/backend/tests/test_openai_tool_passthrough.py b/studio/backend/tests/test_openai_tool_passthrough.py
index 4679ef3a5fd..f588bc532fe 100644
--- a/studio/backend/tests/test_openai_tool_passthrough.py
+++ b/studio/backend/tests/test_openai_tool_passthrough.py
@@ -4383,6 +4383,7 @@ class TestGgufVisionMessages:
"role": "tool",
"content": "[1 image returned]",
"name": "mcp__fs__read_media_file",
+ "tool_call_id": "call_0",
}
def test_a_replayed_envelope_alone_does_not_demand_a_vision_model(self):
diff --git a/studio/backend/tests/test_sf_tool_choice_none_history.py b/studio/backend/tests/test_sf_tool_choice_none_history.py
index a7ddf7c9ca5..efb154efe11 100644
--- a/studio/backend/tests/test_sf_tool_choice_none_history.py
+++ b/studio/backend/tests/test_sf_tool_choice_none_history.py
@@ -172,3 +172,97 @@ def test_tool_history_survives_plain_openai_client(monkeypatch):
tool_msgs = [m for m in msgs if isinstance(m, dict) and m.get("role") == "tool"]
assert assistant and assistant[0].get("tool_calls"), "assistant tool_calls dropped"
assert tool_msgs and tool_msgs[0].get("tool_call_id") == "call_abc", "tool_call_id dropped"
+
+
+def test_studio_tool_loop_keeps_earlier_tool_calls(monkeypatch):
+ backend = _ScriptedBackend()
+ payload = ChatCompletionRequest(
+ model = "default",
+ messages = [
+ ChatMessage(role = "user", content = "sum 1..100 in python"),
+ ChatMessage(
+ role = "assistant",
+ content = None,
+ tool_calls = [
+ {
+ "id": "call_py",
+ "type": "function",
+ "function": {
+ "name": "python",
+ "arguments": '{"code": "print(sum(range(101)))"}',
+ },
+ }
+ ],
+ ),
+ ChatMessage(role = "tool", tool_call_id = "call_py", name = "python", content = "5050"),
+ ChatMessage(role = "assistant", content = "The sum is 5050."),
+ ChatMessage(role = "user", content = "now up to 5000, reuse your code"),
+ ],
+ enable_tools = True,
+ enabled_tools = ["python"],
+ studio_tool_history = True,
+ stream = False,
+ )
+ _run(payload, monkeypatch, backend)
+
+ loop_calls = [c for c in backend.calls if c.get("loop")]
+ assert loop_calls, "the Studio tool loop never ran"
+ msgs = loop_calls[0]["messages"]
+ assert [m["role"] for m in msgs] == ["user", "assistant", "tool", "assistant", "user"]
+ assert msgs[1]["tool_calls"][0]["function"]["name"] == "python"
+ assert msgs[1]["tool_calls"][0]["function"]["arguments"] == {"code": "print(sum(range(101)))"}
+ assert msgs[2]["tool_call_id"] == "call_py"
+
+
+def test_provider_synthetic_tool_calls_never_reach_the_local_template():
+ from routes.inference import _extract_content_parts
+
+ messages = [
+ ChatMessage(role = "user", content = "compute 2**100"),
+ ChatMessage(
+ role = "assistant",
+ content = None,
+ tool_calls = [
+ {
+ "id": "srv_1",
+ "type": "function",
+ "function": {
+ "name": "code_execution",
+ "arguments": json.dumps({"_server_tool": True, "code": "print(2**100)"}),
+ },
+ }
+ ],
+ ),
+ ChatMessage(role = "tool", tool_call_id = "srv_1", name = "code_execution", content = "1267"),
+ ChatMessage(role = "assistant", content = "It is 1267."),
+ ChatMessage(role = "user", content = "and 2**10?"),
+ ]
+ _, chat_messages, _ = _extract_content_parts(messages)
+
+ assert [m["role"] for m in chat_messages] == ["user", "assistant", "user"]
+ assert "_server_tool" not in json.dumps(chat_messages)
+
+
+def test_deeply_nested_history_arguments_stay_a_string():
+ from routes.inference import _extract_content_parts
+
+ arguments = '{"x":' + "[" * 10000 + "0" + "]" * 10000 + "}"
+ messages = [
+ ChatMessage(role = "user", content = "q"),
+ ChatMessage(
+ role = "assistant",
+ content = None,
+ tool_calls = [
+ {
+ "id": "call_0",
+ "type": "function",
+ "function": {"name": "python", "arguments": arguments},
+ }
+ ],
+ ),
+ ChatMessage(role = "tool", tool_call_id = "call_0", name = "python", content = "r"),
+ ChatMessage(role = "user", content = "again"),
+ ]
+ _, chat_messages, _ = _extract_content_parts(messages)
+
+ assert chat_messages[1]["tool_calls"][0]["function"]["arguments"] == arguments