{"record":{"id":"d84e6e0b5cd47fab","repo":"stanfordnlp/CoreNLP","slug":"error-creating-composite-rule-no-annotation-field","errorCode":null,"errorMessage":"Error creating composite rule: no annotation field","messagePattern":"Error creating composite rule: no annotation field","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ling/tokensregex/SequenceMatchRules.java","lineNumber":483,"sourceCode":"      annotationExtractor.expressionToValue = matched -> {\n        if (matched != null && matched.context != null && matched.context instanceof SequenceMatchResult ) {\n          return valueExtractor.apply( (SequenceMatchResult<CoreMap>) matched.context);\n        } else return null;\n      };\n      annotationExtractor.valueExtractor = new CoreMapFunctionApplier<>(env, r.annotationField, valueExtractRule);\n      r.extractRule = exprExtractRule;\n      r.filterRule = new AnnotationMatchedFilter(annotationExtractor);\n      r.pattern = pattern;\n      r.result = result;\n      pattern.weight = r.weight;\n      pattern.priority = r.priority;\n    }\n\n    protected AnnotationExtractRule create(Env env, SequencePattern.PatternExpr expr, Expression result) {\n      AnnotationExtractRule r = super.create(env, null);\n      r.isComposite = true;\n      if (r.annotationField == null) { r.annotationField = r.resultNestedAnnotationField;  }\n      if (r.annotationField == null) { throw new IllegalArgumentException(\"Error creating composite rule: no annotation field\"); }\n      r.ruleType = TOKEN_PATTERN_RULE_TYPE;\n      updateExtractRule(r, env, expr, null, result);\n      return r;\n    }\n\n    @Override\n    public AnnotationExtractRule create(Env env, Map<String,Object> attributes) {\n      AnnotationExtractRule r = super.create(env, attributes);\n      r.isComposite = true;\n      if (r.annotationField == null) { r.annotationField = r.resultNestedAnnotationField;  }\n      if (r.annotationField == null) { throw new IllegalArgumentException(\"Error creating composite rule: no annotation field\"); }\n      if (r.ruleType == null) { r.ruleType = TOKEN_PATTERN_RULE_TYPE; }\n      //SequencePattern.PatternExpr expr = (SequencePattern.PatternExpr) attributes.get(\"pattern\");\n      TokenSequencePattern expr = (TokenSequencePattern) Expressions.asObject(env, attributes.get(\"pattern\"));\n      Expression action = Expressions.asExpression(env, attributes.get(\"action\"));\n      Expression result = Expressions.asExpression(env, attributes.get(\"result\"));\n      updateExtractRule(r, env, expr, action, result);\n      return r;","sourceCodeStart":465,"sourceCodeEnd":501,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ling/tokensregex/SequenceMatchRules.java#L465-L501","documentation":"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.","triggerScenarios":"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)).","commonSituations":"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.","solutions":["Set attributes.put(\"annotationField\", CoreMapAttributeAggregator/Class) or a String key when creating the rule","Set env.setDefaults(Collections.singletonMap(\"annotationField\", CoreAnnotations.SentencesAnnotation.class))","Ensure the rule result Expression declares a nested annotation field"],"exampleFix":"// before\nMap<String,Object> attr = new HashMap<>();\nattr.put(\"pattern\", pattern); // no annotationField\n// after\nMap<String,Object> attr = new HashMap<>();\nattr.put(\"annotationField\", CoreAnnotations.SentencesAnnotation.class);\nattr.put(\"pattern\", pattern);","handlingStrategy":"validation","validationCode":"if (attributes.get(\"annotationField\") == null\n    && env.getDefaults().get(\"annotationField\") == null) {\n  throw new IllegalArgumentException(\"composite rule requires annotationField\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  rule = creator.create(env, attributes);\n} catch (IllegalArgumentException e) {\n  if (e.getMessage().contains(\"no annotation field\")) {\n    env.setDefaults(Collections.singletonMap(\"annotationField\", CoreAnnotations.SentencesAnnotation.class));\n    // retry creation\n  }\n}","preventionTips":["Set annotationField in env defaults before creating composite rules","Include annotationField in every composite rule definition","Ensure result Expressions declare a nested annotation field"],"tags":["java","tokensregex","configuration"],"backgroundTag":"missing-required-config-field","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}