from praisonai._framework_availability import is_available# Check if CrewAI is availableif is_available("crewai"): print("CrewAI is installed and importable")# Check AG2 (special detection logic)if is_available("ag2"): print("AG2 is available (both distribution and namespace)")# Check multiple frameworksframeworks = ["autogen", "autogen_v4", "crewai", "ag2"]available = [fw for fw in frameworks if is_available(fw)]print(f"Available frameworks: {available}")
AG2 ships under the autogen namespace, so is_available("ag2") checks both:
importlib.metadata.distribution('ag2') - Ensures AG2 distribution is installed
importlib.util.find_spec("autogen") - Ensures autogen namespace is importable
This prevents false positives when only the legacy AutoGen package is installed.
from praisonai._framework_availability import is_available# This checks both AG2 distribution AND autogen namespaceif is_available("ag2"): print("AG2 is properly installed and autogen namespace is available")
The availability checker uses double-checked locking under threading.Lock for thread-safe caching:
import threadingfrom praisonai._framework_availability import is_availabledef worker(): # Safe to call from multiple threads return is_available("crewai")threads = [threading.Thread(target=worker) for _ in range(10)]for t in threads: t.start()for t in threads: t.join()
import unittest.mockfrom praisonai._framework_availability import is_available, invalidatedef test_framework_detection(): # Clear any cached results invalidate() with unittest.mock.patch('importlib.util.find_spec', return_value=None): assert not is_available("crewai") # Clear cache again for clean test state invalidate()