{"record":{"id":"27b2dfe67a6fcbc2","repo":"huggingface/transformers","slug":"some-items-in-the-output-dictionary-have-a-differe","errorCode":null,"errorMessage":"Some items in the output dictionary have a different batch size than others.","messagePattern":"Some items in the output dictionary have a different batch size than others\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/feature_extraction_sequence_utils.py","lineNumber":185,"sourceCode":"                )\n\n        for key, value in processed_features.items():\n            if isinstance(value[0], (int, float)):\n                processed_features[key] = to_numpy(value)\n            elif not isinstance(value, np.ndarray):\n                # An already-batched numpy array can be used as-is; splitting it\n                # into a list of per-example arrays is pure overhead and is very\n                # slow for large inputs (e.g. long audio).\n                processed_features[key] = [to_numpy(v) for v in value]\n\n        # Convert padding_strategy in PaddingStrategy\n        padding_strategy = self._get_padding_strategies(padding=padding, max_length=max_length)\n\n        required_input = processed_features[self.model_input_names[0]]\n\n        batch_size = len(required_input)\n        if not all(len(v) == batch_size for v in processed_features.values()):\n            raise ValueError(\"Some items in the output dictionary have a different batch size than others.\")\n\n        truncated_inputs = []\n        for i in range(batch_size):\n            inputs = {k: v[i] for k, v in processed_features.items()}\n            # truncation\n            inputs_slice = self._truncate(\n                inputs,\n                max_length=max_length,\n                pad_to_multiple_of=pad_to_multiple_of,\n                truncation=truncation,\n            )\n            truncated_inputs.append(inputs_slice)\n\n        if padding_strategy == PaddingStrategy.LONGEST:\n            # make sure that `max_length` cannot be longer than the longest truncated length\n            max_length = max(len(input_slice[self.model_input_names[0]]) for input_slice in truncated_inputs)\n            padding_strategy = PaddingStrategy.MAX_LENGTH\n","sourceCodeStart":167,"sourceCodeEnd":203,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/feature_extraction_sequence_utils.py#L167-L203","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"# before\nbatch = {\"input_values\": [a, b], \"labels\": [lab]}  # len mismatch\n\n# after\nbatch = {\"input_values\": [a, b], \"labels\": [lab0, lab1]}","handlingStrategy":"validation","validationCode":"def assert_consistent_batch(batch: dict):\n    sizes = {k: len(v) for k, v in batch.items()}\n    if len(set(sizes.values())) > 1:\n        raise ValueError(f\"inconsistent batch sizes: {sizes}\")\n    return batch","typeGuard":"def is_uniform_batch(batch: dict) -> bool:\n    return len({len(v) for v in batch.values()}) == 1","tryCatchPattern":"try:\n    fe.pad(batch, padding=True)\nexcept ValueError as e:\n    if \"different batch size\" in str(e):\n        n = len(batch[fe.model_input_names[0]])\n        batch = {k: v[:n] if len(v) > n else v for k, v in batch.items()}\n        fe.pad(batch, padding=True)\n    else:\n        raise","preventionTips":["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"],"tags":["feature-extractor","batching","padding","transformers"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}