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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Chat: keep the payload when a provider streams arguments as a decoded object (#10275)
Shubhankar Tripathyb8bfff26717b
diff --git a/studio/backend/core/inference/studio_tool_loop.py b/studio/backend/core/inference/studio_tool_loop.pyindex 91b8e9822a4..2aa4d61d28a 100644--- a/studio/backend/core/inference/studio_tool_loop.py+++ b/studio/backend/core/inference/studio_tool_loop.py@@ -281,6 +281,21 @@ def _normalized_call(call: dict[str, Any], fallback_id: str = "") -> dict[str, Areturn normalized+def _argument_fragment(value: Any) -> Any:+ """A decoded-object ``arguments`` delta as the text it would have streamed as.++ llama-server has shipped a decoded object where a string fragment belongs+ (ggml-org/llama.cpp#20198). Everything else passes through untouched, so ``None`` still means+ "announced, no arguments field". Mirrors ``streamedToolCallArguments`` in ``tool-call-arguments.ts``.+ """+ if isinstance(value, (dict, list)):+ try:+ return json.dumps(value, ensure_ascii = False, separators = (",", ":"))+ except (TypeError, ValueError, RecursionError):+ return ""+ return value++def _delta_text(content: Any) -> str:"""Text of a content delta, whether it is a plain string or content parts.@@ -642,7 +657,7 @@ class _Turn:# id would claim the finished call and append to it (issue #9807).held = self.by_index.get(key)new_function = raw_call.get("function")- new_arguments = (+ new_arguments = _argument_fragment(new_function.get("arguments") if isinstance(new_function, dict) else None)new_name = new_function.get("name") if isinstance(new_function, dict) else None@@ -797,8 +812,8 @@ class _Turn:current["function"]["name"] = fragmentelse:current["function"]["name"] = name_before + fragment- if isinstance(function.get("arguments"), str) and not resends_this_call:- current["function"]["arguments"] += function["arguments"]+ if isinstance(new_arguments, str) and not resends_this_call:+ current["function"]["arguments"] += new_argumentsif not (isinstance(call_id, str) and call_id):# The id fork cannot see this: an id-less stream has no ids to differ on, so appending glued two# calls into one blob (issue #9807).diff --git a/studio/backend/tests/test_studio_tool_loop.py b/studio/backend/tests/test_studio_tool_loop.pyindex 54eebe30655..2fd650a776e 100644--- a/studio/backend/tests/test_studio_tool_loop.py+++ b/studio/backend/tests/test_studio_tool_loop.py@@ -988,6 +988,77 @@ def _call_delta(index, call_id, name, arguments):}+def test_a_decoded_object_arguments_delta_reaches_the_tool(executed):+ """A string-only accumulator ran the tool with ``{}``, and the backend's authoritative+ tool_start then overwrote the payload the frontend had recovered, so both ends are pinned.+ """+ transport = FakeTransport(+ [+ [+ _sse(+ {"tool_calls": [_call_delta(0, "call_obj", "web_search", {"query": "value"})]}+ ),+ _sse(finish = "tool_calls"),+ _DONE,+ ],+ [_sse({"content": "done"}), _sse(finish = "stop"), _DONE],+ ],+ heals = False,+ )+ lines = _run(transport)++ assert [call["arguments"] for call in executed] == [{"query": "value"}]+ assert _events(lines, "tool_start")[0]["arguments"] == {"query": "value"}+++def test_a_decoded_object_lands_where_its_string_spelling_would():+ """Serializing puts the object on the path the accumulator already has for text, so the+ object-boundary fork and the id that holds a snapshot to its own call read it the same+ either way. Pinned as a pair: the one way this helper can mislead is by giving the two+ dialects different answers.+ """+ streams = [+ (+ "one payload",+ [[_call_delta(0, "c1", "s", {"query": "v"})]],+ [[_call_delta(0, "c1", "s", '{"query":"v"}')]],+ ),+ (+ "an empty opening, then the rest as fragments",+ [+ [_call_delta(0, "c1", "s", {})],+ [{"index": 0, "function": {"arguments": '{"query":'}}],+ [{"index": 0, "function": {"arguments": '"v"}'}}],+ ],+ [+ [_call_delta(0, "c1", "s", "{}")],+ [{"index": 0, "function": {"arguments": '{"query":'}}],+ [{"index": 0, "function": {"arguments": '"v"}'}}],+ ],+ ),+ (+ "two snapshots under one id",+ [+ [_call_delta(0, "c1", "s", {"query": "a"})],+ [_call_delta(0, "c1", "s", {"query": "ab"})],+ ],+ [+ [_call_delta(0, "c1", "s", '{"query":"a"}')],+ [_call_delta(0, "c1", "s", '{"query":"ab"}')],+ ],+ ),+ ]++ def _shape(batches):+ turn = loop_mod._Turn(round = 1)+ for batch in batches:+ turn.merge_structured(batch)+ return [(c["function"]["name"], c["function"]["arguments"]) for c in turn.calls()]++ for label, as_object, as_string in streams:+ assert _shape(as_object) == _shape(as_string), label++def test_budget_exhausted_parallel_call_is_replayed_with_its_call(executed):"""A tool result is only legal next to the call it answers.diff --git a/studio/frontend/src/features/chat/api/chat-adapter.ts b/studio/frontend/src/features/chat/api/chat-adapter.tsindex 22b0ca470b3..026e1f3e0a2 100644--- a/studio/frontend/src/features/chat/api/chat-adapter.ts+++ b/studio/frontend/src/features/chat/api/chat-adapter.ts@@ -114,6 +114,7 @@ import {createBoundaryScan,mergedToolCallArgumentsText,splitTopLevelJsonObjects,+ streamedToolCallArguments,toolCallArgumentsText,toolCallReplayArguments,} from "../tool-call-arguments";@@ -6998,10 +6999,9 @@ export function createOpenAIStreamAdapter(typeof call.index === "number" ? call.index : undefined;const stableId = call.id;// The chunk is cast, not validated, and llama-server has shipped `arguments` as a decoded object.- const deltaArgs =- typeof call.function?.arguments === "string"- ? call.function.arguments- : "";+ const deltaArgs = streamedToolCallArguments(+ call.function?.arguments,+ );// Unsloth's local Codex loop follows the OpenAI tool-call delta with tool_start/tool_end, so// resolve the backend id now to keep all three shapes on one card. Before resolving, since// a provider claiming a minted spelling would merge two calls.diff --git a/studio/frontend/src/features/chat/tool-call-arguments.ts b/studio/frontend/src/features/chat/tool-call-arguments.tsindex d791818c1fb..7034ea7f3a1 100644--- a/studio/frontend/src/features/chat/tool-call-arguments.ts+++ b/studio/frontend/src/features/chat/tool-call-arguments.ts@@ -418,3 +418,24 @@ export function mergedToolCallArgumentsText(return stringifyJson(mergedArgs ?? {});}}++/**+ * The argument text one streamed delta contributes to its call's slot.+ *+ * The API specifies `function.arguments` as a string fragment; llama-server has shipped an+ * already-decoded object instead (ggml-org/llama.cpp#20198), and dropping it loses the call's+ * whole payload. Any other non-string contributes nothing rather than `"undefined"`.+ */+export function streamedToolCallArguments(value: unknown): string {+ if (typeof value === "string") {+ return value;+ }+ if (value === null || typeof value !== "object") {+ return "";+ }+ try {+ return JSON.stringify(value) ?? "";+ } catch {+ return "";+ }+}diff --git a/studio/frontend/tests/parallel-tool-call-arguments.test.ts b/studio/frontend/tests/parallel-tool-call-arguments.test.tsindex 0affa355b01..2040538792a 100644--- a/studio/frontend/tests/parallel-tool-call-arguments.test.ts+++ b/studio/frontend/tests/parallel-tool-call-arguments.test.ts@@ -16,6 +16,7 @@ import ts from "typescript";import {createBoundaryScan,splitTopLevelJsonObjects,+ streamedToolCallArguments,toolCallReplayArguments,} from "../src/features/chat/tool-call-arguments.ts";import {@@ -267,6 +268,7 @@ function makeStream(mintPartIds = false): {return new Function("splitTopLevelJsonObjects","createBoundaryScan",+ "streamedToolCallArguments","findStreamedToolCallPartIndex","mintStreamedToolCallId","bindStreamedToolCallCard",@@ -275,6 +277,7 @@ function makeStream(mintPartIds = false): {)(splitTopLevelJsonObjects,createBoundaryScan,+ streamedToolCallArguments,findStreamedToolCallPartIndex,mintStreamedToolCallId,bindStreamedToolCallCard,@@ -723,6 +726,8 @@ test("a name resent or grown after a call closed invents nothing", () => {test("an argument fragment that is not a string does not abort the stream", () => {// llama-server has shipped `arguments` as a decoded object rather than the// string the API specifies, and the chunk is cast rather than validated.+ // A serialized object is one complete arguments document, so a further fragment+ // in the same slot is the next parallel call rather than a continuation.const parts = run([[{@@ -733,10 +738,13 @@ test("an argument fragment that is not a string does not abort the stream", () =},},],- [{ index: 0, function: { arguments: '{"a":1}' } }],+ [{ index: 0, function: { arguments: '{"b":2}' } }],]);- assert.deepEqual(shape(parts), [["alpha", '{"a":1}']]);+ assert.deepEqual(shape(parts), [+ ["alpha", '{"a":1}'],+ ["alpha", '{"b":2}'],+ ]);});test("a fragment that does not open an object does not open a call", () => {@@ -1571,3 +1579,92 @@ test("a tool that really takes a _raw parameter keeps it either way", () => {'{"_raw":"{\\"one\\":1}"}',);});++test("decoded object arguments preserve their JSON payload", () => {+ assert.equal(+ streamedToolCallArguments({ query: "雪", nested: [1, { ok: true }] }),+ '{"query":"雪","nested":[1,{"ok":true}]}',+ );+});++test("string fragments pass through byte-exact and junk contributes nothing", () => {+ assert.equal(streamedToolCallArguments('{"partial'), '{"partial');+ assert.equal(streamedToolCallArguments(""), "");+ assert.equal(streamedToolCallArguments(undefined), "");+ assert.equal(streamedToolCallArguments(null), "");+ assert.equal(streamedToolCallArguments(7), "");+});++test("the adapter's delta site reads arguments through the helper", () => {+ // Pinned so a tidy-up cannot bring the payload-dropping typeof-ternary back.+ assert.ok(+ adapterSource.includes(+ "const deltaArgs = streamedToolCallArguments(",+ ),+ "chat-adapter.ts no longer routes delta arguments through streamedToolCallArguments",+ );+});++test("a delta whose arguments arrive as a decoded object keeps its payload", () => {+ const parts = run([+ [+ {+ id: "call-obj",+ index: 0,+ function: {+ name: "web_search",+ // What llama-server has actually shipped, cast or not.+ arguments: { query: "value" } as unknown as string,+ },+ },+ ],+ ]);+ assert.deepEqual(+ parts.map((part) => part.argsText),+ ['{"query":"value"}'],+ );+});++test("a decoded object lands exactly where its string spelling would", () => {+ // The whole contract: serializing puts the object on the path the accumulator+ // already has for text, so every downstream rule -- the object boundary that+ // opens the next parallel call, the id that keeps a snapshot on its own -- reads+ // it the same either way. Pinned as a pair so a later change cannot give the two+ // dialects different answers, which is the one way this helper can mislead.+ const asObject = (a: unknown) => a as unknown as string;+ for (const [label, objectStream, stringStream] of [+ [+ "one payload",+ [[{ id: "c1", index: 0, function: { name: "s", arguments: asObject({ query: "v" }) } }]],+ [[{ id: "c1", index: 0, function: { name: "s", arguments: '{"query":"v"}' } }]],+ ],+ [+ "an empty opening, then the rest as fragments",+ [+ [{ id: "c1", index: 0, function: { name: "s", arguments: asObject({}) } }],+ [{ index: 0, function: { arguments: '{"query":' } }],+ [{ index: 0, function: { arguments: '"v"}' } }],+ ],+ [+ [{ id: "c1", index: 0, function: { name: "s", arguments: "{}" } }],+ [{ index: 0, function: { arguments: '{"query":' } }],+ [{ index: 0, function: { arguments: '"v"}' } }],+ ],+ ],+ [+ "two snapshots under one id",+ [+ [{ id: "c1", index: 0, function: { name: "s", arguments: asObject({ query: "a" }) } }],+ [{ id: "c1", index: 0, function: { name: "s", arguments: asObject({ query: "ab" }) } }],+ ],+ [+ [{ id: "c1", index: 0, function: { name: "s", arguments: '{"query":"a"}' } }],+ [{ id: "c1", index: 0, function: { name: "s", arguments: '{"query":"ab"}' } }],+ ],+ ],+ ] as const) {+ const shapeOf = (deltas: unknown) =>+ run(deltas as never).map((part) => [part.toolName, part.argsText]);+ assert.deepEqual(shapeOf(objectStream), shapeOf(stringStream), label);+ }+});