hankcs/HanLP · error · ValueError
Unsupported argument type: {item}
Error message
Unsupported argument type: {item} What it means
Feedforward requires the hidden_dims list length to exactly equal num_layers, since each layer consumes one hidden dimension. If you pass, e.g., num_layers=2 with hidden_dims=[100], the config is inconsistent and the constructor raises ValueError.
Source
Thrown at hanlp/common/transform.py:87
def __init__(self, *fields) -> None:
super().__init__()
for each in fields:
self.append(FieldToIndex(each))
def append(self, item: Union[str, Tuple[str, Vocab], Tuple[str, str, Vocab], FieldToIndex]) -> None:
if isinstance(item, str):
item = FieldToIndex(item)
elif isinstance(item, (list, tuple)):
if len(item) == 2:
item = FieldToIndex(src=item[0], vocab=item[1])
elif len(item) == 3:
item = FieldToIndex(src=item[0], dst=item[1], vocab=item[2])
else:
raise ValueError(f'Unsupported argument length: {item}')
elif isinstance(item, FieldToIndex):
pass
else:
raise ValueError(f'Unsupported argument type: {item}')
super(self).append(item)
def save_vocab(self, save_dir):
for each in self:
each.save_vocab(save_dir, None)
def load_vocab(self, save_dir):
for each in self:
each.load_vocab(save_dir, None)
class VocabDict(SerializableDict):
def __init__(self, *args, **kwargs) -> None:
"""A dict holding :class:`hanlp.common.vocab.Vocab` instances. When used as a transform, it transforms the field
corresponding to each :class:`hanlp.common.vocab.Vocab` into indices.
Args:View on GitHub (pinned to ddb1299bdd)
Solutions
- Set num_layers = len(hidden_dims) (or extend hidden_dims to match num_layers)
- Pass hidden_dims as an explicit list with one entry per layer, e.g. [128, 64] for 2 layers
- Validate config programmatically before building the model
Example fix
# before Feedforward(input_dim=300, num_layers=2, hidden_dims=[128]) # after Feedforward(input_dim=300, num_layers=2, hidden_dims=[128, 128])
Defensive patterns
Strategy: validation
Validate before calling
assert len(hidden_dims) == num_layers, f'hidden_dims {len(hidden_dims)} != num_layers {num_layers}' Try / catch
try:
ff = Feedforward(input_dim, num_layers, hidden_dims)
except ValueError as e:
raise ValueError(f'Invalid Feedforward config: {e}') from e Prevention
- Derive num_layers from len(hidden_dims) in configs
- Always pass hidden_dims as an explicit list
- Lint config dicts before model construction
When it happens
Trigger: Constructing Feedforward(input_dim, num_layers, hidden_dims, ...) where len(hidden_dims) != num_layers, or passing a single int where a per-layer list is expected.
Common situations: Typos in component configs (e.g. transformer/tagger head configs); refactoring a 1-layer FFN to N layers and forgetting to extend hidden_dims; passing hidden_dims as a scalar instead of a list.
Understand the failure class
Background: Config validation failed: what "invalid value for {key}" and settings-rejection errors mean across 19 open-source libraries — this error's family across 19 libraries.
Related errors
- Unrecognized mapper type {mapper}
- self.model.config.pad_token_id has to be defined.
- embed_dim must be divisible by num_heads (got `embed_dim`: {
- Attention weights should be of size {(bsz * self.num_heads,
- You cannot specify both input_ids and inputs_embeds at the s
AI-assisted analysis of hankcs/HanLP@ddb1299bdd (2026-08-27).
Data as JSON: /api/errors/5e1a47c6a430a7ce.
Report an issue: GitHub.