Skip to content

Build and run agents you can see, understand and trust.

fix(rag): read content inside block-level Word content controls (#2845)

sxh9977f6b4dbcf
diff --git a/src/agentscope/rag/_parser/_word.py b/src/agentscope/rag/_parser/_word.py
index 900d5b438..4fe63b3e4 100644
--- a/src/agentscope/rag/_parser/_word.py
+++ b/src/agentscope/rag/_parser/_word.py
@@ -215,6 +215,35 @@ def _extract_image_blocks(
return blocks
+def _body_elements(body: Any) -> Iterator[Any]:
+ """Yield the top-level content elements of a document body.
+
+ Block-level content controls (``w:sdt`` — how Word writes its
+ automatic table of contents and every developer-mode block control)
+ are containers, not content: their ``w:sdtContent`` children are
+ yielded in their place, recursively, so a control cannot hide a
+ paragraph or a table. Inline ``w:sdt`` needs no handling here, as
+ its runs are already descendants of the paragraph holding it.
+
+ Args:
+ body (`Any`):
+ The ``w:body`` element of a python-docx document.
+
+ Yields:
+ `Any`:
+ Each body child, with content controls replaced by theirs.
+ """
+ from docx.oxml.ns import qn
+
+ for element in body:
+ if element.tag == qn("w:sdt"):
+ content = element.find(qn("w:sdtContent"))
+ if content is not None:
+ yield from _body_elements(content)
+ else:
+ yield element
+
+
class WordParser(ParserBase):
"""Parser for Word ``.docx`` files.
@@ -342,7 +371,7 @@ class WordParser(ParserBase):
)
text_buffer.clear()
- for element in doc.element.body:
+ for element in _body_elements(doc.element.body):
if isinstance(element, CT_P):
para = Paragraph(element, doc)
diff --git a/tests/rag_parser_test.py b/tests/rag_parser_test.py
index 82b02020c..0b3cbf753 100644
--- a/tests/rag_parser_test.py
+++ b/tests/rag_parser_test.py
@@ -492,6 +492,62 @@ def _make_docx_with_image_in_table_text_box() -> bytes:
return buffer.getvalue()
+_W_NS = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
+
+
+def _make_docx_content_control(*, wrapped: bool) -> bytes:
+ """Build a DOCX whose middle content is in ``w:sdt``, or is plain.
+
+ ``wrapped=True`` puts a paragraph, a table and a nested control
+ inside a block-level content control, which is how Word writes an
+ automatic table of contents; ``wrapped=False`` writes the same
+ content directly under the body.
+
+ Args:
+ wrapped (`bool`):
+ Whether the middle content is wrapped in ``w:sdt``.
+
+ Returns:
+ `bytes`:
+ The DOCX bytes.
+ """
+ from docx import Document as DocxDocument
+ from docx.oxml import parse_xml
+
+ ns = f'xmlns:w="{_W_NS}"'
+ heading = f"<w:p {ns}><w:r><w:t>Controlled heading</w:t></w:r></w:p>"
+ table = (
+ f"<w:tbl {ns}><w:tr>"
+ "<w:tc><w:p><w:r><w:t>cell-a</w:t></w:r></w:p></w:tc>"
+ "<w:tc><w:p><w:r><w:t>cell-b</w:t></w:r></w:p></w:tc>"
+ "</w:tr></w:tbl>"
+ )
+ nested = f"<w:p {ns}><w:r><w:t>Nested control</w:t></w:r></w:p>"
+ if wrapped:
+ fragments = [
+ f"<w:sdt {ns}><w:sdtContent>"
+ f"{heading}{table}"
+ f"<w:sdt><w:sdtContent>{nested}</w:sdtContent></w:sdt>"
+ f"</w:sdtContent></w:sdt>",
+ ]
+ else:
+ fragments = [heading, table, nested]
+
+ doc = DocxDocument()
+ doc.add_paragraph("Before.")
+ after = doc.add_paragraph("After.")
+ for fragment in fragments:
+ # ``w:sectPr`` has to stay the last body child, so insert rather
+ # than append.
+ after._element.addprevious( # pylint: disable=protected-access
+ parse_xml(fragment),
+ )
+
+ buffer = io.BytesIO()
+ doc.save(buffer)
+ return buffer.getvalue()
+
+
def _make_xlsx_simple(
sheets: dict[str, list[list[str]]],
) -> bytes:
@@ -2893,3 +2949,56 @@ class WordParserParagraphTest(IsolatedAsyncioTestCase):
[section.model_dump() for section in sections],
_expected_word_sections("ChoiceFallback\nAfter"),
)
+
+
+class WordParserContentControlTest(IsolatedAsyncioTestCase):
+ """Regression coverage for block-level Word content controls."""
+
+ async def test_block_content_control_text_is_kept(self) -> None:
+ """A block-level ``w:sdt`` does not hide what it contains."""
+ sections = await WordParser(include_image=False).parse(
+ _make_docx_content_control(wrapped=True),
+ "toc.docx",
+ )
+
+ self.assertEqual(
+ [s.model_dump() for s in sections],
+ [
+ {
+ "content": {
+ "type": "text",
+ "text": (
+ "Before.\n"
+ "Controlled heading\n"
+ "| cell-a | cell-b |\n"
+ "| --- | --- |\n"
+ "\n"
+ "Nested control\n"
+ "After."
+ ),
+ "id": AnyString(),
+ "created_at": AnyString(),
+ "finished_at": None,
+ },
+ "source": "toc.docx",
+ "metadata": {},
+ },
+ ],
+ )
+
+ async def test_content_control_is_transparent(self) -> None:
+ """The same content parses the same wrapped or unwrapped."""
+ parser = WordParser(include_image=False)
+ wrapped = await parser.parse(
+ _make_docx_content_control(wrapped=True),
+ "d.docx",
+ )
+ plain = await parser.parse(
+ _make_docx_content_control(wrapped=False),
+ "d.docx",
+ )
+
+ self.assertEqual(
+ [(s.content.text, s.source, s.metadata) for s in wrapped],
+ [(s.content.text, s.source, s.metadata) for s in plain],
+ )