Skip to content

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

feat(rag): surface Excel worksheet parsing failures (#3021)

Daomiyo3eb3a8be9c5e
diff --git a/src/agentscope/rag/_parser/_excel.py b/src/agentscope/rag/_parser/_excel.py
index 0f385fc44..d8b2d42d1 100644
--- a/src/agentscope/rag/_parser/_excel.py
+++ b/src/agentscope/rag/_parser/_excel.py
@@ -303,7 +303,7 @@ class ExcelParser(ParserBase):
`FileNotFoundError`: If ``file`` is a ``str`` pointing to
a path that does not exist.
`ImportError`: If :mod:`pandas` is not installed.
- `ValueError`: If the bytes cannot be parsed.
+ `ValueError`: If the workbook or any worksheet cannot be parsed.
"""
try:
import pandas as pd
@@ -385,8 +385,9 @@ class ExcelParser(ParserBase):
keep_default_na=False,
)
except Exception as e:
- logger.warning("Failed to parse sheet '%s': %s", sheet_name, e)
- return sheet_sections
+ raise ValueError(
+ f"Failed to parse sheet {sheet_name!r} in {filename!r}: {e}",
+ ) from e
# Keep the first row as cell data: pandas column labels would rename
# duplicate headers and replace blank headers with "Unnamed: ...".
diff --git a/tests/rag_parser_test.py b/tests/rag_parser_test.py
index 0b3cbf753..53ea9c6f6 100644
--- a/tests/rag_parser_test.py
+++ b/tests/rag_parser_test.py
@@ -1723,6 +1723,38 @@ class PPTParserTest(IsolatedAsyncioTestCase):
class ExcelParserTest(IsolatedAsyncioTestCase):
"""Behavioural coverage for :class:`ExcelParser`."""
+ async def test_corrupt_sheet_does_not_return_partial_content(self) -> None:
+ """A deferred worksheet error fails the whole workbook parse."""
+ from xml.etree.ElementTree import ParseError
+
+ xlsx_bytes = _make_xlsx_simple(
+ {
+ "Good": [["Key"], ["visible"]],
+ "Broken": [["Key"], ["must not disappear"]],
+ },
+ )
+ buffer = io.BytesIO()
+ with zipfile.ZipFile(io.BytesIO(xlsx_bytes)) as source:
+ with zipfile.ZipFile(buffer, "w") as target:
+ for entry in source.infolist():
+ data = source.read(entry.filename)
+ if entry.filename == "xl/worksheets/sheet2.xml":
+ data = data.replace(
+ b"</sheetData>",
+ b"</brokenSheetData>",
+ )
+ target.writestr(entry, data)
+
+ for separate_sheet in (False, True):
+ with self.subTest(separate_sheet=separate_sheet):
+ parser = ExcelParser(separate_sheet=separate_sheet)
+ with self.assertRaisesRegex(
+ ValueError,
+ "Failed to parse sheet 'Broken' in 'corrupt.xlsx'",
+ ) as raised:
+ await parser.parse(buffer.getvalue(), "corrupt.xlsx")
+ self.assertIsInstance(raised.exception.__cause__, ParseError)
+
async def test_duplicate_and_blank_headers(self) -> None:
"""Headers remain cell values, without pandas-generated labels."""
from openpyxl import Workbook