Repository: huggingface/transformers
File: src/transformers/pipelines/base.py (Python)

Code before:
                transformers_module = importlib.import_module("transformers")
                _class = getattr(transformers_module, architecture, None)
                if _class is not None:
                    classes.append(_class)
            class_tuple = class_tuple + tuple(classes)

        if len(class_tuple) == 0:
            raise ValueError(f"Pipeline cannot infer suitable model classes from {model}")

        all_traceback = {}
        for model_class in class_tuple:
            kwargs = model_kwargs.copy()

            try:
                model = model_class.from_pretrained(model, **kwargs)
                # Stop loading on the first successful load.
                break
            except (OSError, ValueError, TypeError, RuntimeError) as e:
                # `from_pretrained` may raise a `TypeError` or `RuntimeError` when the requested `dtype`
                # is not supported on the execution device (e.g. bf16 on a consumer GPU). We capture those so
                # we can transparently retry the load in float32 before surfacing an error to the user.
                fallback_tried = False

                # Check if it's a safetensors-related error and retry without safetensors
                error_msg = str(e).lower()
                if isinstance(e, OSError) and "safetensors" in error_msg:
                    fallback_tried = True
                    no_safetensors_kwargs = kwargs.copy()
                    no_safetensors_kwargs["use_safetensors"] = False
                    try:
                        model = model_class.from_pretrained(model, **no_safetensors_kwargs)
                        logger.warning(
                            "Falling back to loading without safetensors because the model does not have safetensors files."
                        )
                        break
                    except Exception:
                        # If it still fails, capture the traceback and continue to the next class.
                        all_traceback[model_class.__name__] = traceback.format_exc()
                        continue

                # Check if it's a dtype-related error and retry with float32
                if "dtype" in kwargs:
                    import torch

                    fallback_tried = True
                    fp32_kwargs = kwargs.copy()
                    fp32_kwargs["dtype"] = torch.float32

                    try:
                        model = model_class.from_pretrained(model, **fp32_kwargs)
                        logger.warning(

Review comment (suggestion):
missing `and not fallback_tried` ? and perhaps it would be good to have a more strict check on the error message: we dont want this to be used if there is an underlying issue w/ safetensors

Code after:
                _class = getattr(transformers_module, architecture, None)
                if _class is not None:
                    classes.append(_class)
            class_tuple = class_tuple + tuple(classes)

        if len(class_tuple) == 0:
            raise ValueError(f"Pipeline cannot infer suitable model classes from {model}")

        all_traceback = {}
        for model_class in class_tuple:
            kwargs = model_kwargs.copy()

            try:
                model = model_class.from_pretrained(model, **kwargs)
                # Stop loading on the first successful load.
                break
            except (OSError, ValueError, TypeError, RuntimeError):
                # `from_pretrained` may raise a `TypeError` or `RuntimeError` when the requested `dtype`
                # is not supported on the execution device (e.g. bf16 on a consumer GPU). We capture those so
                # we can transparently retry the load in float32 before surfacing an error to the user.
                fallback_tried = False
                if "dtype" in kwargs:
                    import torch

                    fallback_tried = True
                    fp32_kwargs = kwargs.copy()
                    fp32_kwargs["dtype"] = torch.float32

                    try:
                        model = model_class.from_pretrained(model, **fp32_kwargs)
                        logger.warning(
                            "Falling back to torch.float32 because loading with the original dtype failed on the"
                            " target device."
                        )
                        break
                    except Exception:
                        # If it still fails, capture the traceback and continue to the next class.
                        all_traceback[model_class.__name__] = traceback.format_exc()
                        continue

                # If no fallback was attempted or it also failed, record the original traceback.
                if not fallback_tried:
                    all_traceback[model_class.__name__] = traceback.format_exc()
                continue

        if isinstance(model, str):
            error = ""
            for class_name, trace in all_traceback.items():
                error += f"while loading with {class_name}, an error is thrown:\n{trace}\n"
            raise ValueError(
                f"Could not load model {model} with any of the following classes: {class_tuple}. See the original errors:\n\n{error}\n"