stanfordnlp/CoreNLP · error · RuntimeException

Cannot determine annotation key for

Error message

Cannot determine annotation key for ${propName}=${matchedExpressionsAnnotationKeyName}

What it means

TokensRegexAnnotator reads the 'tokensregex.matchedExpressionsAnnotationKey' option and resolves it via EnvLookup.lookupAnnotationKeyWithClassname against the environment. If the name cannot be resolved to a Class<? extends CoreAnnotation> key, it throws RuntimeException stating the property and its value.

Solutions

  1. Use a fully-qualified name of an existing CoreAnnotations key, e.g. edu.stanford.nlp.ling.CoreAnnotations$TextAnnotation.
  2. Register your custom annotation key in the TokensRegex env before referencing it.
  3. Verify resolution by calling EnvLookup.lookupAnnotationKeyWithClassname yourself in a test.
  4. Remove the option to use default behavior if you do not need matched expressions stored.

Example fix

// before
props.setProperty("tokensregex.matchedExpressionsAnnotationKey", "MyMatchKey");
// after
props.setProperty("tokensregex.matchedExpressionsAnnotationKey", "edu.stanford.nlp.ling.CoreAnnotations$TextAnnotation");
Defensive patterns

Strategy: validation

Validate before calling

String keyName = props.getProperty("tokensregex.matchedExpressionsAnnotationKey");
if (keyName != null && EnvLookup.lookupAnnotationKeyWithClassname(env, keyName) == null)
  throw new IllegalStateException("matchedExpressionsAnnotationKey not resolvable: " + keyName);

Type guard

boolean isResolvableAnnotationKey(Env env, String name) { return EnvLookup.lookupAnnotationKeyWithClassname(env, name) != null; }

Try / catch

try { new TokensRegexAnnotator(props); } catch (RuntimeException e) { if (e.getMessage().startsWith("Cannot determine annotation key")) { log.error("Check matchedExpressionsAnnotationKey: " + e.getMessage()); } throw e; }

Prevention

When it happens

Trigger: Setting the matchedExpressionsAnnotationKey option to a string that is not a registered annotation key class name in the TokensRegex environment (misspelled class name, custom annotation not registered in the env, or a plain word).

Common situations: Typos in class names like CoreAnnotations.TextAnnotation; using a custom annotation class without declaring it to the environment; copying an option name from docs of a different CoreNLP version.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/pipeline/TokensRegexAnnotator.java:90

      System.err.println("using case insensitive!");
      env.setDefaultStringMatchFlags(NodePattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
      env.setDefaultStringPatternFlags(Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
    }
    if (files.length != 0) {
      extractor = CoreMapExpressionExtractor.createExtractorFromFiles(env, files);
    } else {
      extractor = null;
    }
    verbose = PropertiesUtils.getBool(props, prefix + "verbose", false);
    options.setTokenOffsets = PropertiesUtils.getBool(props, prefix + "setTokenOffsets", options.setTokenOffsets);
    options.extractWithTokens = PropertiesUtils.getBool(props, prefix + "extractWithTokens", options.extractWithTokens);
    options.flatten = PropertiesUtils.getBool(props, prefix + "flatten", options.flatten);
    String matchedExpressionsAnnotationKeyName = props.getProperty(prefix + "matchedExpressionsAnnotationKey");
    if (matchedExpressionsAnnotationKeyName != null) {
      options.matchedExpressionsAnnotationKey = EnvLookup.lookupAnnotationKeyWithClassname(env, matchedExpressionsAnnotationKeyName);
      if (options.matchedExpressionsAnnotationKey == null) {
        String propName = prefix + "matchedExpressionsAnnotationKey";
        throw new RuntimeException("Cannot determine annotation key for " + propName + '=' + matchedExpressionsAnnotationKeyName);
      }
    }
  }

  public TokensRegexAnnotator(Properties props) {
    this(null, props);
  }


  private static void addTokenOffsets(CoreMap annotation) {
    // We are going to mark the token begin and token end for each token
    Integer startTokenOffset = annotation.get(CoreAnnotations.TokenBeginAnnotation.class);
    if (startTokenOffset == null) {
      startTokenOffset = 0;
    }
    //set token offsets
    int i = 0;
    for (CoreMap c:annotation.get(CoreAnnotations.TokensAnnotation.class)) {

View on GitHub (pinned to 1b7edd19c4)