Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Remove downloaded temporary files after extraction errors (#1489)

Hua.T3bc7df0482ae
diff --git a/src/python/txtai/pipeline/data/textractor.py b/src/python/txtai/pipeline/data/textractor.py
index 747c743c..3c349d07 100644
--- a/src/python/txtai/pipeline/data/textractor.py
+++ b/src/python/txtai/pipeline/data/textractor.py
@@ -59,15 +59,17 @@ class Textractor(Segmentation):
# Retrieve remote file, if necessary
path = path if exists else self.download(path)
- # Parse content to HTML
- html = self.html(path)
+ try:
+ # Parse content to HTML
+ html = self.html(path)
- # FiletoHTML pipeline returns None when input is already HTML
- html = html if html else self.safeopen.retrieve(path)
+ # FiletoHTML pipeline returns None when input is already HTML
+ html = html if html else self.safeopen.retrieve(path)
- # Delete temporary file
- if not exists:
- os.remove(path)
+ finally:
+ # Delete temporary file
+ if not exists:
+ os.remove(path)
else:
# Read data from url/path
diff --git a/test/python/testpipeline/testdata/testtextractor.py b/test/python/testpipeline/testdata/testtextractor.py
index e75d68f4..144f1d69 100644
--- a/test/python/testpipeline/testdata/testtextractor.py
+++ b/test/python/testpipeline/testdata/testtextractor.py
@@ -83,6 +83,26 @@ class TestTextractor(unittest.TestCase):
self.assertIs(raised.exception, error)
self.assertEqual(os.listdir(directory), [])
+ def testConversionError(self):
+ """
+ Test failed conversion removes the downloaded temporary file
+ """
+
+ textractor = Textractor(backend=None)
+ error = RuntimeError("conversion failed")
+
+ with tempfile.TemporaryDirectory() as directory:
+ with (
+ patch("tempfile.tempdir", directory),
+ patch.object(textractor.safeopen, "retrieve", return_value=b"document"),
+ patch.object(textractor, "html", side_effect=error),
+ ):
+ with self.assertRaises(RuntimeError) as raised:
+ textractor("https://example.com/document.pdf")
+
+ self.assertIs(raised.exception, error)
+ self.assertEqual(os.listdir(directory), [])
+
def testDocling(self):
"""
Test docling backend