Skip to content

The agent that grows with you

fix: keep LM Studio / Kimi / MiniMax streamed tool calls as one call

Teknium3f3bdf3ee264
diff --git a/agent/chat_completion_helpers_tool_calls.py b/agent/chat_completion_helpers_tool_calls.py
index 02e11c7d738..aadd2eae59f 100644
--- a/agent/chat_completion_helpers_tool_calls.py
+++ b/agent/chat_completion_helpers_tool_calls.py
@@ -1,5 +1,6 @@
"""Streamed tool-call delta assembly for the chat-completions stream (and Relay's accumulator)."""
+import json
from typing import Optional
from agent.reasoning_carriers import field
@@ -26,6 +27,23 @@ class _ToolCallAccumulator:
self.acc[idx]["function"]["arguments"] = "".join(parts)
return self.acc
+ def _starts_new_call(self, tc_delta, slot: int) -> bool:
+ """Whether a fresh id at a reused raw index opens a new call (Ollama parallel batch) or
+ continues the current one. LM Studio and Kimi send a fresh id on every argument fragment
+ with no name, and some MiniMax routes resend the same name on each fragment too: only a
+ named delta can start a call, and a same-name one only once the current call's arguments
+ are complete JSON (two parallel calls of one tool must still split)."""
+ name = getattr(getattr(tc_delta, "function", None), "name", None)
+ if not name:
+ return False
+ if name != self.acc.get(slot, {}).get("function", {}).get("name"):
+ return True
+ try:
+ json.loads("".join(self._argument_parts.get(slot, ())))
+ except ValueError:
+ return False
+ return True
+
def feed(self, tc_delta) -> Optional[str]:
"""Merge one delta; return the tool name the first time it is complete."""
raw_idx = getattr(tc_delta, "index", None)
@@ -37,7 +55,10 @@ class _ToolCallAccumulator:
tc_id = str(tc_id)
self._active_slot_by_idx.setdefault(raw_idx, raw_idx)
- if delta_id and raw_idx in self._last_id_at_idx and delta_id != self._last_id_at_idx[raw_idx]:
+ if (
+ delta_id and raw_idx in self._last_id_at_idx and delta_id != self._last_id_at_idx[raw_idx]
+ and self._starts_new_call(tc_delta, self._active_slot_by_idx[raw_idx])
+ ):
self._active_slot_by_idx[raw_idx] = max(self.acc, default=-1) + 1
if delta_id:
self._last_id_at_idx[raw_idx] = delta_id
diff --git a/tests/agent/test_chat_completion_helpers_tool_calls.py b/tests/agent/test_chat_completion_helpers_tool_calls.py
new file mode 100644
index 00000000000..fed9a9abd13
--- /dev/null
+++ b/tests/agent/test_chat_completion_helpers_tool_calls.py
@@ -0,0 +1,33 @@
+"""Slot assignment of streamed tool-call deltas whose id changes at a reused index."""
+
+from types import SimpleNamespace
+
+from agent.chat_completion_helpers_tool_calls import _ToolCallAccumulator
+
+
+def _calls(*deltas):
+ acc = _ToolCallAccumulator()
+ for tc_id, name, args in deltas:
+ acc.feed(SimpleNamespace(index=0, id=tc_id, function=SimpleNamespace(name=name, arguments=args)))
+ return [(e["function"]["name"], e["function"]["arguments"]) for _, e in sorted(acc.materialize().items())]
+
+
+def test_nameless_fragments_with_fresh_ids_stay_one_call():
+ # LM Studio / Kimi: fresh id on every argument fragment, name only on the first (#5254).
+ assert _calls(
+ ("701601222", "terminal", ""),
+ ("tool-call-1", None, '{"com'),
+ ("tool-call-2", None, 'mand": '),
+ ("tool-call-3", None, '"ls"}'),
+ ) == [("terminal", '{"command": "ls"}')]
+
+
+def test_same_name_fresh_id_splits_only_after_complete_arguments():
+ # A name resent on each fragment continues the call (MiniMax); a same-name delta after
+ # complete arguments is a second parallel call (Ollama); a different name always splits.
+ assert _calls(
+ ("mm-1", "terminal", '{"command": '),
+ ("mm-2", "terminal", '"ls"}'),
+ ("mm-3", "terminal", '{"command": "pwd"}'),
+ ("mm-4", "read_file", "{}"),
+ ) == [("terminal", '{"command": "ls"}'), ("terminal", '{"command": "pwd"}'), ("read_file", "{}")]