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.

Streaming tool detection: guard late tool_calls, filter incomplete fragments (#4648)

Daniel Han9d68621614b5
diff --git a/studio/backend/core/inference/llama_cpp.py b/studio/backend/core/inference/llama_cpp.py
index 8a0d1c0962f..2b4ed3b2e6d 100644
--- a/studio/backend/core/inference/llama_cpp.py
+++ b/studio/backend/core/inference/llama_cpp.py
@@ -2135,6 +2135,11 @@ class LlamaCppBackend:
# ── Structured tool_calls ──
tc_deltas = delta.get("tool_calls")
if tc_deltas:
+ # Once visible content has been
+ # emitted, do not reclassify this
+ # turn as a tool call.
+ if _last_emitted:
+ continue
has_structured_tc = True
detect_state = _S_DRAINING
for tc_d in tc_deltas:
@@ -2362,7 +2367,18 @@ class LlamaCppBackend:
tool_calls = None
content_text = content_accum
if has_structured_tc:
- tool_calls = [tool_calls_acc[i] for i in sorted(tool_calls_acc)]
+ # Filter out incomplete fragments (e.g. from
+ # truncation by max_tokens or disconnect).
+ tool_calls = [
+ tool_calls_acc[i]
+ for i in sorted(tool_calls_acc)
+ if (
+ tool_calls_acc[i]
+ .get("function", {})
+ .get("name", "")
+ .strip()
+ )
+ ] or None
if (
not tool_calls
and auto_heal_tool_calls