[BUG] ExcelKnowledgeSource rewrites cell values: 12 becomes 12.0, 01234 becomes 1234, N/A becomes blank
Description
ExcelKnowledgeSource reads each sheet with pd.read_excel(xl, sheet_name), and pandas infers a dtype for every column. As a result, the text that gets chunked and embedded differs from what the workbook holds:
- A column of whole numbers with one blank cell becomes float, so
12is stored as12.0. - Text that looks numeric loses its leading zeros: a ZIP code or ID typed as text,
01234, becomes1234, or1234.0next to a blank. - Cells whose text is one of pandas' default NA values (
N/A,NA,NULL,NaN,null,n/a, …) are emptied, so a "Status: N/A" cell carries no information.
Steps to Reproduce
from pathlib import Path
from openpyxl import Workbook
from crewai.knowledge.source.excel_knowledge_source import ExcelKnowledgeSource
wb = Workbook()
ws = wb.active
ws.append(["SKU", "Qty", "Zip", "Status"])
ws.append(["A1", 12, "01234", "N/A"])
ws.append(["A2", None, "02134", "NULL"])
wb.save("stock.xlsx")
source = ExcelKnowledgeSource(file_paths=[Path("stock.xlsx")])
print(source.content[Path("stock.xlsx")]["Sheet"])
Expected behavior
SKU,Qty,Zip,Status
A1,12,01234,N/A
A2,,02134,NULL
Actual
SKU,Qty,Zip,Status
A1,12.0,1234,
A2,,2134,
Possible Solution
Read the cells as written: pd.read_excel(xl, sheet_name, dtype=str, keep_default_na=False). Empty cells still come out empty.
crewAI main (9c24fda7f), pandas 2.2.3, openpyxl 3.1.5, Python 3.12, macOS.
I'd like to pick this up. The fix reads each sheet with dtype=str, keep_default_na=False so the stored CSV keeps the values as stored in the workbook, and I'll add a regression test that builds the workbook from the report above. PR coming shortly.
This is still reproducible on main. A fix is open in #8396. Happy to adjust anything reviewers would like changed.