From 5dff35ba03a029fadcf24c88e1bdb33aa0a53ee2 Mon Sep 17 00:00:00 2001 From: Dividesbyzer0 <54127744+zoomdbz@users.noreply.github.com> Date: Sun, 7 Jun 2026 12:14:43 -0400 Subject: [PATCH] fix(cookbook): don't 500 the packages panel when an optional package crashes on import (#2618) list_packages() probes each optional package with importlib.import_module() but only caught ImportError / PackageNotFoundError. A package that is installed yet raises a different exception on import took down the whole panel with a 500, surfaced in the UI as "Error loading packages: Unexpected token 'I', ...". Concrete Windows case: a CUDA build of llama-cpp-python runs os.add_dll_directory(r"...\CUDA\v12.3\bin") at import and raises FileNotFoundError when that toolkit dir is absent. Catch any exception during the import probe and report the package as not-installed instead of failing the entire request. Co-authored-by: Claude Opus 4.8 --- routes/shell_routes.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/routes/shell_routes.py b/routes/shell_routes.py index 3ffaab522..a3126abbb 100644 --- a/routes/shell_routes.py +++ b/routes/shell_routes.py @@ -1209,6 +1209,12 @@ def setup_shell_routes() -> APIRouter: pkg["installed"] = False except importlib_metadata.PackageNotFoundError: pkg["installed"] = False + except Exception: + # Installed but crashes on import — e.g. a CUDA build of + # llama-cpp-python raising FileNotFoundError when the CUDA + # toolkit dir is absent. One broken optional package must not + # 500 the entire packages panel; report it as not usable. + pkg["installed"] = False if pkg.get("installed"): update_status = _package_pip_update_status(pkg, probe)