huggingface/transformers · error · ValueError
out_features and out_indices should have the same length if
Error message
out_features and out_indices should have the same length if both are set
What it means
Raised when both out_features and out_indices are set on a backbone config but have different lengths. The two attributes are two views of the same selection (feature names vs positional indices of stage_names); a length mismatch means they cannot possibly describe the same stages, so verification fails fast before any model code runs.
Source
Thrown at src/transformers/backbone_utils.py:120
if any(idx for idx in positive_indices if idx not in range(len(self.stage_names))):
raise ValueError(
f"out_indices must be valid indices for stage_names {self.stage_names}, got {self._out_indices}"
)
if len(positive_indices) != len(set(positive_indices)):
msg = f"out_indices must not contain any duplicates, got {self._out_indices}"
msg += f"(equivalent to {positive_indices}))" if positive_indices != self._out_indices else ""
raise ValueError(msg)
if positive_indices != tuple(sorted(positive_indices)):
sorted_negative = [
idx for _, idx in sorted(zip(positive_indices, self._out_indices), key=lambda x: x[0])
]
raise ValueError(
f"out_indices must be in the same order as stage_names, expected {sorted_negative} got {self._out_indices}"
)
if self._out_features is not None and self._out_indices is not None:
if len(self._out_features) != len(self._out_indices):
raise ValueError("out_features and out_indices should have the same length if both are set")
if self._out_features != [self.stage_names[idx] for idx in self._out_indices]:
raise ValueError("out_features and out_indices should correspond to the same stages if both are set")
@property
def out_features(self):
return self._out_features
@out_features.setter
def out_features(self, out_features: list[str]):
"""
Set the out_features attribute. This will also update the out_indices attribute to match the new out_features.
"""
self.set_output_features_output_indices(out_features=out_features, out_indices=None)
@property
def out_indices(self):
return self._out_indices
View on GitHub (pinned to a597f97485)
Solutions
- Set only one of the two (out_features recommended); the setter keeps the other in sync automatically
- If setting both, derive one from the other: out_indices = [stage_names.index(f) for f in out_features]
- Set them together via config.set_output_features_output_indices(out_features, out_indices)
Example fix
# before config.out_features = ['stage1', 'stage2', 'stage3'] config.out_indices = [1, 2] # after config.out_features = ['stage1', 'stage2', 'stage3'] config.out_indices = [1, 2, 3] # or drop this line; the setter syncs it
Defensive patterns
Strategy: validation
Validate before calling
if config.out_features is not None and config.out_indices is not None:
assert len(config.out_features) == len(config.out_indices), "length mismatch" Prevention
- Set only out_features; the config setter syncs out_indices automatically
- When updating one attribute, update (or None out) the other in the same change
- Use config.set_output_features_output_indices(f, i) to set both atomically
When it happens
Trigger: Setting config.out_features = ['stage1', 'stage2'] together with config.out_indices = [1] (or vice versa), then building the model or calling verify_out_features_out_indices(). Also fired by set_output_features_output_indices when callers pass mismatched pairs.
Common situations: Partially updating a fine-tuned config: user changes out_features but leaves stale out_indices from the base checkpoint; serialization/deserialization that drops one element; programmatic config surgery.
Related errors
- out_features and out_indices should correspond to the same s
- out_indices must be a list, got {type(self._out_indices)}
- out_indices must be valid indices for stage_names {self.stag
- out_indices must not contain any duplicates, got {self._out_
- out_indices must be in the same order as stage_names, expect
AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14).
Data as JSON: /api/errors/49c78503025de750.
Report an issue: GitHub.