fix: lazy load top-level AutoModel exports#3290
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces lazy loading for AutoModel and AutoFrontend in funasr/__init__.py to allow importing the package and checking its version without requiring PyTorch. A test has been added to verify this behavior. The reviewer suggested making the PyTorch missing error message dynamic to correctly reference either AutoModel or AutoFrontend depending on which attribute was accessed.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| if error.name == "torch": | ||
| raise ModuleNotFoundError( | ||
| "FunASR requires PyTorch before using AutoModel. Install a " | ||
| "platform-appropriate torch build first, for example: " | ||
| "`python -m pip install torch torchaudio --index-url " | ||
| "https://download.pytorch.org/whl/cu126` for CUDA 12.6, " | ||
| "or follow https://pytorch.org/get-started/locally/.", | ||
| name="torch", | ||
| ) from error |
There was a problem hiding this comment.
The error message is currently hardcoded to mention AutoModel. Since _LAZY_EXPORTS also contains AutoFrontend, attempting to import AutoFrontend when PyTorch is missing will raise an error message that confusingly refers to AutoModel.
We can make this dynamic by using the name parameter passed to __getattr__ to ensure the error message is accurate for whichever component is being imported.
| if error.name == "torch": | |
| raise ModuleNotFoundError( | |
| "FunASR requires PyTorch before using AutoModel. Install a " | |
| "platform-appropriate torch build first, for example: " | |
| "`python -m pip install torch torchaudio --index-url " | |
| "https://download.pytorch.org/whl/cu126` for CUDA 12.6, " | |
| "or follow https://pytorch.org/get-started/locally/.", | |
| name="torch", | |
| ) from error | |
| if error.name == "torch": | |
| raise ModuleNotFoundError( | |
| f"FunASR requires PyTorch before using {name}. Install a " | |
| "platform-appropriate torch build first, for example: " | |
| "'python -m pip install torch torchaudio --index-url " | |
| "https://download.pytorch.org/whl/cu126' for CUDA 12.6, " | |
| "or follow https://pytorch.org/get-started/locally/.", | |
| name="torch", | |
| ) from error |
Summary
Fixes a first-install onboarding blocker found while smoke-testing the newly published
funasr==1.3.20wheel in a fresh environment.pip install funasr==1.3.20succeeds, but because the package intentionally does not install a platform-specific PyTorch build,import funasrimmediately fails at the top-levelfrom funasr.auto.auto_model import AutoModelimport.This PR keeps the current dependency strategy and makes the top-level exports lazy instead:
import funasrandfunasr.__version__work without torch;from funasr import AutoModelandAutoFrontendstill resolve normally when dependencies are present;AutoModelraises a clear PyTorch install hint;1.3.21and records the fix in EN/ZH README release notes.Validation
python3 -m pytest tests/test_top_level_import.py tests/test_cli.py -q-> 5 passedpython3 -m compileall -q funasr/__init__.py tests/test_top_level_import.pygit diff --checkpython3 -m buildtwine check dist/*-> wheel and sdist passeddist/funasr-1.3.21-py3-none-any.whlwith--no-depsin a fresh venv:import funasrreports1.3.21;from funasr import AutoModelraises the expected torch install hint.import funasrreports1.3.21; accessingAutoModelraises the expected torch install hint because torch is still not part ofinstall_requires.