stanfordnlp/CoreNLP · error · java.lang.RuntimeException

Unexpected class while looking up

Error message

Unexpected class  while looking up 

What it means

Companion to the list-element check: in the exhaustive lookup, the trie node retrieved for a word must itself be Map or List (with Phrase elements handled inline). Any other node class throws this RuntimeException, indicating internal trie corruption or an unexpected node type.

Solutions

  1. Rebuild the PhraseTable cleanly using only public add APIs, single-threaded.
  2. Treat the table as immutable after construction; share it across threads only after building completes.
  3. Remove reflection/serialization code that touches internal node objects.
  4. Report a reproducer to Stanford NLP if it occurs without external mutation.

Example fix

// before
root.put(word, "somestring"); // invalid node
// after
table.addPhrase(phraseText); // PhraseTable builds Map/List/Phrase nodes itself
Defensive patterns

Strategy: try-catch

Validate before calling

// immutable-after-build discipline
buildTableOnce(phrases);

Type guard

boolean isLookupNode(Object o) { return o instanceof Map || o instanceof List; }

Try / catch

try { matches = table.lookupByRegex(...); } catch (RuntimeException e) { if (e.getMessage().contains("Unexpected class")) { table = rebuildTable(phrases); } else { throw e; } }

Prevention

When it happens

Trigger: Multi-match lookup where tree.get(word) yields a non-Map, non-List object - typically after external mutation of internal maps or concurrent addPhrase races.

Common situations: Reflection writes into the root tree; multi-threaded population; mixing PhraseTable instances or serialized structures across versions.

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/3019884433137bca. Report an issue: GitHub.

Appendix: source

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

            if (obj instanceof Phrase) {
              // check rest of the phrase matches
              Phrase phrase = (Phrase) obj;
              if (acceptablePhrases == null || acceptablePhrases.contains(phrase)) {
                int matchedTokenEnd = checkWordListMatch(
                  phrase, tokens, cur.tokenStart, cur.tokenEnd, i+1, matchEnd);
                if (matchedTokenEnd >= 0) {
                  matched.add(new PhraseMatch(phrase, cur.tokenStart, matchedTokenEnd));
                }
              }
            } else if (obj instanceof Map) {
              todoStack.push(new StackEntry((Map<String,Object>) obj, cur.tokenStart, i+1, cur.tokenEnd, -1));
            } else  {
              throw new RuntimeException("Unexpected class in list " + obj.getClass() + " while looking up " + word);
            }
          }
          break;
        } else {
          throw new RuntimeException("Unexpected class " + node.getClass() + " while looking up " + word);
        }
      }
      if (cur.continueAt >= 0) {
        int newStart = (cur.continueAt > cur.tokenStart)? cur.continueAt: cur.tokenStart+1;
        if (newStart < cur.tokenEnd) {
          todoStack.push(new StackEntry(cur.tree, newStart, newStart, cur.tokenEnd, newStart+1));
        }
      }
    }
    return matched;
  }

  public Iterator<Phrase> iterator() {
    return new PhraseTableIterator(this);
  }

  private static class PhraseTableIterator extends AbstractIterator<Phrase> {
    private PhraseTable phraseTable;

View on GitHub (pinned to 1b7edd19c4)