Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Fix FileTask/UrlTask crash on non-string elements

chuenchen3099b8a606a166e
diff --git a/src/python/txtai/workflow/task/file.py b/src/python/txtai/workflow/task/file.py
index 66d34249..9f878147 100644
--- a/src/python/txtai/workflow/task/file.py
+++ b/src/python/txtai/workflow/task/file.py
@@ -17,11 +17,15 @@ class FileTask(Task):
FILE = r"file:\/\/"
def accept(self, element):
+ # Only process string elements
+ if not isinstance(element, str):
+ return False
+
# Replace file prefixes
element = re.sub(FileTask.FILE, "", element)
# Only accept file paths that exist
- return super().accept(element) and isinstance(element, str) and os.path.exists(element)
+ return super().accept(element) and os.path.exists(element)
def prepare(self, element):
# Replace file prefixes
diff --git a/src/python/txtai/workflow/task/url.py b/src/python/txtai/workflow/task/url.py
index 056d5637..e952234c 100644
--- a/src/python/txtai/workflow/task/url.py
+++ b/src/python/txtai/workflow/task/url.py
@@ -17,4 +17,4 @@ class UrlTask(Task):
def accept(self, element):
# Only accept elements that start with a url prefix
- return super().accept(element) and re.match(UrlTask.PREFIX, element.lower())
+ return super().accept(element) and isinstance(element, str) and re.match(UrlTask.PREFIX, element.lower())
diff --git a/test/python/testworkflow.py b/test/python/testworkflow.py
index 8a148057..7ff183d6 100644
--- a/test/python/testworkflow.py
+++ b/test/python/testworkflow.py
@@ -27,6 +27,7 @@ from txtai.workflow import (
RetrieveTask,
StorageTask,
TemplateTask,
+ UrlTask,
WorkflowTask,
)
@@ -225,6 +226,27 @@ class TestWorkflow(unittest.TestCase):
self.assertEqual(results[0].size, (1024, 682))
+ def testFileUrlTaskNonStringElements(self):
+ """
+ Test that FileTask/UrlTask/ImageTask pass through non-string and non-matching
+ elements instead of crashing, matching the base Task.accept contract for
+ heterogeneous batches.
+ """
+
+ with tempfile.NamedTemporaryFile() as temp:
+ # Elements: a real file path (matches FileTask), a url (matches UrlTask),
+ # a non-string, None and a non-matching string - none of these should crash.
+ data = [temp.name, "http://example.com", 5, None, "no match here"]
+
+ urls = list(Workflow([UrlTask(lambda x: [y.upper() for y in x])])(list(data)))
+ self.assertEqual(urls, [temp.name, "HTTP://EXAMPLE.COM", 5, None, "no match here"])
+
+ files = list(Workflow([FileTask(lambda x: [f.upper() for f in x])])(list(data)))
+ self.assertEqual(files, [temp.name.upper(), "http://example.com", 5, None, "no match here"])
+
+ images = list(Workflow([ImageTask()])(list(data)))
+ self.assertEqual(images, [temp.name, "http://example.com", 5, None, "no match here"])
+
def testInvalidWorkflow(self):
"""
Test task with invalid parameters