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
Studio RAG: compile the docx run XPath once and remember style list lookups per document (#16343)
Daniel Han2c59e26472a2
diff --git a/studio/backend/core/rag/parsers.py b/studio/backend/core/rag/parsers.pyindex 9a0a3cafc6d..cc2c42c382f 100644--- a/studio/backend/core/rag/parsers.py+++ b/studio/backend/core/rag/parsers.py@@ -12,6 +12,7 @@ import osimport reimport secretsimport struct+import threadingfrom dataclasses import dataclassfrom html.parser import HTMLParser@@ -986,18 +987,25 @@ def _docx_symbol(sym) -> str:return chr(code) if 0 <= code <= 0x10FFFF and chr(code).isprintable() else ""+_docx_xpath = threading.local()++def _docx_run_text(run) -> str:- if run.find(f"{_W}fldChar/{_W}ffData/{_W}checkBox") is None and run.find(_W + "sym") is None:- return run.text+ # Per thread: run.xpath() recompiles per call; lxml locks an evaluator shared across threads.+ parts = getattr(_docx_xpath, "run_parts", None)+ if parts is None:+ from lxml import etree+ parts = _docx_xpath.run_parts = etree.XPath(+ "w:br | w:cr | w:noBreakHyphen | w:ptab | w:t | w:tab | w:fldChar | w:sym",+ namespaces = {"w": _W[1:-1]},+ )return "".join(_docx_checkbox(e)if e.tag == _W + "fldChar"else _docx_symbol(e)if e.tag == _W + "sym"else str(e)- for e in run.xpath(- "w:br | w:cr | w:noBreakHyphen | w:ptab | w:t | w:tab | w:fldChar | w:sym"- )+ for e in parts(run))@@ -1065,17 +1073,26 @@ def _docx_number_lists(document) -> None:abstracts = {a.get(_W + "abstractNumId"): a for a in numbering.iterchildren(_W + "abstractNum")}nums = {n.get(_W + "numId"): n for n in numbering.iterchildren(_W + "num")}+ style_nums = {}+# numId and ilvl inherit through basedOn independently, like any other paragraph property.def style_num(style_id,name,depth = 0,):+ if depth == 0 and (style_id, name) in style_nums:+ return style_nums[style_id, name]style = style_by_id.get(style_id)if style is None or depth > 20:- return None- value = val(style.find(f"{_W}pPr/{_W}numPr"), name)- return style_num(val(style, "basedOn"), name, depth + 1) if value is None else value+ value = None+ else:+ value = val(style.find(f"{_W}pPr/{_W}numPr"), name)+ if value is None:+ value = style_num(val(style, "basedOn"), name, depth + 1)+ if depth == 0:+ style_nums[style_id, name] = value+ return valuedef linked(abstract_id, depth = 0):num_id = style_num(val(abstracts.get(abstract_id), "numStyleLink"), "numId")diff --git a/studio/backend/tests/test_rag_docx_run_text.py b/studio/backend/tests/test_rag_docx_run_text.pynew file mode 100644index 00000000000..f10a4d1e471--- /dev/null+++ b/studio/backend/tests/test_rag_docx_run_text.py@@ -0,0 +1,90 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++from __future__ import annotations++import itertools+import sys+import threading+from pathlib import Path++import pytest++_BACKEND_DIR = str(Path(__file__).resolve().parent.parent)+if _BACKEND_DIR not in sys.path:+ sys.path.insert(0, _BACKEND_DIR)++pytest.importorskip("docx")+import docx # noqa: E402+from docx.oxml import OxmlElement # noqa: E402+from docx.oxml.ns import qn # noqa: E402++from core.rag import parsers # noqa: E402+++def _child(tag, **attrs):+ element = OxmlElement(tag)+ for key, value in attrs.items():+ element.set(qn(key), value)+ return element+++def _children():+ text = _child("w:t")+ text.text = "word"+ spaced = _child("w:t")+ spaced.text = " gap "+ deleted = _child("w:delText")+ deleted.text = "gone"+ instr = _child("w:instrText")+ instr.text = " PAGE "+ return [+ text,+ spaced,+ _child("w:tab"),+ _child("w:br"),+ _child("w:br", **{"w:type": "page"}),+ _child("w:br", **{"w:type": "column"}),+ _child("w:br", **{"w:type": "textWrapping"}),+ _child("w:cr"),+ _child("w:noBreakHyphen"),+ _child("w:softHyphen"),+ _child("w:ptab", **{"w:relativeTo": "margin", "w:alignment": "right", "w:leader": "none"}),+ deleted,+ instr,+ ]+++def _run_with(parts):+ document = docx.Document()+ run = document.add_paragraph().add_run()+ for part in parts:+ run._r.append(part)+ return run+++def test_run_text_matches_python_docx_for_every_text_child():+ """Without checkbox fields or symbols, the run text is exactly python-docx's Run.text."""+ kinds = len(_children())+ for i, j in itertools.product(range(kinds), repeat = 2):+ fresh = _children()+ run = _run_with([fresh[i], fresh[j]])+ assert parsers._docx_run_text(run._r) == run.text, (i, j)+ run = _run_with(_children())+ assert parsers._docx_run_text(run._r) == run.text+++def test_run_text_is_the_same_on_every_thread():+ run = _run_with(_children())+ expected = run.text+ results = []++ def read():+ results.extend(parsers._docx_run_text(run._r) for _ in range(50))++ threads = [threading.Thread(target = read) for _ in range(4)]+ for thread in threads:+ thread.start()+ for thread in threads:+ thread.join()+ assert results == [expected] * 200