languagetool-org/languagetool · error · RuntimeException

<antipattern>s can only contain <example>s without errors (i

Error message

<antipattern>s can only contain <example>s without errors (i.e. no 'correction=...'). 

What it means

Anti-patterns describe texts that must NOT match, so they may only contain correct <example> elements. An <example> with a correction attribute (i.e. an incorrect example with a fix) inside an <antipattern> is invalid and throws this RuntimeException.

Source

Thrown at languagetool-core/src/main/java/org/languagetool/rules/patterns/PatternRuleHandler.java:616

          rulegroupAntiPatterns.add(rule);
        }
        tokenCounter = 0;
        inAntiPattern = false;
        endPos = -1;
        startPos = -1;
        inAntiPatternExample = false;
        xmlLineNumberAntiPattern = -1;
        break;
      case EXAMPLE:
        if (inAntiPatternExample) {
          antipatternExamples.add(new CorrectExample(antiPatternExample.toString()));
        } else if (inAntiPatternForRuleGroupExample) {
          antipatternForRuleGroupsExamples.add(new CorrectExample(antiPatternForRuleGroupExample.toString()));
        } else if (inCorrectExample) {
          correctExamples.add(new CorrectExample(correctExample.toString()));
        } else if (inIncorrectExample) {
          if (inAntiPattern) {
            throw new RuntimeException("<antipattern>s can only contain <example>s without errors (i.e. no 'correction=...'). " +
              "Rule id: " + id + "[" + subId + "], antipattern no " + antiPatternCounter);
          }
          IncorrectExample example;
          if (exampleCorrection == null) {
            example = new IncorrectExample(incorrectExample.toString());
          } else {
            List<String> corrections = new ArrayList<>(Arrays.asList(exampleCorrection.toString().split("\\|")));
            if (exampleCorrection.toString().endsWith("|")) {  // suggestions plus an empty suggestion (split() will ignore trailing empty items)
              corrections.add("");
            }
            example = new IncorrectExample(incorrectExample.toString(), corrections);
          }
          incorrectExamples.add(example);
        } else if (inErrorTriggerExample) {
          errorTriggeringExamples.add(new ErrorTriggeringExample(errorTriggerExample.toString()));
        }
        inCorrectExample = false;
        inIncorrectExample = false;

View on GitHub (pinned to 2e990059ce)

Solutions

  1. Remove the correction attribute from the <example> inside the <antipattern> so it is a plain error-free example
  2. Or delete the example from the antipattern entirely
  3. Or move the example to the rule's own <example> section if it is actually an incorrect example

Example fix

// before
<antipattern>
  <example correction="fixed">bad text</example>
</antipattern>
// after
<antipattern>
  <example>good text</example>
</antipattern>
Defensive patterns

Strategy: validation

Validate before calling

// Ensure no example inside antipattern has a correction attribute
NodeList anti = doc.getElementsByTagName("antipattern");
for (int i = 0; i < anti.getLength(); i++) {
  NodeList kids = anti.item(i).getChildNodes();
  for (int j = 0; j < kids.getLength(); j++) {
    Node n = kids.item(j);
    if ("example".equals(n.getNodeName()) && n.getAttributes().getNamedItem("correction") != null)
      throw new IllegalStateException("correction example inside antipattern");
  }
}

Try / catch

try { loader.parse(ruleXml, lang); } catch (RuntimeException e) { if (e.getMessage().contains("<antipattern>s can only contain")) { log.error("Remove correction from antipattern example"); } throw e; }

Prevention

When it happens

Trigger: endElement closes an <example> with correction attribute while inAntiPattern is true; thrown with rule id, subId and the antipattern counter identifying the offending element.

Common situations: Copy-pasting an incorrect example with correction="..." from a normal rule into an antipattern block; misunderstanding that antipatterns only accept error-free examples.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of languagetool-org/languagetool@2e990059ce (2026-09-06). Data as JSON: /api/errors/5eea199ad4c4ee15. Report an issue: GitHub.