Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Raise FileNotFoundError in ReadTool when file does not exist (#1444) (#1449)

Naimish0ea7cb0d8234
diff --git a/src/python/txtai/agent/tool/read.py b/src/python/txtai/agent/tool/read.py
index 4403306d..9e00286b 100644
--- a/src/python/txtai/agent/tool/read.py
+++ b/src/python/txtai/agent/tool/read.py
@@ -52,6 +52,11 @@ class ReadTool(Tool):
content
"""
+ # Validate file path exists or is a valid URL
+ valid, _ = self.textractor.safeopen.valid(path)
+ if not valid:
+ raise FileNotFoundError(f"File not found: {path}")
+
content = self.textractor(path)
content = re.sub(r"\n{3,}", "\n\n", content)
diff --git a/test/python/testagent.py b/test/python/testagent.py
index d64f1131..97ca9a56 100644
--- a/test/python/testagent.py
+++ b/test/python/testagent.py
@@ -265,6 +265,15 @@ class TestAgent(unittest.TestCase):
# Timeout is disabled when set to None
self.assertIsNone(BashTool(timeout=None).timeout)
+ def testToolsReadNonexistent(self):
+ """
+ Test the read tool raises FileNotFoundError when a file does not exist
+ """
+
+ # Reading a non-existent file must raise FileNotFoundError
+ with self.assertRaises(FileNotFoundError):
+ ToolFactory.default("read")(os.path.join(tempfile.gettempdir(), "does_not_exist_file.txt"))
+
def testToolsEditCRLF(self):
"""
Test the edit tool preserves CRLF line endings