stanfordnlp/CoreNLP · error · IllegalArgumentException

Expected tagged words

Error message

Expected tagged words

What it means

ShiftReduceParser.initialStateFromTaggedSentence builds the initial parser state from input words. Each word must carry a POS tag; when a word is not a HasTag instance (and isn't already a TaggedWord/CoreLabel with a tag), the parser cannot obtain a tag and throws IllegalArgumentException("Expected tagged words").

Solutions

  1. Run a POS tagger over the sentence so each token has a tag, and pass TaggedWord/CoreLabel objects
  2. Construct the sentence as List<TaggedWord> with setTag on each word before calling the parser
  3. Use the parser's documented pipeline (tokenizer + tagger) instead of hand-built Word lists

Example fix

// before
List<Word> words = Arrays.asList(new Word("The"), new Word("dog"));
State s = parser.initialStateFromTaggedSentence(words);

// after
List<TaggedWord> words = Arrays.asList(new TaggedWord("The", "DT"), new TaggedWord("dog", "NN"));
State s = parser.initialStateFromTaggedSentence(words);
Defensive patterns

Strategy: validation

Validate before calling

for (HasWord hw : sentence) {
  if (!(hw instanceof HasTag) || ((HasTag) hw).tag() == null) {
    throw new IllegalArgumentException("Word '" + hw.word() + "' is not tagged");
  }
}

Type guard

boolean isTagged(HasWord hw) {
  return hw instanceof HasTag && ((HasTag) hw).tag() != null;
}

Try / catch

try {
  state = parser.initialStateFromTaggedSentence(sentence);
} catch (IllegalArgumentException e) {
  state = parser.initialStateFromTaggedSentence(tagger.tagSentence(rawTokens));
}

Prevention

When it happens

Trigger: Calling initialStateFromTaggedSentence (directly or via initialStateFromGoldTagTree / parse(List<? extends HasWord>)) with a sentence of plain Word/String tokens that do not implement HasTag.

Common situations: Tokenizing with a plain whitespace or Word-based tokenizer instead of a PTBTokenizer producing CoreLabels, then feeding tokens straight to the parser; forgetting to run a POS tagger on the token list.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/c576c8199fec3af7. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/parser/shiftreduce/ShiftReduceParser.java:245

    return initialStateFromTaggedSentence(tree.taggedYield());
  }

  public static State initialStateFromTaggedSentence(List<? extends HasWord> words) {
    List<Tree> preterminals = Generics.newArrayList();
    for (int index = 0; index < words.size(); ++index) {
      HasWord hw = words.get(index);

      CoreLabel wordLabel;
      String tag;
      if (hw instanceof CoreLabel) {
        wordLabel = (CoreLabel) hw;
        tag = wordLabel.tag();
      } else {
        wordLabel = new CoreLabel();
        wordLabel.setValue(hw.word());
        wordLabel.setWord(hw.word());
        if (!(hw instanceof HasTag)) {
          throw new IllegalArgumentException("Expected tagged words");
        }
        tag = ((HasTag) hw).tag();
        wordLabel.setTag(tag);
      }
      if (tag == null) {
        throw new IllegalArgumentException("Input word not tagged");
      }
      CoreLabel tagLabel = new CoreLabel();
      tagLabel.setValue(tag);

      // Index from 1.  Tools downstream from the parser expect that
      // Internally this parser uses the index, so we have to
      // overwrite incorrect indices if the label is already indexed
      wordLabel.setIndex(index + 1);
      tagLabel.setIndex(index + 1);

      LabeledScoredTreeNode wordNode = new LabeledScoredTreeNode(wordLabel);
      LabeledScoredTreeNode tagNode = new LabeledScoredTreeNode(tagLabel);

View on GitHub (pinned to 1b7edd19c4)