stanfordnlp/CoreNLP · error · RuntimeException
! wordVectors used to initialize Embedding doesn't contain…
Error message
! wordVectors used to initialize Embedding doesn't contain any recognized form of
What it means
Embedding.getEmbeddingSize requires the supplied word-vector map to contain an entry for the unknown-word token. It looks for known UNK spellings (e.g. '<unk>') and, if none is present, throws this RuntimeException because the embedding's unknown-word vector cannot be resolved and its size (numElements) cannot be determined.
Solutions
- Add an entry keyed '<unk>' (or the library's UNKNOWN_WORD) with a vector before constructing the Embedding
- Use the string form constructor with a vectors file that includes an UNK line
- Pre-process the map: wordVectors.put("<unk>", someVector)
Example fix
// before
Embedding e = new Embedding(vocabVectors); // no unk key
// after
vocabVectors.put("<unk>", new double[]{0.0, 0.0, 0.0});
Embedding e = new Embedding(vocabVectors); Defensive patterns
Strategy: validation
Validate before calling
if (!wordVectors.containsKey("<unk>") && !wordVectors.containsKey("UNK") && !wordVectors.containsKey("UUUNKKK"))
wordVectors.put("<unk>", new double[expectedDim]); Type guard
boolean hasUnk = wordVectors.keySet().stream().anyMatch(k -> k.toLowerCase().contains("unk")); Try / catch
try { Embedding e = new Embedding(wordVectors); } catch (RuntimeException e) { if (e.getMessage().contains("doesn't contain any recognized form")) { wordVectors.put("<unk>", defaultVector()); e = new Embedding(wordVectors); } else throw e; } Prevention
- Always add an UNK entry when building a vocabulary map from scratch
- Keep the UNK token spelling consistent with the library's UNKNOWN_WORD
- Unit-test embedding construction with your exact vocab preprocessing
When it happens
Trigger: Calling new Embedding(Map<String,double[]>) or setWordVectors() with a map that has neither the library's default UNKNOWN_WORD key nor '<unk>' nor any recognized UNK form.
Common situations: Building embeddings from a custom word2vec/GloVe map that lacks an UNK entry; trimming vocab and dropping UNK; renaming UNK to something the library doesn't recognize.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- Word vectors file has dimension too small for requested…
- Not sure if RVFDataset runs correctly in this method…
- minValue for feature
- maxValue for feature
- datum
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/80a99f2cb88ce1b4.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/neural/Embedding.java:308
this.wordVectors = wordVectors;
this.embeddingSize = getEmbeddingSize(wordVectors);
}
private static int getEmbeddingSize(Map<String, SimpleMatrix> wordVectors){
if (!wordVectors.containsKey(UNKNOWN_WORD)){
// find if there's any other unk string
String unkStr = "";
if (wordVectors.containsKey("UNK")) { unkStr = "UNK"; }
if (wordVectors.containsKey("UUUNKKK")) { unkStr = "UUUNKKK"; }
if (wordVectors.containsKey("UNKNOWN")) { unkStr = "UNKNOWN"; }
if (wordVectors.containsKey("*UNKNOWN*")) { unkStr = "*UNKNOWN*"; }
if (wordVectors.containsKey("<unk>")) { unkStr = "<unk>"; }
// set UNKNOWN_WORD
if ( ! unkStr.isEmpty()){
wordVectors.put(UNKNOWN_WORD, wordVectors.get(unkStr));
} else {
throw new RuntimeException("! wordVectors used to initialize Embedding doesn't contain any recognized form of " + UNKNOWN_WORD);
}
}
return wordVectors.get(UNKNOWN_WORD).getNumElements();
}
}
View on GitHub (pinned to 1b7edd19c4)