Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Preserve agent dependency import errors (#1450)

Daniel Pengd4186a44d1ce
diff --git a/src/python/txtai/agent/__init__.py b/src/python/txtai/agent/__init__.py
index 96919655..81e054ef 100644
--- a/src/python/txtai/agent/__init__.py
+++ b/src/python/txtai/agent/__init__.py
@@ -8,5 +8,7 @@ try:
from .factory import ProcessFactory
from .model import PipelineModel
from .tool import *
-except ImportError:
+except ImportError as missing:
from .placeholder import Agent
+
+ Agent.ERROR = missing
diff --git a/src/python/txtai/agent/placeholder.py b/src/python/txtai/agent/placeholder.py
index 5897a38d..8a04d8c1 100644
--- a/src/python/txtai/agent/placeholder.py
+++ b/src/python/txtai/agent/placeholder.py
@@ -5,12 +5,15 @@ Placeholder module
class Agent:
"""
- Agent placeholder stub for when smolagents isn't installed
+ Agent placeholder for when agent dependencies cannot be imported
"""
+ # Original dependency import failure
+ ERROR = None
+
def __init__(self, *args, **kwargs):
"""
- Raises an exception that smolagents isn't installed.
+ Raises an exception with installation guidance and the original cause.
"""
- raise ImportError('smolagents is not available - install "agent" extra to enable')
+ raise ImportError('smolagents is not available - install "agent" extra to enable') from self.ERROR
diff --git a/test/python/testagent.py b/test/python/testagent.py
index 1ee1a701..d64f1131 100644
--- a/test/python/testagent.py
+++ b/test/python/testagent.py
@@ -3,7 +3,9 @@ Agent module tests
"""
import os
+import sys
import tempfile
+import traceback
import unittest
from threading import Thread
@@ -49,6 +51,37 @@ class TestAgent(unittest.TestCase):
self.assertEqual(agent("Hello"), "Hi")
+ def testImportError(self):
+ """
+ Test missing and transitive dependencies preserve their import failure.
+ """
+
+ # pylint: disable=C0415
+ for module in ("smolagents", "mcpadapt.core"):
+ with self.subTest(module=module), patch.dict(sys.modules, {module: None}):
+ # Reload txtai with one dependency unavailable, as in testoptional.
+ for name in list(sys.modules):
+ if name == "txtai" or name.startswith("txtai."):
+ del sys.modules[name]
+
+ from txtai import Agent as UnavailableAgent
+
+ with self.assertRaises(ImportError) as error:
+ UnavailableAgent()
+
+ cause = error.exception.__cause__
+ self.assertIsInstance(cause, ModuleNotFoundError)
+ self.assertIs(cause, UnavailableAgent.ERROR)
+ self.assertEqual(cause.name, module)
+ self.assertIsNotNone(cause.__traceback__)
+ self.assertIn(str(cause), "".join(traceback.format_exception(error.exception)))
+ self.assertIn('install "agent" extra', str(error.exception))
+
+ # Module restoration must keep the real Agent available to other tests.
+ from txtai import Agent as RestoredAgent
+
+ self.assertIs(RestoredAgent, Agent)
+
def testInstructions(self):
"""
Test loading an agents.md file