Skip to content

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

fix(rag): escape Excel Markdown table cells (#2528)

Max Freedom Pollardd8629a987133
diff --git a/src/agentscope/rag/_parser/_excel.py b/src/agentscope/rag/_parser/_excel.py
index 46909c81a..4f0d80939 100644
--- a/src/agentscope/rag/_parser/_excel.py
+++ b/src/agentscope/rag/_parser/_excel.py
@@ -21,7 +21,7 @@ from ..._logging import logger
from ...message import Base64Source, DataBlock, TextBlock
from .._document import Section
from ._base import ParserBase
-from ._utils import _guess_image_media_type
+from ._utils import _format_markdown_table_cell, _guess_image_media_type
def _get_excel_column_name(col_index: int) -> str:
@@ -416,7 +416,7 @@ class ExcelParser(ParserBase):
num_cols = len(table_data[0])
def _fmt(cell: str, row_idx: int, col_idx: int) -> str:
- escaped = cell.replace("|", "\\|")
+ escaped = _format_markdown_table_cell(cell)
if self.include_cell_coordinates:
coord = f"{_get_excel_column_name(col_idx)}{row_idx + 1}"
return f"[{coord}] {escaped}"
diff --git a/tests/rag_parser_test.py b/tests/rag_parser_test.py
index f944c29ec..e1ddcb099 100644
--- a/tests/rag_parser_test.py
+++ b/tests/rag_parser_test.py
@@ -912,6 +912,46 @@ class ExcelParserTest(IsolatedAsyncioTestCase):
],
)
+ async def test_markdown_table_escapes_special_cells(self) -> None:
+ """Pipes and line breaks do not corrupt Markdown table rows."""
+ xlsx_bytes = _make_xlsx_simple(
+ {
+ "S1": [
+ ["A|B", r"Path \| label"],
+ ["1|2", "Line 1\nLine 2"],
+ ],
+ },
+ )
+ parser = ExcelParser(include_sheet_names=False)
+ sections = await parser.parse(xlsx_bytes, "special.xlsx")
+
+ expected_text = (
+ "\n".join(
+ [
+ r"| A\|B | Path \\\| label |",
+ "| --- | --- |",
+ r"| 1\|2 | Line 1<br>Line 2 |",
+ ],
+ )
+ + "\n"
+ )
+ self.assertEqual(
+ [section.model_dump() for section in sections],
+ [
+ {
+ "content": {
+ "type": "text",
+ "text": expected_text,
+ "id": AnyString(),
+ "created_at": AnyString(),
+ "finished_at": None,
+ },
+ "source": "special.xlsx",
+ "metadata": {},
+ },
+ ],
+ )
+
async def test_single_sheet_json(self) -> None:
"""``table_format="json"`` emits JSON rows."""
xlsx_bytes = _make_xlsx_simple(