Skip to content

fix(utils): include original cause in _LazyModule ModuleNotFoundError - #47899

Closed
Sayyam-Akram wants to merge 2 commits into
huggingface:mainfrom
Sayyam-Akram:fix/lazy-module-import-error-message
Closed

fix(utils): include original cause in _LazyModule ModuleNotFoundError#47899
Sayyam-Akram wants to merge 2 commits into
huggingface:mainfrom
Sayyam-Akram:fix/lazy-module-import-error-message

Conversation

@Sayyam-Akram

@Sayyam-Akram Sayyam-Akram commented Aug 11, 2026

Copy link
Copy Markdown

CI

What is the purpose of this PR?

Fixes #47893

If an optional module like accelerate or sentencepiece is not installed in your system, and the import fails, for example when using MistralForSequenceClassification, then _LazyModule.__getattr__ will catch the underlying ModuleNotFoundError and raise a generic error message. This obscures what is really needed from the user.

This PR enhances the _LazyModule.__getattr__ function with the original exception information (Caused by: {e}). This provides instant, unmistakable diagnostic information of what package is missing.

I've also written a unit test in tests/utils/test_import_utils.py to make sure that exception causes are preserved.

Code Agent Policy

  • (Only for the first time) I confirm that this PR description and code is not written by an LLM or code agent

Before submitting

Who can review?

@github-actions

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 31485637225:2
Result: success | Jobs: 2 | Tests: 21 | Failures: 1 | Duration: 1m 59s

@Rocketknight1

Copy link
Copy Markdown
Member

this is the most code agent code i've ever seen in my life and you're blocked

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ModuleNotFoundError: Could not import module 'MistralForSequenceClassification'

2 participants