[Fix] Resolve deprecated pipeline modules in from_pretrained
What does this PR do?
Fixes #14008
The bug
DiffusionPipeline.from_pretrained raises a spurious ValueError when loading any pipeline that was relocated under diffusers.pipelines.deprecated (for example Wuerstchen, moved in #13157):
from diffusers import DiffusionPipeline
DiffusionPipeline.from_pretrained("warp-ai/wuerstchen-prior")
ValueError: prior/wuerstchen.py as defined in `model_index.json` does not exist in
warp-ai/wuerstchen-prior and is not a module in 'diffusers/pipelines'.
Root cause
This is a bug in the deprecation mechanism itself, not in the deprecated pipeline.
_get_custom_components_and_folders decides whether a component refers to a built-in module via hasattr(diffusers.pipelines, module_candidate). When a pipeline is moved to diffusers.pipelines.deprecated, its module name is no longer a top-level attribute of diffusers.pipelines (deprecated modules are nested under the deprecated key in _import_structure). So hasattr returns False, the component is mistaken for a missing custom module, and loading fails.
The fix suggested in the issue (hasattr(diffusers.pipelines.deprecated, module_candidate)) does not work: diffusers.pipelines.deprecated is a _LazyModule, so the module name (wuerstchen) is not an attribute until it is imported; only the lazily exposed class names are. hasattr therefore still returns False.
Fix
Resolve the module spec under the deprecated namespace without importing it, via importlib.util.find_spec("diffusers.pipelines.deprecated.<module>"). This is import side-effect free (no heavy import is triggered) and correctly recognizes relocated modules. Genuinely missing modules still raise, since find_spec returns None for them.
Tests
Added GetCustomComponentsAndFoldersTests in tests/pipelines/test_pipeline_utils.py (the existing home for pipeline_loading_utils unit tests). All are function level and require no network or GPU.
$ python -m pytest tests/pipelines/test_pipeline_utils.py::GetCustomComponentsAndFoldersTests -q
4 passed, 2 warnings in 2.28s
Reverting only the production change reproduces the issue (from_pretrained raises the ValueError above) and makes the new tests fail, confirming they guard the bug. ruff check and ruff format --check pass on the changed files.
Before submitting
- [x] Did you use an AI agent to help with this PR? Yes, Claude Opus 4.8. The diff was self-reviewed against
.ai/review-rules.md, and I understand and take responsibility for the change. - [x] Did you read the contributor guideline?
- [x] Did you write any new necessary tests?
Claude: Thanks for this — the _is_deprecated_pipeline_module check does get past the early ValueError in _get_custom_components_and_folders when loading e.g. warp-ai/wuerstchen-prior.
One thing to flag: with this PR applied, loading a deprecated pipeline still doesn't complete. get_class_obj_and_candidates later does importlib.import_module(library_name) with the bare module name, so it then raises:
ModuleNotFoundError: No module named 'wuerstchen'
(pipeline_loading_utils.py, in the else: # else we just import it from the library. branch.) The module resolves only under diffusers.pipelines.deprecated.<library_name>, so that import path may need the same deprecated-namespace fallback for the fix to make these pipelines actually loadable end-to-end.
Drafted by Claude.