huggingface/transformers · error · ValueError

{auto_class} is not a valid auto class.

Error message

{auto_class} is not a valid auto class.

What it means

ValueError from PreTrainedConfig.register_for_auto_class when the given auto_class name (string or class __name__) does not exist as an attribute of transformers.models.auto. Only classes actually exported by the auto module (e.g. AutoConfig, AutoModelForCausalLM) can back a registration.

Source

Thrown at src/transformers/configuration_utils.py:1270

    @classmethod
    def register_for_auto_class(cls, auto_class="AutoConfig"):
        """
        Register this class with a given auto class. This should only be used for custom configurations as the ones in
        the library are already mapped with `AutoConfig`.



        Args:
            auto_class (`str` or `type`, *optional*, defaults to `"AutoConfig"`):
                The auto class to register this new configuration with.
        """
        if not isinstance(auto_class, str):
            auto_class = auto_class.__name__

        import transformers.models.auto as auto_module

        if not hasattr(auto_module, auto_class):
            raise ValueError(f"{auto_class} is not a valid auto class.")

        cls._auto_class = auto_class

    @classmethod
    def is_remote_code(cls) -> bool:
        """Return whether the current config is custom code, i.e. code loaded from the hub, or class that we just
        registered via `register_for_auto_class`."""
        return cls._auto_class is not None

    @classmethod
    def is_custom_code(cls) -> bool:
        """Return whether the current config is custom code, i.e. either code loaded from the hub, or defined in any
        user-specific module/session."""
        return cls.is_remote_code() or not cls.__module__.startswith("transformers.")

    def _get_generation_parameters(self) -> dict[str, Any]:
        """
        Checks if there are generation parameters in `PreTrainedConfig` instance. Note that

View on GitHub (pinned to a597f97485)

Solutions

  1. Use a valid auto class name, e.g. "AutoConfig" for configs or "AutoModelForCausalLM" for models.
  2. Check availability first: import transformers.models.auto as auto; hasattr(auto, name).
  3. Verify the name is exported in your installed Transformers version's auto module.

Example fix

// before
MyConfig.register_for_auto_class("AutoModelForTextGeneration")  # ValueError

// after
MyConfig.register_for_auto_class("AutoModelForCausalLM")
Defensive patterns

Strategy: validation

Validate before calling

import transformers.models.auto as auto
valid = [n for n in dir(auto) if n.startswith("Auto")]
if auto_class_name not in valid:
    raise ValueError(f"{auto_class_name!r} not in {valid}")
MyConfig.register_for_auto_class(auto_class_name)

Type guard

def is_valid_auto_class(name: str) -> bool:
    import transformers.models.auto as auto
    return hasattr(auto, name)

Prevention

When it happens

Trigger: MyConfig.register_for_auto_class("AutoModelForCausulLM") (typo), passing "AutoModel"-style names that are not in the auto module, or passing a custom class whose name differs from any auto export.

Common situations: Registering custom remote-code models for auto-loading; renamed or hypothetical auto classes; code written against a different Transformers version where an auto class does not exist.

Related errors


AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14). Data as JSON: /api/errors/c26f84b9c62b6ba5. Report an issue: GitHub.