huggingface/transformers · error · ValueError
Unable to convert output '{key}' (type: {type(value).__name_
Error message
Unable to convert output '{key}' (type: {type(value).__name__}) to tensor: {str(e)}
You can try:
1. Use padding=True to ensure all outputs have the same shape
2. Set return_tensors=None to return Python objects instead of tensors What it means
The generic tensor-conversion failure in BatchFeature.convert_to_tensors: as_tensor(value) raised for a non-overflow key (ragged nested lists, mismatched lengths across the batch, unsupported element types). The error names the key, its python type, and the underlying message, and suggests padding or returning python objects.
Source
Thrown at src/transformers/feature_extraction_utils.py:206
# 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:
args (`Tuple`):
Will be passed to the `to(...)` function of the tensors.
kwargs (`Dict`, *optional*):
Will be passed to the `to(...)` function of the tensors.View on GitHub (pinned to a597f97485)
Solutions
- Add padding=True (plus max_length if a fixed size is needed) so all sequences align
- Use return_tensors=None when you need to keep ragged python/numpy structures
- Verify each key in the batch has a consistent shape before conversion
Example fix
# before fe(list_of_variable_length_audio, return_tensors="pt") # after fe(list_of_variable_length_audio, return_tensors="pt", padding=True)
Defensive patterns
Strategy: fallback
Validate before calling
def lengths_uniform(values) -> bool:
import numpy as np
if isinstance(values, (list, tuple)) and values and isinstance(values[0], (list, tuple, np.ndarray)):
return len({len(v) for v in values}) == 1
return True Try / catch
try:
out = fe(batch, return_tensors="pt")
except ValueError as e:
if "to tensor" in str(e):
out = fe(batch, return_tensors="pt", padding=True)
else:
raise Prevention
- Always pad variable-length batches before tensor conversion
- Fall back to return_tensors=None for ragged data
- Shape-check each key before requesting tensors
When it happens
Trigger: Calling a feature extractor with return_tensors='pt'/'np' on an unpadded ragged batch (sequences of different lengths), or a value that is a nested list of inconsistent shapes for a key other than overflowing_values.
Common situations: Batching variable-length audio without padding=True; mixing single example and batched arrays; passing strings or object arrays as feature values.
Related errors
- Unable to create tensor for '{key}' with overflowing values
- Indexing with integers is not available when using Python ba
- Attempting to cast a BatchFeature to type {str(arg)}. This i
- You should supply an instance of `transformers.BatchFeature`
- type of {first_element} unknown: {type(first_element)}. Shou
AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14).
Data as JSON: /api/errors/c35fc9cb199568bf.
Report an issue: GitHub.