Skip to content

modeling_utils unsafely accesses sys.modules[] #45003

@cjkindel

Description

@cjkindel

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions