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 all text when adding Word files to a knowledge base (#11725)

Nilay86edc79c864b
diff --git a/studio/backend/core/rag/parsers.py b/studio/backend/core/rag/parsers.py
index 1c06972f4f3..d36ff9121b7 100644
--- a/studio/backend/core/rag/parsers.py
+++ b/studio/backend/core/rag/parsers.py
@@ -418,15 +418,44 @@ _DOCX_SKIP_RUNS_UNDER = frozenset(
)
+def _docx_placeholder(element) -> bool:
+ # An unfilled content control holds Word's prompt ("Click or tap here to enter text."), not a value.
+ if element.tag != _W + "sdt":
+ return False
+ flag = element.find(_W + "sdtPr/" + _W + "showingPlcHdr")
+ return flag is not None and flag.get(_W + "val", "true") not in ("0", "false", "off")
+
+
def _docx_inside(element, stop, tags) -> bool:
node = element.getparent()
while node is not stop:
- if node.tag in tags:
+ if node.tag in tags or _docx_placeholder(node):
return True
node = node.getparent()
return False
+def _docx_unwrap_table_controls(body) -> None:
+ # python-docx skips w:tr / w:tc wrapped in content controls (cover pages, repeating sections).
+ for wrapper in list(body.iter(_W + "sdt", _W + "customXml")):
+ parent = wrapper.getparent()
+ if parent is None or parent.tag not in (_W + "tbl", _W + "tr"):
+ continue
+ content = wrapper.find(_W + "sdtContent") if wrapper.tag == _W + "sdt" else wrapper
+ keep = (_W + "tr", _W + "tc", _W + "sdt", _W + "customXml")
+ placeholder = _docx_placeholder(wrapper)
+ idx = parent.index(wrapper)
+ for i, child in enumerate(
+ [c for c in (content if content is not None else ()) if c.tag in keep]
+ ):
+ if placeholder: # keep the cells so columns line up, drop the prompt text
+ for tc in child.iter(_W + "tc"):
+ for el in [e for e in tc if e.tag != _W + "tcPr"]:
+ tc.remove(el)
+ parent.insert(idx + i, child)
+ parent.remove(wrapper)
+
+
def _docx_blocks(element, parent):
"""Paragraphs and tables in document order, including content controls and text boxes."""
from docx.table import Table
@@ -441,7 +470,7 @@ def _docx_blocks(element, parent):
yield from _docx_blocks(box, parent)
elif child.tag == _W + "tbl":
yield Table(child, parent)
- elif child.tag in (_W + "sdt", _W + "customXml"):
+ elif child.tag in (_W + "sdt", _W + "customXml") and not _docx_placeholder(child):
content = child.find(_W + "sdtContent")
yield from _docx_blocks(child if content is None else content, parent)
@@ -501,6 +530,7 @@ def _docx(path: str) -> list[Page]:
document = docx.Document(path)
lines: list[str] = []
+ _docx_unwrap_table_controls(document.element.body)
# Walk body content in document order: paragraphs alone drop tables entirely.
for block in _docx_blocks(document.element.body, document):
if isinstance(block, Paragraph):
diff --git a/studio/backend/tests/test_gpu_selection.py b/studio/backend/tests/test_gpu_selection.py
index 3828c7981b0..3b50cba6555 100644
--- a/studio/backend/tests/test_gpu_selection.py
+++ b/studio/backend/tests/test_gpu_selection.py
@@ -938,7 +938,9 @@ class TestExplicitPickWithoutTorchKernels(unittest.TestCase):
mock_auto_select.assert_not_called()
def test_the_refusal_names_the_arch_on_wheels_without_gcnArchName(self):
- props = SimpleNamespace(name = "AMD Radeon RX 5700 XT", gcnArchName = "", gfx_arch_name = "gfx1010:xnack-")
+ props = SimpleNamespace(
+ name = "AMD Radeon RX 5700 XT", gcnArchName = "", gfx_arch_name = "gfx1010:xnack-"
+ )
with (
patch("torch.cuda.device_count", return_value = 2),
patch("torch.cuda.get_device_properties", return_value = props),
diff --git a/studio/backend/tests/test_rag_parsing.py b/studio/backend/tests/test_rag_parsing.py
index 3fb6225bbba..f6a91ad1a72 100644
--- a/studio/backend/tests/test_rag_parsing.py
+++ b/studio/backend/tests/test_rag_parsing.py
@@ -443,3 +443,114 @@ def test_docx_reads_one_branch_of_alternate_content(tmp_path):
"</mc:AlternateContent></w:p>",
)
assert text == "Preferred"
+
+
+def test_docx_keeps_rows_and_cells_wrapped_in_content_controls(tmp_path):
+ document, docx, parsers = _shared_setup_1()
+ from docx.oxml import parse_xml
+ from docx.oxml.ns import nsdecls
+
+ ns = nsdecls("w")
+ table = document.add_table(rows = 1, cols = 2)
+ table.cell(0, 0).text = "Name"
+ tr = table.rows[0]._tr
+ tc = table.cell(0, 1)._tc
+ tr.remove(tc)
+ tr.append(
+ parse_xml(
+ f"<w:sdt {ns}><w:sdtContent><w:tc><w:p><w:r><w:t>CELL-WRAPPED</w:t></w:r></w:p></w:tc>"
+ "</w:sdtContent></w:sdt>"
+ )
+ )
+ table._tbl.append(
+ parse_xml(
+ f"<w:sdt {ns}><w:sdtContent><w:sdt><w:sdtContent><w:tr>"
+ "<w:tc><w:p><w:r><w:t>ROW-A</w:t></w:r></w:p></w:tc>"
+ "<w:tc><w:p><w:r><w:t>ROW-B</w:t></w:r></w:p></w:tc>"
+ "</w:tr></w:sdtContent></w:sdt></w:sdtContent></w:sdt>"
+ )
+ )
+ path = tmp_path / "wrapped.docx"
+ document.save(str(path))
+
+ text = "\n".join(pg.text for pg in parsers.parse(str(path)))
+ assert "Name | CELL-WRAPPED" in text
+ assert "ROW-A | ROW-B" in text
+
+
+def test_docx_skips_placeholder_text_and_keeps_field_and_bidi_runs(tmp_path):
+ document, docx, parsers = _shared_setup_1()
+ from docx.oxml import parse_xml
+ from docx.oxml.ns import nsdecls
+
+ ns = nsdecls("w")
+
+ def sdt(props, inner):
+ return parse_xml(
+ f"<w:sdt {ns}><w:sdtPr>{props}</w:sdtPr><w:sdtContent>{inner}</w:sdtContent></w:sdt>"
+ )
+
+ body = document.element.body
+ body.insert(
+ len(body) - 1,
+ sdt("<w:showingPlcHdr/>", "<w:p><w:r><w:t>BLOCK-PROMPT</w:t></w:r></w:p>"),
+ )
+ p = document.add_paragraph("Name: ")._p
+ p.append(sdt("<w:showingPlcHdr/>", "<w:r><w:t>Click or tap here to enter text.</w:t></w:r>"))
+ p = document.add_paragraph("Client: ")._p
+ p.append(sdt('<w:showingPlcHdr w:val="0"/>', "<w:r><w:t>ACME</w:t></w:r>"))
+ p = document.add_paragraph("Ref ")._p
+ p.append(
+ parse_xml(
+ f'<w:fldSimple {ns} w:instr=" MERGEFIELD Name "><w:r><w:t>FIELD</w:t></w:r></w:fldSimple>'
+ )
+ )
+ p.append(parse_xml(f'<w:dir {ns} w:val="rtl"><w:r><w:t> RTL</w:t></w:r></w:dir>'))
+ table = document.add_table(rows = 1, cols = 2)
+ table.cell(0, 0).text = "Owner"
+ table.cell(0, 1)._tc.append(
+ sdt("<w:showingPlcHdr/>", "<w:p><w:r><w:t>CELL-PROMPT</w:t></w:r></w:p>")
+ )
+ path = tmp_path / "placeholders.docx"
+ document.save(str(path))
+
+ text = "\n".join(pg.text for pg in parsers.parse(str(path)))
+ assert "PROMPT" not in text and "Click or tap" not in text
+ assert "Client: ACME" in text
+ assert "Ref FIELD RTL" in text
+ assert "Owner | " in text
+
+
+def test_docx_skips_placeholder_rows_and_cells_but_keeps_columns(tmp_path):
+ document, docx, parsers = _shared_setup_1()
+ from docx.oxml import parse_xml
+ from docx.oxml.ns import nsdecls
+
+ ns = nsdecls("w")
+ table = document.add_table(rows = 1, cols = 3)
+ table.cell(0, 0).text = "Name"
+ table.cell(0, 2).text = "END"
+ tr = table.rows[0]._tr
+ tc = table.cell(0, 1)._tc
+ idx = tr.index(tc)
+ tr.remove(tc)
+ tr.insert(
+ idx,
+ parse_xml(
+ f"<w:sdt {ns}><w:sdtPr><w:showingPlcHdr/></w:sdtPr><w:sdtContent>"
+ "<w:tc><w:p><w:r><w:t>CELL-PROMPT</w:t></w:r></w:p></w:tc></w:sdtContent></w:sdt>"
+ ),
+ )
+ table._tbl.append(
+ parse_xml(
+ f"<w:sdt {ns}><w:sdtPr><w:showingPlcHdr/></w:sdtPr><w:sdtContent><w:tr>"
+ "<w:tc><w:p><w:r><w:t>ROW-PROMPT</w:t></w:r></w:p></w:tc>"
+ "<w:tc><w:p/></w:tc><w:tc><w:p/></w:tc></w:tr></w:sdtContent></w:sdt>"
+ )
+ )
+ path = tmp_path / "placeholder_cells.docx"
+ document.save(str(path))
+
+ text = "\n".join(pg.text for pg in parsers.parse(str(path)))
+ assert "PROMPT" not in text
+ assert "Name | | END" in text