stanfordnlp/CoreNLP · error · java.lang.RuntimeException

Unexpected class in list while adding word () in phrase

Error message

Unexpected class in list  while adding word () in phrase 

What it means

In the same list-node traversal, any element that is neither a Phrase nor a Map is rejected with 'Unexpected class in list ... while adding word'. The list node may only contain Phrase objects plus at most one continuation Map.

Solutions

  1. Ensure only Phrase and Map instances ever enter list nodes; migrate any foreign payloads into the Phrase object's data field
  2. Rebuild the table via the public API
  3. Add an assertion when inserting into the tree to catch bad node types early

Example fix

// before
listNode.add(42); // stray count
// after
Phrase p = new Phrase(wordList, text, tag, 42); // store data in phraseData
listNode.add(p);
Defensive patterns

Strategy: try-catch

Validate before calling

// validate only via library API; foreign objects must be stored in phraseData, not the tree
if (listNodeHasForeignObjects) migrateToPhraseData();

Type guard

static boolean listElementsOk(List<?> l) { return l.stream().allMatch(o -> o instanceof Phrase || o instanceof Map); }

Try / catch

try { table.addPhrase(wordList, text, tag, data); } catch (RuntimeException e) { if (e.getMessage().startsWith("Unexpected class in list")) { rebuildPhraseTableFromSource(); } else throw e; }

Prevention

When it happens

Trigger: A List trie node contains a foreign object type (String, Integer, custom object) inserted by external code, and addPhrase iterates past it while adding a word.

Common situations: Post-processing the internal tree (e.g. serializing/deserializing with generic types, or storing counts as raw objects inside the trie).

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/ling/tokensregex/PhraseTable.java:418

          if (obj instanceof Phrase) {
            // check rest of the phrase matches
            Phrase oldphrase = (Phrase) obj;
            int matchedTokenEnd = checkWordListMatch(
              oldphrase, wordList, 0, wordList.size(), i, true);
            if (matchedTokenEnd >= 0) {
              oldPhraseNewFormAdded = oldphrase.addForm(phraseText);
              phraseAdded = true;
              break;
            }
          } else if (obj instanceof Map) {
            if (nMaps == 1) {
              throw new RuntimeException("More than one map in list while adding word "
                      + i + "(" + word + ") in phrase " + phraseText);
            }
            tree = (Map<String, Object>) obj;
            nMaps++;
          } else  {
            throw new RuntimeException("Unexpected class in list " + obj.getClass() + " while adding word "
                    + i + "(" + word + ") in phrase " + phraseText);
          }
        }
        if (!phraseAdded && nMaps == 0) {
          // add to list
          Phrase newphrase = new Phrase(wordList, phraseText, tag, phraseData);
          lookupList.add(newphrase);
          newPhraseAdded = true;
          phraseAdded = true;
          if (lookupList.size() > MAX_LIST_SIZE) {
            // convert lookupList (should consist only of phrases) to map
            Map newMap = new HashMap<String,Object>(lookupList.size());
            for (Object obj:lookupList) {
              if (obj instanceof Phrase) {
                Phrase oldphrase = (Phrase) obj;
                addPhrase(newMap, oldphrase, i+1);
              } else  {
                throw new RuntimeException("Unexpected class in list " + obj.getClass() + " while converting list to map");

View on GitHub (pinned to 1b7edd19c4)