hankcs/HanLP · error · IOException

error

Error message

error

What it means

Word2VecEmbedding is a pretrained embedding module, not a trainable model, so all training-related Component methods (build_optimizer, build_criterion, etc.) are explicitly disabled with NotImplementedError('Not supported.'). Calling any training-phase hook on this embedding will always raise. The class only supports inference-time vocab/weight loading and forward embedding lookup.

Source

Thrown at plugins/hanlp_restful_java/src/main/java/com/hankcs/hanlp/restful/HanLPClient.java:614

            StringBuilder response = new StringBuilder();
            try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getErrorStream(), StandardCharsets.UTF_8)))
            {
                String responseLine;
                while ((responseLine = br.readLine()) != null)
                {
                    response.append(responseLine.trim());
                }
            }
            String error = String.format("Request failed, status code = %d, error = %s", code, con.getResponseMessage());
            try
            {
                Map detail = mapper.readValue(response.toString(), Map.class);
                error = (String) detail.get("detail");
            }
            catch (Exception ignored)
            {
            }
            throw new IOException(error);
        }

        StringBuilder response = new StringBuilder();
        try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8)))
        {
            String responseLine;
            while ((responseLine = br.readLine()) != null)
            {
                response.append(responseLine.trim());
            }
        }
        return response.toString();
    }

}

View on GitHub (pinned to ddb1299bdd)

Solutions

  1. Use a trainable component (e.g. an NER/tagger model) and pass the Word2Vec embedding as its embed module rather than training the embedding itself
  2. If you only need vectors, call the embedding's forward/inference API (or load the .txt/.tbz2 vectors) instead of fit
  3. Subclass Word2VecEmbedding and override build_optimizer etc. if you genuinely need custom training

Example fix

// before
emb = Word2VecEmbedding(' sgns ', ...)
emb.fit(train_data)  # NotImplementedError
// after
model = hanlp.load(hanlp.pretrained.pos.CTB9_POS_RADICAL_ELECTRA_SMALL)
model.predict(['Hello world'])
Defensive patterns

Strategy: type-guard

Validate before calling

from hanlp.layers.embeddings.word2vec import Word2VecEmbedding
if isinstance(model, Word2VecEmbedding):
    raise TypeError('Embedding modules are inference-only; train a task component instead')

Type guard

def is_trainable(component) -> bool:
    return not isinstance(component, Word2VecEmbedding)

Try / catch

try:
    model.fit(data)
except NotImplementedError as e:
    logging.warning('Component does not support training: %s', e)

Prevention

When it happens

Trigger: Calling .fit(), .train(), build_optimizer/build_criterion, or invoking a training loop on a Word2VecEmbedding (or a component configured with it) instead of a trainable HanLP component.

Common situations: Copy-pasting a training script written for a trainable component and swapping in a Word2Vec embedding; trying to fine-tune static pretrained vectors; using a meta-component's train path with an embedding-only config.

Related errors


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