Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
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.pyindex 46909c81a..4f0d80939 100644--- a/src/agentscope/rag/_parser/_excel.py+++ b/src/agentscope/rag/_parser/_excel.py@@ -21,7 +21,7 @@ from ..._logging import loggerfrom ...message import Base64Source, DataBlock, TextBlockfrom .._document import Sectionfrom ._base import ParserBase-from ._utils import _guess_image_media_type+from ._utils import _format_markdown_table_cell, _guess_image_media_typedef _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.pyindex 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(