huggingface/transformers · error · ValueError
type of {first_element} unknown: {type(first_element)}. Shou
Error message
type of {first_element} unknown: {type(first_element)}. Should be one of a python, numpy, or pytorch object. What it means
When return_tensors is not given, the feature extractor infers the output format from the type of the first element of the required input. If that element is neither a torch tensor, python scalar/list/tuple, nor a numpy array, the type is unknown and conversion is refused with this ValueError.
Source
Thrown at src/transformers/feature_extraction_sequence_utils.py:164
# and rebuild them afterwards if no return_tensors is specified
# Note that we lose the specific device the tensor may be on for PyTorch
first_element = required_input[0]
if isinstance(first_element, (list, tuple)):
# first_element might be an empty list/tuple in some edge cases so we grab the first non empty element.
index = 0
while len(required_input[index]) == 0:
index += 1
if index < len(required_input):
first_element = required_input[index][0]
if return_tensors is None:
if is_torch_tensor(first_element):
return_tensors = "pt"
elif isinstance(first_element, (int, float, list, tuple, np.ndarray)):
return_tensors = "np"
else:
raise ValueError(
f"type of {first_element} unknown: {type(first_element)}. "
"Should be one of a python, numpy, or pytorch object."
)
for key, value in processed_features.items():
if isinstance(value[0], (int, float)):
processed_features[key] = to_numpy(value)
elif not isinstance(value, np.ndarray):
# An already-batched numpy array can be used as-is; splitting it
# into a list of per-example arrays is pure overhead and is very
# slow for large inputs (e.g. long audio).
processed_features[key] = [to_numpy(v) for v in value]
# Convert padding_strategy in PaddingStrategy
padding_strategy = self._get_padding_strategies(padding=padding, max_length=max_length)
required_input = processed_features[self.model_input_names[0]]
View on GitHub (pinned to a597f97485)
Solutions
- Convert the input to a plain numpy array or list of floats before calling the feature extractor
- Explicitly pass return_tensors='pt' (or 'np') so no inference from element type is needed
- Check for None or empty values in the raw audio list and drop/fix them
Example fix
# before fe(series_from_pandas) # pandas Series element # after fe(np.asarray(series_from_pandas, dtype=np.float32))
Defensive patterns
Strategy: validation
Validate before calling
import numpy as np
def to_float_array(x):
if isinstance(x, (np.ndarray, list, tuple)) and not hasattr(x, "to_numpy"):
return x
return np.asarray(x, dtype=np.float32) Type guard
def is_supported_element(x) -> bool:
import torch
return torch.is_tensor(x) or isinstance(x, (int, float, list, tuple, np.ndarray)) Try / catch
try:
fe(audio_list)
except ValueError as e:
if "type of" in str(e) and "unknown" in str(e):
audio_list = [np.asarray(a, dtype=np.float32) for a in audio_list]
fe(audio_list)
else:
raise Prevention
- Normalize audio to np.float32 arrays at the pipeline boundary
- Pass return_tensors explicitly to skip type inference
- Guard against None entries from loaders
When it happens
Trigger: Feeding audio/features whose first element is an exotic type — e.g. a pandas Series, a jax array, a string, a TensorFlow tensor, or None — to a sequence feature extractor without specifying return_tensors.
Common situations: Loading audio via pandas/numpy-adjacent loaders that wrap arrays in containers; None entries from failed loads; passing already-batched nested structures with mixed types.
Related errors
- You should supply an instance of `transformers.BatchFeature`
- When setting ``truncation=True``, make sure that ``max_lengt
- When setting ``padding={PaddingStrategy.MAX_LENGTH}``, make
- only a single or a list of entries is supported but got type
- Unable to create tensor for '{key}' with overflowing values
AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14).
Data as JSON: /api/errors/ae179e0abcf88bf1.
Report an issue: GitHub.