huggingface/transformers · error · ValueError

Unable to create tensor for '{key}' with overflowing values

Error message

Unable to create tensor for '{key}' with overflowing values of different lengths. Original error: {str(e)}

What it means

During convert_to_tensors, the special 'overflowing_values' key (chunks produced by windowed feature extractors) must itself stack into a tensor. Overflow chunks commonly have different lengths, which makes stacking fail; that underlying exception is wrapped in this ValueError with the original message attached.

Source

Thrown at src/transformers/feature_extraction_utils.py:202

        )

        # Do the tensor conversion in batch
        for key, value in self.items():
            # Skip keys explicitly marked for no conversion
            if skip_tensor_conversion and key in skip_tensor_conversion:
                continue

            # Skip values that are not array-like
            if not _is_tensor_or_array_like(value):
                continue

            try:
                if not is_tensor(value):
                    tensor = as_tensor(value)
                    self[key] = tensor
            except Exception as e:
                if key == "overflowing_values":
                    raise ValueError(
                        f"Unable to create tensor for '{key}' with overflowing values of different lengths. "
                        f"Original error: {str(e)}"
                    ) from e
                raise ValueError(
                    f"Unable to convert output '{key}' (type: {type(value).__name__}) to tensor: {str(e)}\n"
                    f"You can try:\n"
                    f"  1. Use padding=True to ensure all outputs have the same shape\n"
                    f"  2. Set return_tensors=None to return Python objects instead of tensors"
                ) from e

        return self

    def to(self, *args, **kwargs) -> "BatchFeature":
        """
        Send all values to device by calling `v.to(*args, **kwargs)` (PyTorch only). This should support casting in
        different `dtypes` and sending the `BatchFeature` to a different `device`.

        Args:

View on GitHub (pinned to a597f97485)

Solutions

  1. Enable padding (padding=True with an appropriate max_length) so all overflow chunks have equal length before tensor conversion
  2. Or set return_tensors=None and handle the list of chunks manually
  3. Ensure truncation settings produce uniform chunk sizes when that is intended

Example fix

# before
fe(audio, return_tensors="pt")  # ragged overflow chunks

# after
fe(audio, return_tensors="pt", padding=True, max_length=fe.chunk_length)
Defensive patterns

Strategy: fallback

Validate before calling

def overflow_lengths_uniform(fe_out) -> bool:
    ov = fe_out.get("overflowing_values")
    if ov is None:
        return True
    return len({getattr(c, "shape", ())[0] if hasattr(c, "shape") else len(c) for c in ov}) == 1

Try / catch

try:
    out = fe(audio, return_tensors="pt")
except ValueError as e:
    if "overflowing_values" in str(e):
        out = fe(audio, return_tensors=None)  # keep chunks as lists
    else:
        raise

Prevention

When it happens

Trigger: Using a windowing feature extractor (e.g. Wav2Vec2 with return_attention_mask and long audio producing overflowing chunks) combined with return_tensors='pt' where the chunks have unequal lengths.

Common situations: Long-audio chunking pipelines; switching padding off but keeping tensor output; version changes where overflow chunk lengths stopped being normalized.

Related errors


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