stanfordnlp/CoreNLP · error · IllegalArgumentException

Error creating composite rule: no annotation field

Error message

Error creating composite rule: no annotation field

What it means

When creating a composite rule from a compiled SequencePattern, the rule needs to know which CoreMap annotation field (e.g. sentences or tokens) the pattern applies to. If neither the rule's annotationField nor the result's nested annotation field is set, IllegalArgumentException 'Error creating composite rule: no annotation field' is thrown because the matcher has no target annotation.

Solutions

  1. Set attributes.put("annotationField", CoreMapAttributeAggregator/Class) or a String key when creating the rule
  2. Set env.setDefaults(Collections.singletonMap("annotationField", CoreAnnotations.SentencesAnnotation.class))
  3. Ensure the rule result Expression declares a nested annotation field

Example fix

// before
Map<String,Object> attr = new HashMap<>();
attr.put("pattern", pattern); // no annotationField
// after
Map<String,Object> attr = new HashMap<>();
attr.put("annotationField", CoreAnnotations.SentencesAnnotation.class);
attr.put("pattern", pattern);
Defensive patterns

Strategy: validation

Validate before calling

if (attributes.get("annotationField") == null
    && env.getDefaults().get("annotationField") == null) {
  throw new IllegalArgumentException("composite rule requires annotationField");
}

Try / catch

try {
  rule = creator.create(env, attributes);
} catch (IllegalArgumentException e) {
  if (e.getMessage().contains("no annotation field")) {
    env.setDefaults(Collections.singletonMap("annotationField", CoreAnnotations.SentencesAnnotation.class));
    // retry creation
  }
}

Prevention

When it happens

Trigger: Programmatic creation of a composite (tokens) rule where the Env/rule attributes define no 'annotationField' and the result Expression specifies no nested annotation field (e.g. creating with super.create(env, null)).

Common situations: Building composite rules in code without setting env default 'annotationField'; rule files that use composite rules without an annotationField entry while the result doesn't imply one.

Understand the failure class

Background: "is required", "must be set", "missing required field": configuration validation errors across open-source libraries — this error's family across 36 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/ling/tokensregex/SequenceMatchRules.java:483

      annotationExtractor.expressionToValue = matched -> {
        if (matched != null && matched.context != null && matched.context instanceof SequenceMatchResult ) {
          return valueExtractor.apply( (SequenceMatchResult<CoreMap>) matched.context);
        } else return null;
      };
      annotationExtractor.valueExtractor = new CoreMapFunctionApplier<>(env, r.annotationField, valueExtractRule);
      r.extractRule = exprExtractRule;
      r.filterRule = new AnnotationMatchedFilter(annotationExtractor);
      r.pattern = pattern;
      r.result = result;
      pattern.weight = r.weight;
      pattern.priority = r.priority;
    }

    protected AnnotationExtractRule create(Env env, SequencePattern.PatternExpr expr, Expression result) {
      AnnotationExtractRule r = super.create(env, null);
      r.isComposite = true;
      if (r.annotationField == null) { r.annotationField = r.resultNestedAnnotationField;  }
      if (r.annotationField == null) { throw new IllegalArgumentException("Error creating composite rule: no annotation field"); }
      r.ruleType = TOKEN_PATTERN_RULE_TYPE;
      updateExtractRule(r, env, expr, null, result);
      return r;
    }

    @Override
    public AnnotationExtractRule create(Env env, Map<String,Object> attributes) {
      AnnotationExtractRule r = super.create(env, attributes);
      r.isComposite = true;
      if (r.annotationField == null) { r.annotationField = r.resultNestedAnnotationField;  }
      if (r.annotationField == null) { throw new IllegalArgumentException("Error creating composite rule: no annotation field"); }
      if (r.ruleType == null) { r.ruleType = TOKEN_PATTERN_RULE_TYPE; }
      //SequencePattern.PatternExpr expr = (SequencePattern.PatternExpr) attributes.get("pattern");
      TokenSequencePattern expr = (TokenSequencePattern) Expressions.asObject(env, attributes.get("pattern"));
      Expression action = Expressions.asExpression(env, attributes.get("action"));
      Expression result = Expressions.asExpression(env, attributes.get("result"));
      updateExtractRule(r, env, expr, action, result);
      return r;

View on GitHub (pinned to 1b7edd19c4)