Skip to content

Conversation

@yashwantbezawada
Copy link
Contributor

@yashwantbezawada yashwantbezawada commented Nov 6, 2025

What does this PR do?

Fixes #41981

I ran into this issue while testing with optimum.quanto. The function was crashing with KeyError when checking for packages with dotted names.

The problem is in import_utils.py line 56 - when it tries to look up the package in PACKAGE_DISTRIBUTION_MAPPING, packages with dots in the name (like optimum.quanto) aren't always there. The mapping might only have optimum as a key, not optimum.quanto.

The function already has fallback logic to handle cases where package metadata can't be found - it imports the package directly and checks for __version__. But the exception handler only caught PackageNotFoundError, not KeyError.

I added KeyError to the exception handler so the fallback works for both cases. Now when a package name isn't in the mapping, it'll use the same fallback path instead of crashing.

This is a one-line change that handles the edge case without affecting normal operation. The existing tests should still pass since the behavior for normal packages is unchanged.

Fixes huggingface#41981

When checking for packages with dotted names like 'optimum.quanto', the
function was crashing with KeyError because these packages aren't always
in PACKAGE_DISTRIBUTION_MAPPING (it might only have 'optimum' as a key,
not 'optimum.quanto').

The function already had fallback logic to handle cases where package
metadata can't be found - it tries to import the package directly. But
the exception handler only caught PackageNotFoundError, not KeyError.

This adds KeyError to the exception handler so the fallback works for
both cases.
@yashwantbezawada yashwantbezawada force-pushed the fix/keyerror-is-package-available-41981 branch from 9af9090 to c7512b1 Compare November 6, 2025 02:17
@Rocketknight1
Copy link
Member

Yes, this makes sense to me! Since it should only have an impact in situations that were already causing a crash, I think it's safe to merge this.

@Rocketknight1 Rocketknight1 enabled auto-merge (squash) November 6, 2025 13:19
@Rocketknight1 Rocketknight1 merged commit 5c1df12 into huggingface:main Nov 6, 2025
23 checks passed
@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Abdennacer-Badaoui pushed a commit to Abdennacer-Badaoui/transformers that referenced this pull request Nov 10, 2025
…uggingface#42050)

Fixes huggingface#41981

When checking for packages with dotted names like 'optimum.quanto', the
function was crashing with KeyError because these packages aren't always
in PACKAGE_DISTRIBUTION_MAPPING (it might only have 'optimum' as a key,
not 'optimum.quanto').

The function already had fallback logic to handle cases where package
metadata can't be found - it tries to import the package directly. But
the exception handler only caught PackageNotFoundError, not KeyError.

This adds KeyError to the exception handler so the fallback works for
both cases.

Co-authored-by: Yashwant Bezawada <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

KeyError: 'optimum.quanto' for _is_package_available

3 participants