55import pathlib
66
77import pdfplumber
8- from markitdown import MarkItDown
8+ from markitdown import MarkItDown , StreamInfo
99
1010logger = logging .getLogger (__name__ )
1111
@@ -20,7 +20,7 @@ async def bytes_to_str(file_bytes: bytes, filename: str) -> str:
2020 # handle most common file types using MarkItDown.
2121 # Note .eml will include the raw html which is very token heavy
2222 case _ if filename_extension in ["docx" , "pptx" , "csv" , "xlsx" , "html" , "eml" ]:
23- return await _markitdown_bytes_to_str (file_bytes )
23+ return await _markitdown_bytes_to_str (file_bytes , "." + filename_extension )
2424
2525 case "pdf" :
2626 return await _pdf_bytes_to_str (file_bytes )
@@ -37,12 +37,16 @@ async def bytes_to_str(file_bytes: bytes, filename: str) -> str:
3737 return f"The filetype `{ filename_extension } ` is not supported or the file itself is malformed: { e } "
3838
3939
40- async def _markitdown_bytes_to_str (file_bytes : bytes ) -> str :
40+ async def _markitdown_bytes_to_str (file_bytes : bytes , filename_extension : str ) -> str :
4141 """
4242 Convert a file using MarkItDown defaults.
4343 """
4444 with io .BytesIO (file_bytes ) as temp :
45- result = await asyncio .to_thread (MarkItDown (enable_plugins = False ).convert , source = temp )
45+ result = await asyncio .to_thread (
46+ MarkItDown (enable_plugins = False ).convert ,
47+ source = temp ,
48+ stream_info = StreamInfo (extension = filename_extension ),
49+ )
4650 text = result .text_content
4751 return text
4852
0 commit comments