transformers - ✅(Solved) Fix modeling_utils unsafely accesses sys.modules[] [3 pull requests, 6 comments, 4 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
huggingface/transformers#45003Fetched 2026-04-08 01:30:54
View on GitHub
Comments
6
Participants
4
Timeline
26
Reactions
0
Author
Timeline (top)
commented ×6cross-referenced ×5mentioned ×5subscribed ×5

Fix Action

Fixed

PR fix notes

PR #44978: fix: handle absent sys.modules entry in modeling_utils

Description (problem / solution / changelog)

What does this PR do?

_can_set_attn_implementation and _can_set_experts_implementation both do a direct subscript lookup into sys.modules:

class_module = sys.modules[cls.__module__]

If the module is not registered under its dotted name in sys.modules — which can happen with lazy loaders, frozen importers, or certain packaging environments on Windows — this raises a KeyError instead of returning False as intended.

The existing comment already acknowledges this class of problem ("This can happen for a custom model in a jupyter notebook or repl for example") but the file guard only ran after the subscript, so it never had a chance to fire.

First introduced: commit 0642963 (PR #42697, merged January 5, 2026).

Real-world traceback reported by Griptape Nodes users:

KeyError: 'transformers.models.clip.modeling_clip'
  modeling_utils.py, in _can_set_experts_implementation
    class_module = sys.modules[cls.__module__]

Reproducing code:

import sys
from transformers.models.clip.modeling_clip import CLIPTextModel, CLIPTextConfig

# Simulate condition where module is absent from sys.modules
del sys.modules["transformers.models.clip.modeling_clip"]

config = CLIPTextConfig()
CLIPTextModel(config)  # KeyError before this fix, succeeds after

Code Agent Policy

  • I confirm that this is not a pure code agent PR.

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline, Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the documentation guidelines, and here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag members/contributors who may be interested in your PR.

@ArthurZucker (original author) @CyrilVallez (model loading)

<!-- Your PR will be replied to more quickly if you can figure out the right person to tag with @ If you know how to use git blame, that is the easiest way, otherwise, here is a rough guide of **who to tag**. Please tag fewer than 3 people. Models: - text models: @ArthurZucker @Cyrilvallez - vision models: @yonigozlan @molbap - audio models: @eustlb @ebezzam @vasqu - multimodal models: @zucchini-nlp - graph models: @clefourrier Library: - generate: @zucchini-nlp (visual-language models) or @gante (all others) - continuous batching: @remi-or @ArthurZucker @McPatate - pipelines: @Rocketknight1 - tokenizers: @ArthurZucker and @itazap - trainer: @SunMarc - attention: @vasqu @ArthurZucker @CyrilVallez - model loading (from pretrained, etc): @CyrilVallez - distributed: @3outeille @ArthurZucker - CIs: @ydshieh Integrations: - ray/raytune: @richardliaw, @amogkam - Big Model Inference: @SunMarc - quantization: @SunMarc - kernels: @drbh - peft: @BenjaminBossan @githubnemo Devices/Backends: - AMD ROCm: @ivarflakstad - Intel XPU: @IlyasMoutawwakil - Ascend NPU: @ivarflakstad Documentation: @stevhliu Research projects are not maintained and should be taken as is. -->

Closes https://github.com/huggingface/transformers/issues/45003

Changed files

  • src/transformers/modeling_utils.py (modified, +4/-4)

PR #45015: fix: guard sys.modules access in _can_set_attn/experts_implementation

Description (problem / solution / changelog)

What does this PR do?

Fixes a KeyError in _can_set_attn_implementation and _can_set_experts_implementation when a model's module is absent from sys.modules.

Fixes #45003

Root Cause

Both _can_set_attn_implementation (line 1951) and _can_set_experts_implementation (line 1970) use a direct dictionary subscript:

class_module = sys.modules[cls.__module__]

This raises KeyError when the module is not registered in sys.modules — which happens with lazy loaders, frozen importers, or tools like Griptape Nodes that manipulate the module registry.

The existing comment on the very next line already acknowledges this class of scenario ("This can happen for a custom model in a jupyter notebook or repl"), but the __file__ guard only runs after the dictionary access, so it never fires.

Fix

Replace sys.modules[cls.__module__] with sys.modules.get(cls.__module__) and add a class_module is None check alongside the existing hasattr guard. Both functions now gracefully return False instead of crashing.

Tests

Added TestCanSetImplementationWithMissingSysModule with 3 tests:

  • _can_set_attn_implementation returns False when module is missing from sys.modules
  • _can_set_experts_implementation returns False when module is missing from sys.modules
  • Full model initialization succeeds when module is missing from sys.modules

All existing TestAttentionImplementation tests continue to pass.

Code Agent Policy

  • I confirm that this is not a pure code agent PR.

Changed files

  • src/transformers/modeling_utils.py (modified, +8/-6)
  • tests/utils/test_modeling_utils.py (modified, +47/-0)

Code Example

import sys
from transformers.models.clip.modeling_clip import CLIPTextModel, CLIPTextConfig

# Simulate condition where module is absent from sys.modules
del sys.modules["transformers.models.clip.modeling_clip"]

config = CLIPTextConfig()
CLIPTextModel(config)  # KeyError before this fix, succeeds after
RAW_BUFFERClick to expand / collapse

System Info

  • transformers version: 5.3.0.dev0
  • Platform: macOS-26.3.1-arm64-arm-64bit
  • Python version: 3.11.12
  • Huggingface_hub version: 1.6.0
  • Safetensors version: 0.7.0
  • Accelerate version: not installed
  • Accelerate config: not found
  • DeepSpeed version: not installed
  • PyTorch version (accelerator?): 2.5.1 (NA)
  • Using distributed or parallel set-up in script?: N/A

Who can help?

@Cyrilvallez @ArthurZucker

Information

  • The official example scripts
  • My own modified scripts

Tasks

  • An officially supported task in the examples folder (such as GLUE/SQuAD, ...)
  • My own task or dataset (give details below)

Reproduction

Run this:

import sys
from transformers.models.clip.modeling_clip import CLIPTextModel, CLIPTextConfig

# Simulate condition where module is absent from sys.modules
del sys.modules["transformers.models.clip.modeling_clip"]

config = CLIPTextConfig()
CLIPTextModel(config)  # KeyError before this fix, succeeds after

I submitted a fix PR for this but it was incorrectly closed as AI slop. While I did use Claude to help me debug and isolate the problem, I can assure you this is a real fix submitted by a real human with real customers impacted by this bug.

Expected behavior

Uncaught KeyError exceptions are not raised by module importing logic

extent analysis

Fix Plan

To resolve the KeyError exception, we need to ensure that the CLIPTextModel is properly registered in the sys.modules dictionary.

Here are the steps to fix the issue:

  • Check if the module is already loaded in sys.modules before attempting to import it.
  • If the module is not loaded, import it manually using __import__.

Example Code

import sys
from transformers.models.clip.modeling_clip import CLIPTextModel, CLIPTextConfig

# Check if module is loaded
if "transformers.models.clip.modeling_clip" not in sys.modules:
    # Import module manually if not loaded
    __import__("transformers.models.clip.modeling_clip")

config = CLIPTextConfig()
CLIPTextModel(config)  # Should succeed without KeyError

Alternatively, you can also use the importlib module to import the module dynamically:

import importlib
import sys
from transformers.models.clip.modeling_clip import CLIPTextModel, CLIPTextConfig

# Check if module is loaded
if "transformers.models.clip.modeling_clip" not in sys.modules:
    # Import module manually if not loaded
    importlib.import_module("transformers.models.clip.modeling_clip")

config = CLIPTextConfig()
CLIPTextModel(config)  # Should succeed without KeyError

Verification

To verify that the fix worked, run the provided reproduction code and check that it no longer raises a KeyError exception.

Extra Tips

  • Make sure to handle potential exceptions that may occur during the manual import process.
  • Consider adding a try-except block to catch and handle any import-related errors.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

Uncaught KeyError exceptions are not raised by module importing logic

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING