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 each imported tool call's own result when a chat file reuses call ids (#11596)

Nilaydcbb66e84e06
diff --git a/studio/frontend/src/features/chat/utils/chat-import.ts b/studio/frontend/src/features/chat/utils/chat-import.ts
index 2bf592cf74f..687f4e244be 100644
--- a/studio/frontend/src/features/chat/utils/chat-import.ts
+++ b/studio/frontend/src/features/chat/utils/chat-import.ts
@@ -150,12 +150,7 @@ function oaiMessagesToRecords(
baseTs: number,
): MessageRecord[] {
const toolResults = new Map<string, string>();
- for (const m of oaiMsgs) {
- const msg = m as Record<string, unknown>;
- if (msg.role === "tool" && typeof msg.tool_call_id === "string") {
- toolResults.set(msg.tool_call_id, typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content ?? ""));
- }
- }
+ const pending: Array<{ turn: number; part: { toolCallId: string; result?: string } }> = [];
const records: MessageRecord[] = [];
let prevId: string | null = null;
@@ -164,7 +159,21 @@ function oaiMessagesToRecords(
for (const m of oaiMsgs) {
const msg = m as Record<string, unknown>;
const role = msg.role as string;
- if (role === "tool") continue;
+ if (role === "tool") {
+ if (typeof msg.tool_call_id !== "string") continue;
+ const result = typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content ?? "");
+ let at = pending.length - 1;
+ while (at >= 0 && pending[at].part.toolCallId !== msg.tool_call_id) at--;
+ if (at === -1) {
+ toolResults.set(msg.tool_call_id, result);
+ } else {
+ const { turn } = pending[at];
+ at = pending.findIndex((entry) => entry.turn === turn && entry.part.toolCallId === msg.tool_call_id);
+ pending[at].part.result = result;
+ pending.splice(at, 1);
+ }
+ continue;
+ }
const id = crypto.randomUUID();
@@ -183,15 +192,15 @@ function oaiMessagesToRecords(
// _raw matches what the stream adapter and the backend keep for arguments the model did not
// emit as valid JSON.
try { args = JSON.parse(argsStr); } catch { args = { _raw: argsStr }; }
- const result = toolResults.get(tcId);
- parts.push({
+ const part = {
type: "tool-call",
toolCallId: tcId,
toolName: name,
args,
argsText: argsStr,
- ...(result !== undefined ? { result } : {}),
- });
+ };
+ parts.push(part);
+ pending.push({ turn: idx, part });
}
}
content = parts;
@@ -213,6 +222,11 @@ function oaiMessagesToRecords(
idx++;
}
+ for (const { part } of pending) {
+ const result = toolResults.get(part.toolCallId);
+ if (result !== undefined) part.result = result;
+ }
+
return records;
}
diff --git a/studio/frontend/tests/message-jsonl-import.test.ts b/studio/frontend/tests/message-jsonl-import.test.ts
index f1d9e4c3164..2ff84311537 100644
--- a/studio/frontend/tests/message-jsonl-import.test.ts
+++ b/studio/frontend/tests/message-jsonl-import.test.ts
@@ -114,6 +114,67 @@ test("ShareGPT import maps role aliases regardless of case and whitespace", () =
);
});
+function toolCallResults(conversation: ParsedConversation): string[] {
+ return conversation.messages.flatMap((message) =>
+ (message.content as ReadonlyArray<{ type: string; result?: unknown }>)
+ .filter((part) => part.type === "tool-call")
+ .map((part) => String(part.result)),
+ );
+}
+
+test("each turn keeps its own tool result when a chat file reuses call ids", () => {
+ const call =
+ '{"role":"assistant","content":null,"tool_calls":[{"id":"tool_call_0","type":"function","function":{"name":"get_time","arguments":"{}"}}]}';
+ const [conversation] = parseImportText(
+ [
+ '{"role":"user","content":"stopped"}',
+ call,
+ '{"role":"user","content":"first"}',
+ call,
+ '{"role":"tool","tool_call_id":"tool_call_0","name":"get_time","content":"RESULT_A"}',
+ '{"role":"assistant","content":"It is A"}',
+ '{"role":"user","content":"again"}',
+ call,
+ '{"role":"tool","tool_call_id":"tool_call_0","name":"get_time","content":"RESULT_B"}',
+ '{"role":"assistant","content":"It is B"}',
+ ].join("\n"),
+ "conversation-messages.jsonl",
+ );
+
+ assert.deepEqual(toolCallResults(conversation), ["undefined", "RESULT_A", "RESULT_B"]);
+});
+
+test("parallel calls sharing an id in one turn take their results in call order", () => {
+ const [conversation] = parseImportText(
+ [
+ '{"role":"user","content":"weather"}',
+ '{"role":"assistant","content":null,"tool_calls":[' +
+ '{"id":"call_0","type":"function","function":{"name":"get_weather","arguments":"{\\"city\\":\\"Paris\\"}"}},' +
+ '{"id":"call_0","type":"function","function":{"name":"get_weather","arguments":"{\\"city\\":\\"Tokyo\\"}"}}]}',
+ '{"role":"tool","tool_call_id":"call_0","name":"get_weather","content":"PARIS"}',
+ '{"role":"tool","tool_call_id":"call_0","name":"get_weather","content":"TOKYO"}',
+ ].join("\n"),
+ "conversation-messages.jsonl",
+ );
+
+ assert.deepEqual(toolCallResults(conversation), ["PARIS", "TOKYO"]);
+});
+
+test("out-of-order tool results with unique ids still reach their call", () => {
+ const [conversation] = parseImportText(
+ '{"role":"tool","tool_call_id":"call_early","name":"lookup","content":"EARLY"}\n' +
+ '{"role":"assistant","content":null,"tool_calls":[' +
+ '{"id":"call_early","type":"function","function":{"name":"lookup","arguments":"{}"}},' +
+ '{"id":"call_late","type":"function","function":{"name":"lookup","arguments":"{}"}}]}\n' +
+ '{"role":"assistant","content":"Waiting"}\n' +
+ '{"role":"user","content":"Any news?"}\n' +
+ '{"role":"tool","tool_call_id":"call_late","name":"lookup","content":"LATE"}',
+ "conversation-messages.jsonl",
+ );
+
+ assert.deepEqual(toolCallResults(conversation), ["EARLY", "LATE"]);
+});
+
test("assistant images are represented explicitly in JSONL exports", () => {
const exported = structuredClone(
messageToOpenAI({