huggingface/transformers · error · ValueError
out_features must not contain any duplicates, got {self._out
Error message
out_features must not contain any duplicates, got {self._out_features} What it means
Raised by BackboneConfigMixin.verify_out_features_out_indices when len(self._out_features) != len(set(self._out_features)), i.e. the same stage name appears more than once. Duplicate entries would create duplicate output hooks/channels in the feature-map output, which is meaningless for a backbone, so it is rejected. (Duplicates in out_indices are similarly rejected by the mirrored check.)
Source
Thrown at src/transformers/backbone_utils.py:89
self._out_features, self._out_indices = out_features, out_indices
self.verify_out_features_out_indices()
def verify_out_features_out_indices(self):
"""
Verify that out_indices and out_features are valid for the given stage_names.
"""
if self.stage_names is None:
raise ValueError("Stage_names must be set for transformers backbones")
if self._out_features is not None:
if not isinstance(self._out_features, (list,)):
raise ValueError(f"out_features must be a list got {type(self._out_features)}")
if any(feat not in self.stage_names for feat in self._out_features):
raise ValueError(
f"out_features must be a subset of stage_names: {self.stage_names} got {self._out_features}"
)
if len(self._out_features) != len(set(self._out_features)):
raise ValueError(f"out_features must not contain any duplicates, got {self._out_features}")
if self._out_features != (
sorted_feats := [feat for feat in self.stage_names if feat in self._out_features]
):
raise ValueError(
f"out_features must be in the same order as stage_names, expected {sorted_feats} got {self._out_features}"
)
if self._out_indices is not None:
if not isinstance(self._out_indices, list):
raise ValueError(f"out_indices must be a list, got {type(self._out_indices)}")
# Convert negative indices to their positive equivalent: [-1,] -> [len(stage_names) - 1,]
positive_indices = tuple(idx % len(self.stage_names) if idx < 0 else idx for idx in self._out_indices)
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}"View on GitHub (pinned to a597f97485)
Solutions
- Deduplicate while preserving order: out_features = list(dict.fromkeys(out_features))
- Fix the merge logic that produced duplicates instead of concatenating blindly
- Inspect the error message: it prints the exact list including the duplicated names
Example fix
// before out_features = defaults + extras # ['stage1', 'stage2', 'stage2'] cfg = BackboneConfig(out_features=out_features) // after out_features = list(dict.fromkeys(defaults + extras)) # ['stage1', 'stage2'] cfg = BackboneConfig(out_features=out_features)
Defensive patterns
Strategy: validation
Validate before calling
out_features = list(dict.fromkeys(out_features)) # dedupe, keep order cfg = SomeBackboneConfig(out_features=out_features)
Type guard
def has_no_duplicates(out_features) -> bool:
return out_features is None or len(out_features) == len(set(out_features)) Try / catch
try:
cfg = SomeBackboneConfig(out_features=out_features)
except ValueError as e:
if "must not contain any duplicates" in str(e):
cfg = SomeBackboneConfig(out_features=list(dict.fromkeys(out_features)))
else:
raise Prevention
- Deduplicate merged feature lists with dict.fromkeys (order-preserving)
- Don't concatenate default + user feature lists blindly
- Read the error message — it shows the exact duplicated entries
When it happens
Trigger: Passing out_features=["stage2", "stage2"] or out_indices=[1, 1, 2]; config-building code that concatenates user-requested features without deduplicating (e.g. default_features + extra_features).
Common situations: Programmatic config assembly merging feature lists; YAML anchors or JSON defaults that repeat a stage; copy-paste edits duplicating a line.
Related errors
- Stage_names must be set for transformers backbones
- out_features must be a list got {type(self._out_features)}
- out_features must be a subset of stage_names: {self.stage_na
- out_features must be in the same order as stage_names, expec
- out_indices must not contain any duplicates, got {self._out_
AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14).
Data as JSON: /api/errors/17d3770d8c134530.
Report an issue: GitHub.