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: read more chat export shapes without losing turns (#16971)

DimitriChrysafis3fc613e2a254
diff --git a/studio/frontend/src/features/chat/utils/chat-import.ts b/studio/frontend/src/features/chat/utils/chat-import.ts
index 21b8dc5c1b0..105c772619c 100644
--- a/studio/frontend/src/features/chat/utils/chat-import.ts
+++ b/studio/frontend/src/features/chat/utils/chat-import.ts
@@ -116,17 +116,25 @@ function oaiContentToParts(raw: unknown): unknown[] {
return raw.flatMap((value): unknown[] => {
if (typeof value !== "object" || value === null) return [];
const part = value as Record<string, unknown>;
- if (part.type === "text" && typeof part.text === "string") {
+ if (
+ (part.type === "text" ||
+ part.type === "input_text" ||
+ part.type === "output_text") &&
+ typeof part.text === "string"
+ ) {
return [{ type: "text", text: part.text }];
}
if (part.type === "refusal" && typeof part.refusal === "string") {
return [{ type: "text", text: part.refusal }];
}
- if (part.type === "image_url") {
+ if (part.type === "image_url" || part.type === "input_image") {
+ // Chat Completions nests {image_url:{url}}; Responses input_image is a string.
const imageUrl =
- typeof part.image_url === "object" && part.image_url !== null
- ? (part.image_url as Record<string, unknown>).url
- : undefined;
+ typeof part.image_url === "string"
+ ? part.image_url
+ : typeof part.image_url === "object" && part.image_url !== null
+ ? (part.image_url as Record<string, unknown>).url
+ : undefined;
return typeof imageUrl === "string" && imageUrl
? [{ type: "image", image: imageUrl }]
: [];
@@ -140,19 +148,22 @@ function oaiMessagesToRecords(
threadId: string,
baseTs: number,
): MessageRecord[] {
- const toolResults = new Map<string, string>();
- const pending: Array<{ turn: number; part: { toolCallId: string; result?: string } }> = [];
+ const toolResults = new Map<string, unknown>();
+ const pending: Array<{ turn: number; part: { toolCallId: string; result?: unknown } }> = [];
const records: MessageRecord[] = [];
let prevId: string | null = null;
let idx = 0;
for (const m of oaiMsgs) {
+ if (typeof m !== "object" || m === null) continue;
const msg = m as Record<string, unknown>;
const role = msg.role as string;
if (role === "tool") {
if (typeof msg.tool_call_id !== "string") continue;
- const result = typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content ?? "");
+ const result =
+ (msg as { [STRUCTURED_RESULT]?: unknown })[STRUCTURED_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) {
@@ -173,6 +184,7 @@ function oaiMessagesToRecords(
if (role === "assistant") {
const parts = oaiContentToParts(msg.content);
if (parts.length === 0) parts.push(...oaiContentToParts(msg.refusal));
+ const callParts: unknown[] = [];
if (Array.isArray(msg.tool_calls)) {
for (const tc of msg.tool_calls) {
const tcObj = tc as Record<string, unknown>;
@@ -190,11 +202,16 @@ function oaiMessagesToRecords(
args,
argsText: argsStr,
};
- parts.push(part);
+ callParts.push(part);
pending.push({ turn: idx, part });
}
}
- content = parts;
+ const order = (msg as { [ITEM_ORDER]?: unknown[] })[ITEM_ORDER];
+ content = order
+ ? order.flatMap((item) =>
+ typeof item === "number" ? callParts.slice(item, item + 1) : oaiContentToParts([item]),
+ )
+ : [...parts, ...callParts];
} else {
content = oaiContentToParts(msg.content);
}
@@ -225,8 +242,11 @@ function oaiMessagesToRecords(
const SHAREGPT_ROLES = new Map<string, MessageRecord["role"]>([
["human", "user"],
["user", "user"],
+ ["input", "user"],
["gpt", "assistant"],
["assistant", "assistant"],
+ ["output", "assistant"],
+ ["developer", "system"],
["system", "system"],
]);
@@ -239,11 +259,24 @@ function sharegptToRecords(
let prevId: string | null = null;
let idx = 0;
for (const c of conversations) {
+ if (typeof c !== "object" || c === null) continue;
const conv = c as Record<string, unknown>;
- const from = typeof conv.from === "string" ? conv.from : "";
- const value = typeof conv.value === "string" ? conv.value : "";
+ // role/content beats from/value (backend formatter order); an unmapped role key is skipped, not mislabeled.
+ const roleKey =
+ typeof conv.role === "string" && conv.role.trim() ? conv.role : "";
+ const from =
+ roleKey || (typeof conv.from === "string" ? conv.from : "");
+ const value =
+ typeof conv.content === "string" && conv.content.trim()
+ ? conv.content
+ : typeof conv.value === "string"
+ ? conv.value
+ : "";
if (!value.trim()) continue;
- const role = SHAREGPT_ROLES.get(from.trim().toLowerCase()) ?? "assistant";
+ const role =
+ SHAREGPT_ROLES.get(from.trim().toLowerCase()) ??
+ (roleKey ? null : "assistant");
+ if (role === null) continue;
const id = crypto.randomUUID();
records.push({
id,
@@ -322,6 +355,197 @@ function csvToRecords(csvText: string, threadId: string, baseTs: number): Messag
return records;
}
+// Alpaca input follows the instruction, as in the backend's dataset formatter.
+function pairToTurns(obj: Record<string, unknown>): unknown[] {
+ const isAlpaca =
+ typeof obj.instruction === "string" && typeof obj.output === "string";
+ const isPair =
+ typeof obj.prompt === "string" && typeof obj.completion === "string";
+ if (!isAlpaca && !isPair) return [];
+ // Trim only to test emptiness: completion prompts can end in meaningful whitespace.
+ const input = typeof obj.input === "string" && obj.input.trim() ? obj.input : "";
+ const userText = isAlpaca
+ ? input ? `${obj.instruction}\n\n${input}` : (obj.instruction as string)
+ : (obj.prompt as string);
+ const assistantText = (isAlpaca ? obj.output : obj.completion) as string;
+ if (!userText.trim() || !assistantText.trim()) return [];
+ return [
+ ...(typeof obj.system === "string" ? [{ role: "system", content: obj.system }] : []),
+ { role: "user", content: userText },
+ { role: "assistant", content: assistantText },
+ ];
+}
+
+// Live parser's {text, images} tool result; symbol-keyed so imported JSON cannot set it.
+const STRUCTURED_RESULT = Symbol("structuredResult");
+// Item order of a merged Responses assistant run: content parts and tool_calls indices.
+const ITEM_ORDER = Symbol("itemOrder");
+
+function toolTurn(callId: string, output: unknown): Record<string | symbol, unknown> {
+ if (typeof output === "string") return { role: "tool", tool_call_id: callId, content: output };
+ const parts = (Array.isArray(output) ? output : typeof output === "object" && output !== null ? [output] : []) as Array<Record<string, unknown> | null>;
+ const texts: string[] = [];
+ const images: { data: string; mimeType: string }[] = [];
+ const known = parts.length > 0 && parts.every((part) => {
+ const source = part?.image_url ?? part?.url;
+ const url =
+ typeof source === "string" ? source : (source as { url?: unknown } | undefined)?.url;
+ const value = typeof part?.text === "string" ? part.text : part?.logs;
+ if (typeof value === "string") texts.push(value);
+ else if (typeof url !== "string") return false;
+ else {
+ const inline = /^data:([^;,]+);base64,(.*)$/s.exec(url);
+ if (inline) images.push({ mimeType: inline[1], data: inline[2] });
+ else texts.push(url);
+ }
+ return true;
+ });
+ const text = known ? texts.join("\n") : JSON.stringify(output ?? "");
+ return {
+ role: "tool",
+ tool_call_id: callId,
+ content: text,
+ ...(images.length > 0 ? { [STRUCTURED_RESULT]: { text, images } } : {}),
+ };
+}
+
+// Studio's names for provider-run Responses tools (chat-adapter SERVER_SIDE_BUILTIN_TOOL_NAMES).
+const BUILTIN_TOOL_NAMES: Record<string, string> = {
+ web_search_call: "web_search",
+ code_interpreter_call: "code_execution",
+ shell_call: "code_execution",
+};
+// Executed by the client, so neither hosted nor answered on its own.
+const CLIENT_RUN_CALLS = new Set(["function_call", "custom_tool_call", "local_shell_call", "computer_call"]);
+
+// Adjacent assistant items share one turn, as in the backend's _normalise_responses_input.
+function responsesToTurns(obj: Record<string, unknown>): unknown[] {
+ // A lone typed item (a raw function_call_output line) is not a transcript.
+ if (typeof obj.type === "string") return [];
+ const turns: Record<string | symbol, unknown>[] = [];
+ const built = new Set<Record<string | symbol, unknown>>();
+ type Built = { content: unknown[]; tool_calls: unknown[]; [ITEM_ORDER]: unknown[] };
+ const assistantTurn = (): Built => {
+ const last = turns[turns.length - 1];
+ if (last && built.has(last)) return last as unknown as Built;
+ const turn = { role: "assistant", content: [], tool_calls: [], [ITEM_ORDER]: [] };
+ built.add(turn);
+ turns.push(turn);
+ return turn;
+ };
+ const addContent = (...parts: unknown[]) => {
+ const turn = assistantTurn();
+ turn.content.push(...parts);
+ turn[ITEM_ORDER].push(...parts);
+ };
+ // Hosted results flush after the run: mid-run they split it and replay prunes the tool-only half.
+ const hosted = new Set<string>();
+ const deferred: Record<string | symbol, unknown>[] = [];
+ const push = (turn: Record<string | symbol, unknown>) => turns.push(...deferred.splice(0), turn);
+ const text = (value: unknown) =>
+ typeof value === "string" && value.trim() ? value : undefined;
+ const instructions = text(obj.instructions);
+ if (instructions) turns.push({ role: "system", content: instructions });
+ const input = text(obj.input);
+ if (input) turns.push({ role: "user", content: input });
+ const items = [obj.input, obj.output].flatMap((list) =>
+ Array.isArray(list)
+ ? list.filter((item): item is Record<string, unknown> => typeof item === "object" && item !== null)
+ : [],
+ );
+ const answered = new Set(
+ items.flatMap((item) =>
+ typeof item.type === "string" && item.type.endsWith("_call_output") ? [item.call_id ?? item.id] : [],
+ ),
+ );
+ for (const entry of items) {
+ const type = typeof entry.type === "string" ? entry.type : "";
+ if (entry.role === "assistant") {
+ addContent(
+ ...(Array.isArray(entry.content)
+ ? entry.content
+ : text(entry.content)
+ ? [{ type: "text", text: entry.content }]
+ : []),
+ );
+ } else if (typeof entry.role === "string") {
+ push(entry);
+ } else if (type === "image_generation_call") {
+ const encoded = text(entry.result) ?? text(entry.b64_json);
+ if (encoded) {
+ addContent({
+ type: "image_url",
+ image_url: /^(?:https?:|data:)/i.test(encoded)
+ ? encoded
+ : `data:image/${text(entry.output_format) ?? "png"};base64,${encoded}`,
+ });
+ }
+ } else if (type.endsWith("_call")) {
+ const id = text(entry.call_id) ?? text(entry.id) ?? crypto.randomUUID();
+ const builtin = !CLIENT_RUN_CALLS.has(type);
+ if (builtin) hosted.add(id);
+ const commands = (entry.action as { commands?: unknown } | undefined)?.commands;
+ const name =
+ BUILTIN_TOOL_NAMES[type] ??
+ text(entry.name) ??
+ (type === "function_call" || type === "custom_tool_call" ? "unknown" : type.replace(/_call$/, ""));
+ // mcp_call names its server in server_label.
+ const namespace = text(entry.namespace) ?? text(entry.server_label);
+ // Hosted tools use the shapes Studio's own cards render (code_execution reads command).
+ const payload =
+ typeof entry.code === "string"
+ ? { command: entry.code }
+ : type === "shell_call" && Array.isArray(commands)
+ ? { kind: "bash", command: commands.join("\n") }
+ : Array.isArray(entry.queries)
+ ? { queries: entry.queries }
+ : entry.input !== undefined
+ ? { input: entry.input }
+ : (entry.arguments ?? entry.action ?? {});
+ const turn = assistantTurn();
+ turn[ITEM_ORDER].push(turn.tool_calls.length);
+ turn.tool_calls.push({
+ id,
+ type: "function",
+ function: {
+ // namespace__name and {input} wrapping match the backend.
+ name: namespace ? `${namespace}__${name}` : name,
+ arguments:
+ typeof entry.arguments === "string"
+ ? entry.arguments
+ : JSON.stringify(
+ // Marked like Studio's own hosted-tool cards, so replay strips them.
+ builtin && typeof payload === "object" && payload !== null
+ ? { ...payload, _server_tool: true }
+ : payload,
+ ),
+ },
+ });
+ const done =
+ entry.output ??
+ (Array.isArray(entry.outputs) && entry.outputs.length > 0 ? entry.outputs : undefined) ??
+ (Array.isArray(entry.results) && entry.results.length > 0 ? entry.results : undefined) ??
+ entry.error ??
+ (builtin && !answered.has(id) ? (text(entry.status) ?? "completed") : undefined);
+ if (done != null) {
+ if (builtin) deferred.push(toolTurn(id, done));
+ else push(toolTurn(id, done));
+ }
+ } else if (type.endsWith("_call_output")) {
+ // local_shell_call_output names its call in id rather than call_id.
+ const callId = text(entry.call_id) ?? text(entry.id);
+ if (callId === undefined) continue;
+ const turn = toolTurn(callId, entry.output);
+ if (hosted.has(callId)) deferred.push(turn);
+ else push(turn);
+ }
+ }
+ const output = text(obj.output);
+ if (output) addContent({ type: "text", text: output });
+ turns.push(...deferred);
+ return turns;
+}
+
export function recordToConversation(
record: unknown,
fallbackTitle: string,
@@ -336,14 +560,31 @@ export function recordToConversation(
// Fresh ID: reusing the exported thread_id would clobber an existing thread.
const threadId = crypto.randomUUID();
const title = typeof obj.title === "string" ? obj.title : fallbackTitle;
- const baseTs = typeof obj.created_at === "number" ? obj.created_at : Date.now();
+ // Saved Response objects stamp created_at in seconds; chats use ms.
+ const createdAt = typeof obj.created_at === "number" ? obj.created_at : NaN;
+ const baseTs = Number.isNaN(createdAt)
+ ? Date.now()
+ : createdAt < 1e12
+ ? createdAt * 1000
+ : createdAt;
let messages: MessageRecord[] = [];
- if (Array.isArray(obj.messages)) {
+ if (Array.isArray(obj.messages) && obj.messages.length > 0) {
messages = oaiMessagesToRecords(obj.messages, threadId, baseTs);
- } else if (Array.isArray(obj.conversations)) {
+ }
+ if (
+ messages.length === 0 &&
+ Array.isArray(obj.conversations) &&
+ obj.conversations.length > 0
+ ) {
messages = sharegptToRecords(obj.conversations, threadId, baseTs);
}
+ if (messages.length === 0) {
+ messages = oaiMessagesToRecords(pairToTurns(obj), threadId, baseTs);
+ }
+ if (messages.length === 0) {
+ messages = oaiMessagesToRecords(responsesToTurns(obj), threadId, baseTs);
+ }
if (messages.length === 0) return null;
return { title, threadId, messages };
diff --git a/studio/frontend/tests/message-jsonl-import.test.ts b/studio/frontend/tests/message-jsonl-import.test.ts
index a731725597e..c7b4fc64af6 100644
--- a/studio/frontend/tests/message-jsonl-import.test.ts
+++ b/studio/frontend/tests/message-jsonl-import.test.ts
@@ -160,6 +160,182 @@ test("assistant text takes precedence over refusal metadata on import", () => {
assert.deepEqual(conversation.messages[0].content, [{ type: "text", text: "An explanation." }]);
});
+test("Responses API part types import instead of dropping every message", () => {
+ const image = "data:image/png;base64,QUFBQQ==";
+ const [conversation] = parseImportText(
+ JSON.stringify({
+ messages: [
+ { role: "user", content: [{ type: "input_text", text: "Hi" }] },
+ {
+ role: "assistant",
+ content: [
+ { type: "output_text", text: "Hello" },
+ { type: "input_image", image_url: image },
+ ],
+ },
+ ],
+ }),
+ "chat.jsonl",
+ );
+
+ assert.deepEqual(
+ conversation.messages.map(({ role }) => role),
+ ["user", "assistant"],
+ );
+ assert.deepEqual(conversation.messages[0].content, [{ type: "text", text: "Hi" }]);
+ assert.deepEqual(conversation.messages[1].content, [
+ { type: "text", text: "Hello" },
+ { type: "image", image },
+ ]);
+});
+
+test("a verbatim Responses API object imports its input and output turns", () => {
+ const [conversation] = parseImportText(
+ JSON.stringify({
+ object: "response",
+ input: [
+ { type: "message", role: "user", content: [{ type: "input_text", text: "Hi" }] },
+ ],
+ output: [
+ { type: "reasoning", summary: [] },
+ {
+ type: "message",
+ role: "assistant",
+ content: [{ type: "output_text", text: "Hello" }],
+ },
+ ],
+ }),
+ "response.jsonl",
+ );
+
+ assert.deepEqual(
+ conversation.messages.map(({ role }) => role),
+ ["user", "assistant"],
+ );
+ assert.deepEqual(conversation.messages[1].content, [{ type: "text", text: "Hello" }]);
+});
+
+test("a Responses object keeps tool calls and reads created_at seconds", () => {
+ const [conversation] = parseImportText(
+ JSON.stringify({
+ object: "response",
+ created_at: 1740000000,
+ output: [
+ {
+ type: "function_call",
+ call_id: "call_1",
+ name: "get_time",
+ arguments: { zone: "utc" },
+ },
+ { type: "function_call_output", call_id: "call_1", output: "noon" },
+ {
+ type: "message",
+ role: "assistant",
+ content: [{ type: "output_text", text: "It is noon" }],
+ },
+ ],
+ }),
+ "response.jsonl",
+ );
+
+ assert.deepEqual(
+ conversation.messages.map(({ role }) => role),
+ ["assistant", "assistant"],
+ );
+ const toolCall = conversation.messages[0].content[0] as {
+ type: string;
+ result?: string;
+ args?: unknown;
+ };
+ assert.equal(toolCall.type, "tool-call");
+ assert.equal(toolCall.result, "noon");
+ assert.deepEqual(toolCall.args, { zone: "utc" });
+ assert.ok(
+ conversation.messages[0].createdAt > 1e12,
+ "created_at seconds must be converted to milliseconds",
+ );
+});
+
+test("mixed input messages and a string output import the answer too", () => {
+ const [conversation] = parseImportText(
+ JSON.stringify({
+ input: [{ role: "user", content: "Q" }],
+ output: "A",
+ }),
+ "eval.jsonl",
+ );
+
+ assert.deepEqual(
+ conversation.messages.map(({ role }) => role),
+ ["user", "assistant"],
+ );
+});
+
+test("an empty messages array does not shadow a response output", () => {
+ const [conversation] = parseImportText(
+ JSON.stringify({
+ messages: [],
+ output: [
+ {
+ type: "message",
+ role: "assistant",
+ content: [{ type: "output_text", text: "Hello" }],
+ },
+ ],
+ }),
+ "response.jsonl",
+ );
+
+ assert.equal(conversation.messages.length, 1);
+ assert.equal(conversation.messages[0].role, "assistant");
+ assert.deepEqual(conversation.messages[0].content, [
+ { type: "text", text: "Hello" },
+ ]);
+});
+
+test("alpaca and prompt-completion records import as user/assistant turns", () => {
+ for (const [record, expectedUser] of [
+ [
+ { instruction: "Say hi", input: "context", output: "Hello" },
+ "Say hi\n\ncontext",
+ ],
+ [{ instruction: "Say hi", output: "Hello" }, "Say hi"],
+ [{ prompt: "Q", completion: "A" }, "Q"],
+ ] as const) {
+ const [conversation] = parseImportText(
+ JSON.stringify(record),
+ "pairs.jsonl",
+ );
+ assert.deepEqual(
+ conversation.messages.map(({ role }) => role),
+ ["user", "assistant"],
+ );
+ assert.deepEqual(conversation.messages[0].content, [
+ { type: "text", text: expectedUser },
+ ]);
+ }
+});
+
+test("ShareGPT import maps input/output roles and ChatML-keyed entries", () => {
+ const [conversation] = parseImportText(
+ JSON.stringify({
+ conversations: [
+ { from: "input", value: "Hi" },
+ { from: "output", value: "Hello" },
+ { role: "developer", content: "Be precise" },
+ { role: "user", content: "Again" },
+ { role: "assistant", content: "Sure" },
+ ],
+ }),
+ "sharegpt.jsonl",
+ );
+
+ assert.deepEqual(
+ conversation.messages.map(({ role }) => role),
+ ["user", "assistant", "system", "user", "assistant"],
+ );
+});
+
test("ShareGPT import maps role aliases regardless of case and whitespace", () => {
const [conversation] = parseImportText(
JSON.stringify({
@@ -182,6 +358,41 @@ test("ShareGPT import maps role aliases regardless of case and whitespace", () =
);
});
+test("ShareGPT import prefers non-empty role/content over empty from/value", () => {
+ const [conversation] = parseImportText(
+ JSON.stringify({
+ conversations: [
+ { from: "human", role: "", value: "Hi", content: "" },
+ { from: "", role: "gpt", value: "", content: "Hello" },
+ { role: "tool", content: "tool result" },
+ ],
+ }),
+ "sharegpt.jsonl",
+ );
+
+ assert.deepEqual(
+ conversation.messages.map(({ role, content }) => ({
+ role,
+ text: (content as ReadonlyArray<{ text: string }>)[0]?.text,
+ })),
+ [
+ { role: "user", text: "Hi" },
+ { role: "assistant", text: "Hello" },
+ ],
+ );
+});
+
+test("null entries in an import record are skipped, not fatal", () => {
+ for (const record of [
+ { conversations: [null, { from: "gpt", value: "Hello" }] },
+ { messages: [null, { role: "user", content: "Hi" }] },
+ ]) {
+ const [conversation] = parseImportText(JSON.stringify(record), "chat.jsonl");
+
+ assert.equal(conversation.messages.length, 1);
+ }
+});
+
function toolCallResults(conversation: ParsedConversation): string[] {
return conversation.messages.flatMap((message) =>
(message.content as ReadonlyArray<{ type: string; result?: unknown }>)
@@ -417,3 +628,343 @@ test("imports without message send times mark their ordering timestamps as estim
assert.equal(conversation.messages[0].metadata?.createdAtEstimated, true, filename);
}
});
+
+test("a Responses object keeps its instructions and custom tool call input", () => {
+ const [conversation] = parseImportText(
+ JSON.stringify({
+ object: "response",
+ instructions: "Answer tersely.",
+ input: [
+ { role: "user", content: [{ type: "input_text", text: "Patch it" }] },
+ { type: "custom_tool_call", call_id: "c1", name: "apply_patch", input: "*** Begin Patch" },
+ { type: "custom_tool_call_output", call_id: "c1", output: "Done" },
+ ],
+ output: [
+ { type: "message", role: "assistant", content: [{ type: "output_text", text: "Patched." }] },
+ ],
+ }),
+ "response.json",
+ );
+
+ assert.equal(conversation.messages[0].role, "system");
+ assert.deepEqual(conversation.messages[0].content, [{ type: "text", text: "Answer tersely." }]);
+ const call = conversation.messages
+ .flatMap((m) => m.content as ReadonlyArray<{ type?: string; toolName?: string; args?: unknown; result?: unknown }>)
+ .find((p) => p.type === "tool-call");
+ assert.equal(call?.toolName, "apply_patch");
+ assert.deepEqual(call?.args, { input: "*** Begin Patch" });
+ assert.equal(call?.result, "Done");
+});
+
+test("a Responses object keeps parallel calls in one turn, namespaces and generated images", () => {
+ const [conversation] = parseImportText(
+ JSON.stringify({
+ object: "response",
+ input: [
+ { role: "user", content: "Weather and a picture?" },
+ { type: "function_call", call_id: "a", namespace: "mcp__weather", name: "get_forecast", arguments: "{}" },
+ { type: "function_call", call_id: "b", name: "get_time", arguments: "{}" },
+ { type: "function_call_output", call_id: "a", output: "Sunny" },
+ { type: "function_call_output", call_id: "b", output: "Noon" },
+ ],
+ output: [{ type: "image_generation_call", id: "ig", result: "QUJD", output_format: "webp" }],
+ }),
+ "response.json",
+ );
+
+ const roles = conversation.messages.map((m) => m.role);
+ assert.deepEqual(roles, ["user", "assistant", "assistant"]);
+ const calls = conversation.messages[1].content as ReadonlyArray<{ toolName?: string; result?: unknown }>;
+ assert.deepEqual(
+ calls.map((p) => [p.toolName, p.result]),
+ [["mcp__weather__get_forecast", "Sunny"], ["get_time", "Noon"]],
+ );
+ assert.deepEqual(conversation.messages[2].content, [
+ { type: "image", image: "data:image/webp;base64,QUJD" },
+ ]);
+});
+
+test("alpaca system, text-part tool outputs and bundled call outputs survive import", () => {
+ const [alpaca] = parseImportText(
+ JSON.stringify({ system: "Be brief.", instruction: "Name a prime", input: "", output: "7" }),
+ "alpaca.jsonl",
+ );
+ assert.deepEqual(alpaca.messages.map((m) => m.role), ["system", "user", "assistant"]);
+
+ const [response] = parseImportText(
+ JSON.stringify({
+ object: "response",
+ input: [
+ { role: "user", content: "Run it" },
+ { type: "function_call", call_id: "f1", name: "read", arguments: "{}" },
+ { type: "function_call_output", call_id: "f1", output: [{ type: "input_text", text: "file body" }] },
+ { type: "shell_call", call_id: "s1", action: { commands: ["ls"] }, output: "a.txt" },
+ ],
+ output: [{ type: "message", role: "assistant", content: [{ type: "output_text", text: "Done." }] }],
+ }),
+ "response.json",
+ );
+ const calls = response.messages.flatMap(
+ (m) => m.content as ReadonlyArray<{ type?: string; result?: unknown }>,
+ ).filter((p) => p.type === "tool-call");
+ assert.deepEqual(calls.map((p) => p.result), ["file body", "a.txt"]);
+ assert.equal(response.messages.at(-1)?.role, "assistant");
+ assert.deepEqual((response.messages.at(-1)?.content as ReadonlyArray<unknown>).at(-1), { type: "text", text: "Done." });
+});
+
+test("a finished web_search_call imports as an answered call", () => {
+ const [conversation] = parseImportText(
+ JSON.stringify({
+ object: "response",
+ input: [{ role: "user", content: "News?" }],
+ output: [
+ { type: "web_search_call", id: "ws1", status: "completed", action: { type: "search", query: "news" } },
+ { type: "message", role: "assistant", content: [{ type: "output_text", text: "Here is the news." }] },
+ ],
+ }),
+ "response.json",
+ );
+
+ const call = conversation.messages
+ .flatMap((m) => m.content as ReadonlyArray<{ type?: string; toolName?: string; result?: unknown }>)
+ .find((p) => p.type === "tool-call");
+ assert.equal(call?.toolName, "web_search");
+ assert.equal(call?.result, "completed");
+ assert.deepEqual((conversation.messages.at(-1)?.content as ReadonlyArray<unknown>).at(-1), { type: "text", text: "Here is the news." });
+});
+
+test("a builtin call keeps its explicit output item over a status placeholder", () => {
+ const [conversation] = parseImportText(
+ JSON.stringify({
+ object: "response",
+ input: [
+ { role: "user", content: "List files" },
+ { type: "shell_call", call_id: "s1", status: "completed", action: { commands: ["ls"] } },
+ { type: "shell_call_output", call_id: "s1", output: "a.txt" },
+ ],
+ output: [{ type: "message", role: "assistant", content: [{ type: "output_text", text: "One file." }] }],
+ }),
+ "response.json",
+ );
+
+ const call = conversation.messages
+ .flatMap((m) => m.content as ReadonlyArray<{ type?: string; result?: unknown }>)
+ .find((p) => p.type === "tool-call");
+ assert.equal(call?.result, "a.txt");
+});
+
+test("hosted Responses calls import as server-side tool cards with their code and logs", () => {
+ const [conversation] = parseImportText(
+ JSON.stringify({
+ object: "response",
+ input: [{ role: "user", content: "Compute" }],
+ output: [
+ { type: "web_search_call", id: "ws1", status: "completed", action: { type: "search", query: "q" } },
+ { type: "code_interpreter_call", id: "ci1", status: "completed", code: "print(1)", outputs: [{ type: "logs", logs: "1" }] },
+ { type: "message", role: "assistant", content: [{ type: "output_text", text: "1" }] },
+ ],
+ }),
+ "response.json",
+ );
+
+ const calls = conversation.messages
+ .flatMap((m) => m.content as ReadonlyArray<{ type?: string; toolName?: string; args?: Record<string, unknown>; result?: unknown }>)
+ .filter((p) => p.type === "tool-call");
+ assert.deepEqual(
+ calls.map((p) => [p.toolName, p.args?._server_tool, p.result]),
+ [["web_search", true, "completed"], ["code_execution", true, "1"]],
+ );
+ assert.equal(calls[1].args?.command, "print(1)");
+});
+
+test("a Responses mcp_call keeps its server and tool name", () => {
+ const [conversation] = parseImportText(
+ JSON.stringify({
+ object: "response",
+ input: [{ role: "user", content: "Find docs" }],
+ output: [
+ { type: "mcp_call", id: "m1", server_label: "docs", name: "search_docs", arguments: "{\"q\":\"x\"}", output: "hit" },
+ { type: "message", role: "assistant", content: [{ type: "output_text", text: "Found." }] },
+ ],
+ }),
+ "response.json",
+ );
+
+ const call = conversation.messages
+ .flatMap((m) => m.content as ReadonlyArray<{ type?: string; toolName?: string; result?: unknown }>)
+ .find((p) => p.type === "tool-call");
+ assert.equal(call?.toolName, "docs__search_docs");
+ assert.equal(call?.result, "hit");
+});
+
+test("Responses shell calls: hosted ones render their command, client ones and MCP errors stay honest", () => {
+ const [conversation] = parseImportText(
+ JSON.stringify({
+ object: "response",
+ input: [
+ { role: "user", content: "Go" },
+ { type: "shell_call", call_id: "s1", status: "completed", action: { commands: ["ls", "pwd"] } },
+ { type: "local_shell_call", call_id: "l1", action: { type: "exec", command: ["ls"] } },
+ { type: "local_shell_call_output", call_id: "l1", output: "local.txt" },
+ { type: "mcp_call", id: "m1", server_label: "docs", name: "search", arguments: "{}", output: null, error: "Authentication failed" },
+ ],
+ }),
+ "response.json",
+ );
+
+ const calls = conversation.messages
+ .flatMap((m) => m.content as ReadonlyArray<{ type?: string; toolName?: string; args?: Record<string, unknown>; result?: unknown }>)
+ .filter((p) => p.type === "tool-call");
+ assert.deepEqual(calls[0].args, { kind: "bash", command: "ls\npwd", _server_tool: true });
+ assert.equal(calls[1].toolName, "local_shell");
+ assert.equal(calls[1].args?._server_tool, undefined);
+ assert.equal(calls[1].result, "local.txt");
+ assert.equal(calls[2].result, "Authentication failed");
+});
+
+test("images in Responses tool outputs and code interpreter outputs stay images", () => {
+ const [conversation] = parseImportText(
+ JSON.stringify({
+ object: "response",
+ input: [
+ { role: "user", content: "Plot" },
+ { type: "function_call", call_id: "f1", name: "snap", arguments: "{}" },
+ {
+ type: "function_call_output",
+ call_id: "f1",
+ output: [
+ { type: "input_text", text: "shot" },
+ { type: "input_image", image_url: "data:image/png;base64,QUJD" },
+ ],
+ },
+ ],
+ output: [
+ {
+ type: "code_interpreter_call",
+ id: "ci1",
+ code: "plot()",
+ outputs: [{ type: "logs", logs: "ok" }, { type: "image", url: "https://example.com/p.png" }],
+ },
+ ],
+ }),
+ "response.json",
+ );
+
+ const results = conversation.messages
+ .flatMap((m) => m.content as ReadonlyArray<{ type?: string; result?: unknown }>)
+ .filter((p) => p.type === "tool-call")
+ .map((p) => p.result);
+ assert.deepEqual(results, [
+ { text: "shot", images: [{ mimeType: "image/png", data: "QUJD" }] },
+ "ok\nhttps://example.com/p.png",
+ ]);
+});
+
+test("file search results and whitespace-sensitive prompts survive import", () => {
+ const [search] = parseImportText(
+ JSON.stringify({
+ object: "response",
+ input: [{ role: "user", content: "Find it" }],
+ output: [
+ { type: "file_search_call", id: "fs1", status: "completed", queries: ["refund policy"], results: [{ file_id: "f", filename: "p.md", text: "30 days" }] },
+ ],
+ }),
+ "response.json",
+ );
+ const call = search.messages
+ .flatMap((m) => m.content as ReadonlyArray<{ type?: string; args?: unknown; result?: unknown }>)
+ .find((p) => p.type === "tool-call");
+ assert.deepEqual(call?.args, { queries: ["refund policy"], _server_tool: true });
+ assert.equal(call?.result, "30 days");
+
+ const [pair] = parseImportText(
+ JSON.stringify({ prompt: "def f():\n ", completion: "return 1" }),
+ "pairs.jsonl",
+ );
+ assert.deepEqual(pair.messages[0].content, [{ type: "text", text: "def f():\n " }]);
+});
+
+test("hosted results do not split the assistant run they belong to", () => {
+ const [conversation] = parseImportText(
+ JSON.stringify({
+ object: "response",
+ input: [{ role: "user", content: "News?" }],
+ output: [
+ { type: "web_search_call", id: "ws1", status: "completed", action: { type: "search", query: "news" } },
+ { type: "shell_call", call_id: "s1", action: { commands: ["date"] } },
+ { type: "shell_call_output", call_id: "s1", output: "Monday" },
+ { type: "message", role: "assistant", content: [{ type: "output_text", text: "Here is the news." }] },
+ ],
+ }),
+ "response.json",
+ );
+
+ assert.deepEqual(conversation.messages.map((m) => m.role), ["user", "assistant"]);
+ const parts = conversation.messages[1].content as ReadonlyArray<{ type?: string; text?: string; result?: unknown }>;
+ assert.deepEqual(
+ parts.map((p) => (p.type === "tool-call" ? p.result : p.text)),
+ ["completed", "Monday", "Here is the news."],
+ );
+});
+
+test("string output, id-keyed local shell output and lone screenshots import intact", () => {
+ const [conversation] = parseImportText(
+ JSON.stringify({
+ object: "response",
+ input: [
+ { role: "user", content: "Check" },
+ { type: "local_shell_call", id: "lsc1", call_id: "l1", action: { type: "exec", command: ["date"] } },
+ { type: "local_shell_call_output", id: "l1", output: "Monday" },
+ { type: "computer_call", call_id: "c1", action: { type: "screenshot" } },
+ { type: "computer_call_output", call_id: "c1", output: { type: "computer_screenshot", image_url: "data:image/png;base64,QUJD" } },
+ { type: "web_search_call", id: "ws1", status: "completed", action: { type: "search", query: "q" } },
+ ],
+ output: "All good.",
+ }),
+ "response.json",
+ );
+
+ const results = conversation.messages
+ .flatMap((m) => m.content as ReadonlyArray<{ type?: string; result?: unknown }>)
+ .filter((p) => p.type === "tool-call")
+ .map((p) => p.result);
+ assert.deepEqual(results, ["Monday", { text: "", images: [{ mimeType: "image/png", data: "QUJD" }] }, "completed"]);
+ const last = conversation.messages.at(-1);
+ assert.equal(last?.role, "assistant");
+ assert.ok((last?.content as ReadonlyArray<{ type?: string }>).some((p) => p.type === "tool-call"));
+});
+
+test("raw Responses item lines do not import as standalone answers", () => {
+ const conversations = parseImportText(
+ [
+ { type: "function_call", call_id: "c", name: "add", arguments: "{}" },
+ { type: "function_call_output", call_id: "c", output: "8" },
+ ].map((item) => JSON.stringify(item)).join("\n"),
+ "items.jsonl",
+ );
+
+ assert.deepEqual(conversations, []);
+});
+
+test("a hosted call before its answer exports in that order", () => {
+ const [conversation] = parseImportText(
+ JSON.stringify({
+ object: "response",
+ input: [{ role: "user", content: "Compute" }],
+ output: [
+ { type: "code_interpreter_call", id: "ci1", code: "print(2)", outputs: [{ type: "logs", logs: "2" }] },
+ { type: "message", role: "assistant", content: [{ type: "output_text", text: "It printed 2." }] },
+ ],
+ }),
+ "response.json",
+ );
+
+ const exported = messageToOpenAI({
+ role: "assistant",
+ content: conversation.messages[1].content,
+ }) as Array<{ role: string; content?: unknown }>;
+ const answerAt = exported.findIndex(
+ (m) => m.role === "assistant" && JSON.stringify(m.content ?? "").includes("It printed 2."),
+ );
+ assert.equal(answerAt, exported.length - 1);
+});