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 emoji and rare CJK characters intact when an .xls record boundary splits them (#16713)

DimitriChrysafisb597d1e57622
diff --git a/studio/backend/core/rag/office_formats.py b/studio/backend/core/rag/office_formats.py
index 5ab70eda5cd..bfd37ca2793 100644
--- a/studio/backend/core/rag/office_formats.py
+++ b/studio/backend/core/rag/office_formats.py
@@ -1400,10 +1400,16 @@ def _xls_string(segments: list[bytes], seg: int, pos: int) -> tuple[str, int, in
n = min(count, max(avail, 1))
raw = segments[seg][pos : pos + n * width]
pos += n * width
- parts.append(raw.decode("utf-16-le" if wide else "latin-1", "replace"))
+ if not parts and n == count:
+ take(4 * runs + ext)
+ return raw.decode("utf-16-le" if wide else "latin-1", "replace"), seg, pos
+ if wide and len(raw) & 1:
+ # A corrupt record can end mid-character; one U+FFFD keeps the rest aligned.
+ raw = raw[:-1] + b"\xfd\xff"
+ parts.append(raw if wide else raw.decode("latin-1").encode("utf-16-le"))
count -= n
take(4 * runs + ext)
- return "".join(parts), seg, pos
+ return b"".join(parts).decode("utf-16-le", "replace"), seg, pos
def _xls_short_string(body: bytes, pos: int, length_bytes: int) -> str:
diff --git a/studio/backend/tests/test_rag_office_formats.py b/studio/backend/tests/test_rag_office_formats.py
index 31a7f9765bf..4e3b6976842 100644
--- a/studio/backend/tests/test_rag_office_formats.py
+++ b/studio/backend/tests/test_rag_office_formats.py
@@ -1012,6 +1012,121 @@ def test_xls_reads_continued_strings_numbers_formulas_and_dates(tmp_path):
)
+def _xls_continued_cell(
+ path,
+ fragments,
+ *,
+ formula = False,
+):
+ count = sum(len(raw) // (2 if wide else 1) for wide, raw in fragments)
+ first = struct.pack("<HB", count, fragments[0][0]) + fragments[0][1]
+ continued = b"".join(_record(0x003C, bytes([wide]) + raw) for wide, raw in fragments[1:])
+ bof = _record(0x0809, struct.pack("<HH", 0x0600, 0x0005) + b"\0" * 12)
+ boundsheet = _record(0x0085, b"\0" * 6 + b"\x04\0Text")
+ strings = b"" if formula else _record(0x00FC, struct.pack("<II", 1, 1) + first) + continued
+ globals_ = bof + boundsheet + strings + _record(0x000A, b"")
+ if formula:
+ reference = b"\x44" + struct.pack("<HH", 1, 0)
+ cell = _record(
+ 0x0006,
+ struct.pack("<HHH", 0, 0, 0)
+ + b"\0" * 6
+ + b"\xff\xff"
+ + b"\0" * 6
+ + struct.pack("<H", len(reference))
+ + reference,
+ )
+ cell += _record(0x0207, first) + continued
+ else:
+ cell = _record(0x00FD, struct.pack("<HHHI", 0, 0, 0, 0))
+ sheet = _record(0x0809, struct.pack("<HH", 0x0600, 0x0010) + b"\0" * 12)
+ stream = bytearray(globals_ + sheet + cell + _record(0x000A, b""))
+ struct.pack_into("<I", stream, len(bof) + 4, len(globals_))
+ path.write_bytes(compound_file({("Workbook",): bytes(stream)}))
+ return path
+
+
[email protected]("formula", [False, True], ids = ["shared", "formula"])
+ "fragments, expected",
+ [
+ ([(1, b"A\0=\xd8"), (1, b"\0\xdeB\0")], "A\U0001f600B"),
+ ([(1, b"A\0\x42\xd8"), (1, b"\xb7\xdf\xce\x91")], "A\U00020bb7\u91ce"),
+ (
+ [(0, b"Before "), (1, b"=\xd8"), (1, b"\0\xde"), (0, b" caf\xe9")],
+ "Before \U0001f600 caf\u00e9",
+ ),
+ ([(0, b"A"), (1, "\u6771\u4eac".encode("utf-16-le")), (0, b"B")], "A\u6771\u4eacB"),
+ ([(0, b"A"), (1, b"=\xd8")], "A\ufffd"),
+ ([(1, b"\0\xde"), (0, b"B")], "\ufffdB"),
+ ],
+ ids = ["emoji", "cjk", "width-switch", "bmp", "trailing-high", "leading-low"],
+)
+def test_xls_preserves_unicode_across_continuations(tmp_path, formula, fragments, expected):
+ path = _xls_continued_cell(tmp_path / "unicode.xls", fragments, formula = formula)
+ assert _text(path) == f"Sheet: Text\n{expected}"
+
+
[email protected]("formula", [False, True], ids = ["shared", "formula"])
+def test_xls_preserves_surrogates_at_the_full_record_boundary(tmp_path, formula):
+ prefix = 4109 if formula else 4105
+ text = "A" * prefix + "\U0001f600tail"
+ raw = text.encode("utf-16-le")
+ split = (prefix + 1) * 2
+ path = _xls_continued_cell(
+ tmp_path / "long.xls", [(1, raw[:split]), (1, raw[split:])], formula = formula
+ )
+ assert _text(path) == f"Sheet: Text\n{text}"
+
+
+def test_xls_does_not_pair_surrogates_from_separate_strings():
+ data = struct.pack("<HB", 2, 1) + b"A\0=\xd8"
+ data += struct.pack("<HB", 1, 1) + b"\0\xde"
+ first, seg, pos = office_formats._xls_string([data], 0, 0)
+ second, seg, pos = office_formats._xls_string([data], seg, pos)
+ assert (first, second, seg, pos) == ("A\ufffd", "\ufffd", 0, len(data))
+
+
+def test_xls_string_metadata_can_continue_without_a_width_flag():
+ first = struct.pack("<HBH", 1, 0x09, 1) + b"A\0"
+ second = struct.pack("<HH", 0, 0) + struct.pack("<HB", 1, 0) + b"B"
+ text, seg, pos = office_formats._xls_string([first, second], 0, 0)
+ following, seg, pos = office_formats._xls_string([first, second], seg, pos)
+ assert (text, following, seg, pos) == ("A", "B", 1, len(second))
+
+
+def test_xls_string_realigns_after_a_record_ends_mid_character():
+ first = struct.pack("<HB", 4, 1) + b"A\0B"
+ second = b"\x01" + "CD".encode("utf-16-le")
+ text, seg, pos = office_formats._xls_string([first, second], 0, 0)
+ assert (text, seg, pos) == ("A\ufffdCD", 1, len(second))
+
+
[email protected]("formula", [False, True], ids = ["shared", "formula"])
+def test_continued_xls_uploads_preserve_searchable_names(
+ rag_home, stub_embeddings, tmp_path, formula
+):
+ if not rag_db.rag_available():
+ pytest.skip("sqlite-vec unavailable here")
+ name = "\U00020bb7\u91ce"
+ fragments = [(1, "Person ".encode("utf-16-le") + b"\x42\xd8"), (1, b"\xb7\xdf\xce\x91")]
+ source = _xls_continued_cell(tmp_path / "names.xls", fragments, formula = formula)
+ stored, filename, _hash = _save_upload(
+ UploadFile(file = io.BytesIO(source.read_bytes()), filename = source.name)
+ )
+ scope = store.thread_scope("unicode-office")
+ doc_id, job = ingestion.start_ingestion(
+ scope, None, "unicode-office", filename, stored, background = False
+ )
+ assert ingestion.get_job_status(job)["status"] == "completed"
+ conn = rag_db.get_connection()
+ try:
+ assert store.get_document(conn, doc_id)["status"] == "completed"
+ assert store.search_lexical(conn, scope, name, 5)
+ finally:
+ conn.close()
+
+
def test_xls_refuses_a_workbook_past_the_cell_ceiling(tmp_path, monkeypatch):
monkeypatch.setattr(office_formats, "_MAX_XML_ELEMENTS", 3)
with pytest.raises(ValueError, match = "too many cells"):