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.

fix(studio): mark CSV exports as UTF-8 so Excel shows non-Latin text (#11971)

Tom Mathews8fdeb635f8a8
diff --git a/studio/frontend/src/features/chat/prompt-storage/prompt-storage-dialog.tsx b/studio/frontend/src/features/chat/prompt-storage/prompt-storage-dialog.tsx
index 918e11af125..b2419ef75f8 100644
--- a/studio/frontend/src/features/chat/prompt-storage/prompt-storage-dialog.tsx
+++ b/studio/frontend/src/features/chat/prompt-storage/prompt-storage-dialog.tsx
@@ -84,6 +84,7 @@ import {
createConversationMarkdownBuilder,
createConversationMarkdownExporter,
} from "../utils/conversation-markdown-export";
+import { csvDocument, csvEscape, CSV_MIME } from "../utils/csv-export";
import { parseCsv } from "../utils/csv-parse";
import {
canMergeConversationExport,
@@ -124,10 +125,6 @@ async function downloadBlob(
return downloadFile(content, filename, mimeType);
}
-function csvEscape(val: string): string {
- return `"${val.replace(/"/g, '""')}"`;
-}
-
function exportPromptJsonl(entry: PromptEntry): Promise<void> {
return downloadBlob(
ndjsonBody([JSON.stringify({ name: entry.name, text: entry.text })]),
@@ -138,9 +135,9 @@ function exportPromptJsonl(entry: PromptEntry): Promise<void> {
function exportPromptCsv(entry: PromptEntry): Promise<void> {
return downloadBlob(
- `name,text\n${csvEscape(entry.name)},${csvEscape(entry.text)}`,
+ csvDocument(["name,text", `${csvEscape(entry.name)},${csvEscape(entry.text)}`]),
`${sanitizeFilename(entry.name)}.csv`,
- "text/csv",
+ CSV_MIME,
);
}
@@ -151,7 +148,7 @@ function exportAllPromptsJsonl(entries: PromptEntry[]): Promise<void> {
function exportAllPromptsCsv(entries: PromptEntry[]): Promise<void> {
const rows = entries.map((e) => `${csvEscape(e.name)},${csvEscape(e.text)}`).join("\n");
- return downloadBlob(`name,text\n${rows}`, "prompts.csv", "text/csv");
+ return downloadBlob(csvDocument(["name,text", rows]), "prompts.csv", CSV_MIME);
}
function exportListJsonl(entry: PromptListEntry): Promise<void> {
@@ -172,9 +169,9 @@ function exportListCsv(entry: PromptListEntry): Promise<void> {
.map((text, i) => `${csvEscape(entry.name)},${i + 1},${csvEscape(text)}`)
.join("\n");
return downloadBlob(
- `list_name,order,prompt_text\n${rows}`,
+ csvDocument(["list_name,order,prompt_text", rows]),
`${sanitizeFilename(entry.name)}.csv`,
- "text/csv",
+ CSV_MIME,
);
}
@@ -182,7 +179,7 @@ function exportAllListsCsv(entries: PromptListEntry[]): Promise<void> {
const rows = entries
.flatMap((e) => e.items.map((text, i) => `${csvEscape(e.name)},${i + 1},${csvEscape(text)}`))
.join("\n");
- return downloadBlob(`list_name,order,prompt_text\n${rows}`, "prompt-lists.csv", "text/csv");
+ return downloadBlob(csvDocument(["list_name,order,prompt_text", rows]), "prompt-lists.csv", CSV_MIME);
}
function contentBlocksToText(content: unknown): string {
@@ -461,9 +458,9 @@ export async function exportConversationCsv(threadId: string): Promise<void> {
if (rows.length <= 1) { toast.info("No exportable content."); return; }
await downloadBlob(
- rows.join("\n"),
+ csvDocument(rows),
"conversation-" + exportTs() + ".csv",
- "text/csv",
+ CSV_MIME,
);
}
@@ -622,7 +619,7 @@ function exportExt(format: ConvExportFormat): string {
}
function exportMime(format: ConvExportFormat): string {
- return format === "csv" ? "text/csv" : "application/x-ndjson";
+ return format === "csv" ? CSV_MIME : "application/x-ndjson";
}
export async function exportBulkConversationsMerged(
@@ -647,7 +644,7 @@ export async function exportBulkConversationsMerged(
if (parts.length === 0) { toast.info("No exportable content."); return; }
const body = header
- ? header + "\n" + parts.join("\n")
+ ? csvDocument([header, ...parts])
: ndjsonBody(parts);
await downloadBlob(
@@ -672,7 +669,7 @@ export async function exportBulkConversationsSeparate(
for (const id of threadIds) {
const content = await buildThreadContent(id, format);
if (!content) continue;
- const body = header ? header + "\n" + content : ndjsonBody([content]);
+ const body = header ? csvDocument([header, content]) : ndjsonBody([content]);
files[`${id}.${ext}`] = strToU8(body);
}
diff --git a/studio/frontend/src/features/chat/utils/csv-export.ts b/studio/frontend/src/features/chat/utils/csv-export.ts
new file mode 100644
index 00000000000..a748c80b3ac
--- /dev/null
+++ b/studio/frontend/src/features/chat/utils/csv-export.ts
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+export function csvEscape(val: string): string {
+ return `"${val.replace(/"/g, '""')}"`;
+}
+
+export const CSV_MIME = "text/csv;charset=utf-8";
+
+// Excel decodes a BOM-less .csv with the system code page (e.g. Windows-1252), garbling non-Latin1 text.
+export function csvDocument(lines: string[]): string {
+ return "\ufeff" + lines.join("\n");
+}
diff --git a/studio/frontend/src/features/chat/utils/csv-parse.ts b/studio/frontend/src/features/chat/utils/csv-parse.ts
index dc9f59162f5..895501b4205 100644
--- a/studio/frontend/src/features/chat/utils/csv-parse.ts
+++ b/studio/frontend/src/features/chat/utils/csv-parse.ts
@@ -3,6 +3,8 @@
// RFC 4180 CSV parser: handles quoted fields with embedded newlines/commas.
export function parseCsv(text: string): string[][] {
+ // Tauri text reads keep the BOM our exports write; File.text() strips it.
+ if (text.charCodeAt(0) === 0xfeff) text = text.slice(1);
const rows: string[][] = [];
let row: string[] = [];
let i = 0;
diff --git a/studio/frontend/tests/csv-export.test.ts b/studio/frontend/tests/csv-export.test.ts
new file mode 100644
index 00000000000..5338ef55717
--- /dev/null
+++ b/studio/frontend/tests/csv-export.test.ts
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import assert from "node:assert/strict";
+import test from "node:test";
+
+import { csvDocument, csvEscape } from "../src/features/chat/utils/csv-export.ts";
+import { parseCsv } from "../src/features/chat/utils/csv-parse.ts";
+
+test("csvDocument prefixes a UTF-8 byte-order mark", () => {
+ const doc = csvDocument(["a,b", "1,2"]);
+ assert.equal(doc.charCodeAt(0), 0xfeff);
+ assert.equal(doc, "\ufeffa,b\n1,2");
+});
+
+test("escaped rows survive a csvDocument/parseCsv round trip with non-Latin1 and multiline text", () => {
+ const header = ["name", "text"];
+ const rows = [
+ ["comma", "a,b"],
+ ["quotes", 'she said "hi"'],
+ ["crlf", "line one\r\nline two"],
+ ["lf", "line one\nline two"],
+ ["czech", "není potřeba"],
+ ["russian", "это"],
+ ["cjk", "日本語"],
+ ["emoji", "👍🏽"],
+ ];
+
+ const doc = csvDocument([
+ header.map(csvEscape).join(","),
+ ...rows.map((cells) => cells.map(csvEscape).join(",")),
+ ]);
+
+ assert.equal(doc.charCodeAt(0), 0xfeff);
+
+ const parsed = parseCsv(doc);
+ assert.deepEqual(parsed[0], header);
+ assert.deepEqual(parsed.slice(1), rows);
+});
+
+test("parseCsv returns identical rows with and without a leading BOM", () => {
+ const body = 'name,text\nczech,není potřeba\ncjk,日本語\nemoji,👍🏽';
+ const withBom = "\ufeff" + body;
+
+ assert.deepEqual(parseCsv(withBom), parseCsv(body));
+});
diff --git a/studio/frontend/tests/markdown-export-branches.test.ts b/studio/frontend/tests/markdown-export-branches.test.ts
index 18fd68e3c4d..a84a2c1b48e 100644
--- a/studio/frontend/tests/markdown-export-branches.test.ts
+++ b/studio/frontend/tests/markdown-export-branches.test.ts
@@ -12,6 +12,7 @@ import {
createConversationMarkdownExporter,
} from "../src/features/chat/utils/conversation-markdown-export.ts";
import { buildConversationMarkdown } from "../src/features/chat/utils/conversation-markdown.ts";
+import { csvDocument, CSV_MIME } from "../src/features/chat/utils/csv-export.ts";
import * as liveThreadHead from "../src/features/chat/utils/live-thread-head.ts";
import { orderByParentChain } from "../src/features/chat/utils/message-order.ts";
import { readSrc } from "./helpers/kit.ts";
@@ -98,6 +99,8 @@ function loadExporters(
messageToMarkdown: (message: StoredMessage) => message.content,
messageToText: (message: StoredMessage) => message.content,
csvEscape: (value: string) => value,
+ csvDocument,
+ CSV_MIME,
exportTs: () => "ts",
downloadBlob: async (body: string) => {
downloads.push(body);
@@ -183,7 +186,7 @@ test("CSV still writes both replies while markdown writes one", async () => {
const exporters = loadExporters(regenerated, downloads, []);
await exporters.exportConversationCsv("thread");
assert.deepEqual(downloads, [
- "role,content\nuser,Name one fruit.\nassistant,Pears.\nassistant,Apples.",
+ "\ufeffrole,content\nuser,Name one fruit.\nassistant,Pears.\nassistant,Apples.",
]);
});