huggingface/transformers · error · ValueError

Attempting to cast a BatchFeature to type {str(arg)}. This i

Error message

Attempting to cast a BatchFeature to type {str(arg)}. This is not supported.

What it means

BatchFeature.to() parses its first positional argument as either a torch dtype, a device (string, torch.device, or int index), or unknown. If the first arg matches none of those (e.g. a numpy dtype, a module, a list), the code refuses with this ValueError rather than guessing a conversion.

Source

Thrown at src/transformers/feature_extraction_utils.py:246

            [`BatchFeature`]: The same instance after modification.
        """
        requires_backends(self, ["torch"])
        import torch

        device = kwargs.get("device")
        non_blocking = kwargs.get("non_blocking", False)
        # Check if the args are a device or a dtype
        if device is None and len(args) > 0:
            # device should be always the first argument
            arg = args[0]
            if is_torch_dtype(arg):
                # The first argument is a dtype
                pass
            elif isinstance(arg, str) or is_torch_device(arg) or isinstance(arg, int):
                device = arg
            else:
                # it's something else
                raise ValueError(f"Attempting to cast a BatchFeature to type {str(arg)}. This is not supported.")

        # We cast only floating point tensors to avoid issues with tokenizers casting `LongTensor` to `FloatTensor`
        def maybe_to(v):
            # check if v is a floating point tensor
            if isinstance(v, torch.Tensor) and torch.is_floating_point(v):
                # cast and send to device
                return v.to(*args, **kwargs)
            elif isinstance(v, torch.Tensor) and device is not None:
                return v.to(device=device, non_blocking=non_blocking)
            # recursively handle lists and tuples
            elif isinstance(v, (list, tuple)):
                return type(v)(maybe_to(item) for item in v)
            else:
                return v

        self.data = {k: maybe_to(v) for k, v in self.items()}
        return self

View on GitHub (pinned to a597f97485)

Solutions

  1. Use a torch dtype or device: batch.to(torch.float32) or batch.to('cuda:0')
  2. Convert numpy dtypes first: batch.to(torch.from_numpy(np.zeros(1, np.float32)).dtype) or simply torch.float32
  3. Keep only floating-point tensors castable — note ints are moved to device but not dtype-cast by design

Example fix

# before
batch.to(np.float32)

# after
import torch
batch.to(torch.float32)
Defensive patterns

Strategy: type-guard

Validate before calling

import torch

def to_batch(batch, target):
    if not (torch.is_tensor(target) or isinstance(target, (str, int)) or str(target).startswith("cuda")):
        target = torch.float32  # or raise
    return batch.to(target)

Type guard

def is_valid_to_arg(arg) -> bool:
    import torch
    if isinstance(arg, torch.dtype):
        return True
    return isinstance(arg, (str, int, torch.device))

Try / catch

try:
    batch.to(np_dtype)
except ValueError as e:
    if "not supported" in str(e):
        import torch
        batch.to({np.float32: torch.float32, np.float16: torch.float16}[np_dtype])
    else:
        raise

Prevention

When it happens

Trigger: Calling batch.to(np.float32), batch.to(some_object), or chaining .to() with an argument shape modeled on another library where the first parameter is not a device/dtype.

Common situations: Passing numpy dtypes when porting numpy-heavy code; passing a tensor as the first arg; utilities that forward arbitrary kwargs into .to().

Related errors


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