hankcs/HanLP · error · ValueError

Unsupported parameter type: {embed}

Error message

Unsupported parameter type: {embed}

What it means

build_word2vec_with_vocab accepts embed either as a str/tensor-like loadable by nn.Embedding.from_pretrained or as an int dim for a fresh nn.Embedding. Any other type (float, None, dict, module) raises this error. The str path loads a pretrained matrix with padding_idx=vocab.pad_idx and freeze according to trainable.

Source

Thrown at hanlp/layers/embeddings/util.py:110

        unk: UNK token.
        lowercase: Convert words in pretrained embeddings into lowercase.
        trainable: ``False`` to use static embeddings.
        init: Indicate which initialization to use for oov tokens.
        normalize: ``True`` or a method to normalize the embedding matrix.

    Returns:
        An embedding matrix.

    """
    if isinstance(embed, str):
        embed = index_word2vec_with_vocab(embed, vocab, extend_vocab, unk, lowercase, init, normalize)
        embed = nn.Embedding.from_pretrained(embed, freeze=not trainable, padding_idx=vocab.pad_idx)
        return embed
    elif isinstance(embed, int):
        embed = nn.Embedding(len(vocab), embed, padding_idx=vocab.pad_idx)
        return embed
    else:
        raise ValueError(f'Unsupported parameter type: {embed}')

View on GitHub (pinned to ddb1299bdd)

Solutions

  1. Pass an int (e.g. 300) or a path to pretrained vectors
  2. Coerce numeric config values to int
  3. Check that the config key for embedding dim is present and typed correctly

Example fix

# before
embed = build_word2vec_with_vocab(300.0, vocab)  # error
# after
embed = build_word2vec_with_vocab(int(300.0), vocab)
Defensive patterns

Strategy: type-guard

Validate before calling

if isinstance(embed, float): embed = int(embed)
assert isinstance(embed, (int, str)), f'embed must be int or path, got {type(embed)}'

Type guard

def valid_embed_param(embed) -> bool:
    return isinstance(embed, (int, str)) and not isinstance(embed, bool)

Prevention

When it happens

Trigger: Passing embed=None (missing config), a float like 300.0, or an nn.Module to build_word2vec_with_vocab / a word2vec embedding config.

Common situations: Missing key in a YAML/JSON config; numeric dim parsed as float; trying to inject a custom module where only int or pretrained path are supported.

Related errors


AI-assisted analysis of hankcs/HanLP@ddb1299bdd (2026-08-27). Data as JSON: /api/errors/5c56b6d0ca66a468. Report an issue: GitHub.