Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Let the pipeline gate survive a diffusers whose lazy submodule cannot import (#8227)

Daniel Han0d9e6bd68142
diff --git a/studio/backend/core/inference/diffusion_families.py b/studio/backend/core/inference/diffusion_families.py
index ea69f557682..605c0aeb8f0 100644
--- a/studio/backend/core/inference/diffusion_families.py
+++ b/studio/backend/core/inference/diffusion_families.py
@@ -761,15 +761,22 @@ def assert_pipeline_class_available(pipeline_class: str, family_name: str) -> No
500 with the message lost."""
try:
import diffusers
- except ImportError:
+ present = hasattr(diffusers, pipeline_class)
+ except Exception: # noqa: BLE001 -- see below: this check must never raise anything but its own ValueError
# Not this check's business: it answers "is the installed diffusers new enough for this family", and with
- # nothing installed there is no version to judge. Refusing would also break the native sd.cpp engine, which
+ # nothing importable there is no version to judge. Refusing would also break the native sd.cpp engine, which
# serves GGUF picks on a CPU or Apple host without diffusers. A pick that really needs it fails later, in
# the loader. The one thing that must not happen is a raise: ModuleNotFoundError is not the ValueError the
# routes map to 400, so it escapes /images/download-plan as a bare 500 with the message lost.
+ #
+ # The attribute probe is inside the try for the same reason. diffusers' top level is a lazy module, so
+ # ``hasattr`` is what actually imports the pipeline's submodule, and when that submodule's own dependencies
+ # are unsatisfiable it raises RuntimeError ("Failed to import diffusers.pipelines...") -- which hasattr does
+ # NOT swallow, since it only absorbs AttributeError. A partially usable diffusers install therefore escaped
+ # this guard exactly the way a missing one used to.
return
- if hasattr(diffusers, pipeline_class):
+ if present:
return
raise ValueError(
f"'{family_name}' needs diffusers >= 0.39.0 ({pipeline_class}); this environment has "
diff --git a/studio/backend/tests/test_cached_gguf_routes.py b/studio/backend/tests/test_cached_gguf_routes.py
index 8605cbd1cd1..2d756b4ecb6 100644
--- a/studio/backend/tests/test_cached_gguf_routes.py
+++ b/studio/backend/tests/test_cached_gguf_routes.py
@@ -3291,6 +3291,28 @@ def test_pipeline_class_guard_is_silent_when_diffusers_is_absent(monkeypatch):
assert assert_pipeline_class_available("ZImagePipeline", "z-image") is None
+def test_pipeline_class_guard_is_silent_when_a_lazy_submodule_cannot_import(monkeypatch):
+ # Same contract as the absent-diffusers case, for the install that is PRESENT but only
+ # partially usable. diffusers' top level is a lazy module, so the attribute probe is what
+ # actually imports the pipeline's submodule, and when that submodule's own dependencies are
+ # unsatisfiable it raises RuntimeError ("Failed to import diffusers.pipelines..."). hasattr
+ # absorbs AttributeError only, so the RuntimeError escaped this guard exactly the way a
+ # missing diffusers used to: not the ValueError the routes map to 400, so a bare 500 with the
+ # message lost.
+ import types
+
+ from core.inference.diffusion_families import assert_pipeline_class_available
+
+ class _LazyModule(types.ModuleType):
+ __version__ = "0.40.0"
+
+ def __getattr__(self, name):
+ raise RuntimeError(f"Failed to import diffusers.pipelines.{name.lower()}")
+
+ monkeypatch.setitem(sys.modules, "diffusers", _LazyModule("diffusers"))
+ assert assert_pipeline_class_available("ZImagePipeline", "z-image") is None
+
+
def test_cached_pipeline_needs_a_detectable_image_family(monkeypatch):
# A top-level model_index.json only proves the repo is a diffusers pipeline: an unsloth-hosted pipeline of a class this backend
# cannot assemble cleared the trust gate, was advertised, then failed validate_load_request. Both gates now, like the video branch.