huggingface/transformers · error · ValueError
Some items in the output dictionary have a different batch s
Error message
Some items in the output dictionary have a different batch size than others.
What it means
Before per-example truncation/padding, the feature extractor verifies every value in the processed-features dict has the same batch dimension (len == batch size of the main input). Any key whose first dimension differs (a scalar, a single unbatched example, or a ragged list) makes the batch inconsistent and raises this ValueError.
Source
Thrown at src/transformers/feature_extraction_sequence_utils.py:185
)
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]]
batch_size = len(required_input)
if not all(len(v) == batch_size for v in processed_features.values()):
raise ValueError("Some items in the output dictionary have a different batch size than others.")
truncated_inputs = []
for i in range(batch_size):
inputs = {k: v[i] for k, v in processed_features.items()}
# truncation
inputs_slice = self._truncate(
inputs,
max_length=max_length,
pad_to_multiple_of=pad_to_multiple_of,
truncation=truncation,
)
truncated_inputs.append(inputs_slice)
if padding_strategy == PaddingStrategy.LONGEST:
# make sure that `max_length` cannot be longer than the longest truncated length
max_length = max(len(input_slice[self.model_input_names[0]]) for input_slice in truncated_inputs)
padding_strategy = PaddingStrategy.MAX_LENGTH
View on GitHub (pinned to a597f97485)
Solutions
- Make every key in the dict have exactly batch_size entries along dim 0
- Remove keys that are not per-example (pass them outside the feature dict)
- Use the feature extractor's own __call__ on raw audio so all keys are built consistently
Example fix
# before
batch = {"input_values": [a, b], "labels": [lab]} # len mismatch
# after
batch = {"input_values": [a, b], "labels": [lab0, lab1]} Defensive patterns
Strategy: validation
Validate before calling
def assert_consistent_batch(batch: dict):
sizes = {k: len(v) for k, v in batch.items()}
if len(set(sizes.values())) > 1:
raise ValueError(f"inconsistent batch sizes: {sizes}")
return batch Type guard
def is_uniform_batch(batch: dict) -> bool:
return len({len(v) for v in batch.values()}) == 1 Try / catch
try:
fe.pad(batch, padding=True)
except ValueError as e:
if "different batch size" in str(e):
n = len(batch[fe.model_input_names[0]])
batch = {k: v[:n] if len(v) > n else v for k, v in batch.items()}
fe.pad(batch, padding=True)
else:
raise Prevention
- Validate all keys share dim-0 length before padding
- Keep non-per-example metadata outside the feature dict
- Build batches exclusively through the extractor/processor
When it happens
Trigger: Mixing batched and unbatched entries: e.g. {'input_values': [a, b, c], 'attention_mask': one_array_of_len_1} or attaching a per-batch scalar/metadata array with a different length; also lists of raw audio where one key has fewer/more examples.
Common situations: Hand-assembled batches where extra keys (labels, custom metadata) don't match the batch size; off-by-one when slicing batches; mixing single-example and multi-example dicts.
Related errors
- You should supply an instance of `transformers.BatchFeature`
- Invalid padding strategy:{padding_side}
- When setting ``padding={PaddingStrategy.MAX_LENGTH}``, make
- Asking to pad but the feature_extractor does not have a padd
- type of {first_element} unknown: {type(first_element)}. Shou
AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14).
Data as JSON: /api/errors/27b2dfe67a6fcbc2.
Report an issue: GitHub.