{"record":{"id":"d0c20c36f17ff33a","repo":"stanfordnlp/CoreNLP","slug":"invalid-annotation-key","errorCode":null,"errorMessage":"Invalid annotation key ","messagePattern":"Invalid annotation key ","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ling/tokensregex/SequenceMatchRules.java","lineNumber":258,"sourceCode":"          case \"priority\":\n            priority = ((Number) Expressions.asObject(env, obj)).doubleValue();\n            break;\n          case \"stage\":\n            stage = ((Number) Expressions.asObject(env, obj)).intValue();\n            break;\n          case \"weight\":\n            weight = ((Number) Expressions.asObject(env, obj)).doubleValue();\n            break;\n          case \"over\":\n            Object annoKey = Expressions.asObject(env, obj);\n            if (annoKey instanceof Class) {\n              annotationField = (Class) annoKey;\n            } else if (annoKey instanceof String) {\n              annotationField = EnvLookup.lookupAnnotationKeyWithClassname(env, (String) annoKey);\n            } else if (annotationField == null) {\n              annotationField = CoreMap.class;\n            } else {\n              throw new IllegalArgumentException(\"Invalid annotation key \" + annoKey);\n            }\n            break;\n          case \"active\":\n            active = (Boolean) Expressions.asObject(env, obj);\n            break;\n          case \"ruleType\":\n            ruleType = (String) Expressions.asObject(env, obj);\n            break;\n          case \"matchFindType\":\n            matchFindType = SequenceMatcher.FindType.valueOf((String) Expressions.asObject(env, obj));\n            break;\n          case \"matchWithResults\":\n            matchWithResults = ((Boolean) Expressions.asObject(env, obj)).booleanValue();\n            break;\n          case \"matchedExpressionGroup\":\n            matchedExpressionGroup = ((Number) Expressions.asObject(env, obj)).intValue();\n            break;\n        }","sourceCodeStart":240,"sourceCodeEnd":276,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ling/tokensregex/SequenceMatchRules.java#L240-L276","documentation":"When parsing rule attributes in SequenceMatchRules.TemplateExtractorFunc/AnnotationCreator update(), the 'annotationField' value must be a Class or a String resolvable via Env (lookupAnnotationKeyWithClassname). If it is neither (and no annotationField was already set), an IllegalArgumentException is thrown. This validates the CoreMap annotation key supplied in a tokensregex rule definition.","triggerScenarios":"A rule file with `annotationField: <value>` where the value is not a Class and not a String key known to the Env (e.g. a Number, list, or an unresolvable non-String object); typically from programmatically built attribute maps with wrong types.","commonSituations":"Building rules programmatically and putting a wrong-typed object into the 'annotationField' attribute; custom rule languages that pass parsed non-string values.","solutions":["Pass a String annotation key name (e.g. \"words\" or a registered classname) so EnvLookup can resolve it","Pass a Class object directly, e.g. CoreAnnotations.TextAnnotation.class","Ensure env defaults/lookup tables are initialized with the annotation key classname before rule creation"],"exampleFix":"// before\nattributes.put(\"annotationField\", 42); // invalid key type\n// after\nattributes.put(\"annotationField\", CoreAnnotations.TextAnnotation.class);\n// or\nattributes.put(\"annotationField\", \"text\");","handlingStrategy":"validation","validationCode":"Object annoKey = attributes.get(\"annotationField\");\nif (annoKey != null && !(annoKey instanceof Class) && !(annoKey instanceof String)) {\n  throw new IllegalArgumentException(\"annotationField must be a Class or String key\");\n}","typeGuard":"boolean isValidAnnotationKey(Object o) {\n  return o == null || o instanceof Class || o instanceof String;\n}","tryCatchPattern":"try {\n  SequenceMatchRules.createRule(env, attributes);\n} catch (IllegalArgumentException e) {\n  if (e.getMessage().startsWith(\"Invalid annotation key\")) {\n    attributes.put(\"annotationField\", CoreAnnotations.TextAnnotation.class); // retry with Class\n  }\n}","preventionTips":["Always supply annotationField as a String key name or a Class","Verify env lookup tables contain the annotation key classnames","Avoid raw non-String values (numbers, lists) in rule attribute maps"],"tags":["java","tokensregex","configuration"],"backgroundTag":"invalid-argument-value","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"}