languagetool-org/languagetool · error · BadRequestException

Invalid word, you can only add words that don't contain spac

Error message

Invalid word, you can only add words that don't contain spaces: ''

What it means

validateWord rejects words containing whitespace matched by WHITESPACE_PATTERN, throwing BadRequestException. User vocabulary entries must be single tokens without spaces; the exception message embeds the offending word.

Source

Thrown at languagetool-server/src/main/java/org/languagetool/server/DatabaseAccessOpenSource.java:326

    }
  }

  @Override
  List<DictGroupEntry> getDictGroups(Long userId) {
    return Collections.emptyList();
  }

  @Override
  Long getOrCreateDictGroup(Long userId, String groupName) {
    throw new NotImplementedException(NON_PREMIUM_MSG);
  }

  private void validateWord(String word) {
    if (word == null || word.trim().isEmpty()) {
      throw new BadRequestException("Invalid word, cannot be empty or whitespace only");
    }
    if (WHITESPACE_PATTERN.matcher(word).matches()) {
      throw new BadRequestException("Invalid word, you can only add words that don't contain spaces: '" + word + "'");
    }
  }

  /** For unit tests only! */
  @Override
  public void createAndFillTestTables(boolean mysql, List<String> skipStatements) {
    try (SqlSession session = sqlSessionFactory.openSession(true)) {
      System.out.println("Setting up tables and adding test user...");
      String[] statements = { "org.languagetool.server.UserDictMapper.createUserTable",
        "org.languagetool.server.UserDictMapper.createIgnoreWordTable" };
      for (String statement : statements) {
        if (skipStatements.contains(statement)) {
          continue;
        }
        if (mysql) {
          session.insert(statement + "MySQL");
        } else {
          session.insert(statement);

View on GitHub (pinned to 2e990059ce)

Solutions

  1. Split multi-word input on whitespace and add each token individually.
  2. Validate client-side that the word matches ^\S+$ before calling addWord.
  3. Show a UI validation message preventing spaces in the input field.
  4. If phrases are needed, use a premium dictionary group or your own storage instead of the user vocabulary API.

Example fix

// before
server.addWord(userId, "hello world");
// after
for (String token : phrase.trim().split("\\s+")) {
  server.addWord(userId, token);
}
Defensive patterns

Strategy: validation

Validate before calling

function isSingleToken(w) {
  return /^\S+$/.test(w);
}
if (!isSingleToken(word)) throw new Error('word must not contain spaces');

Type guard

boolean isSingleToken(String w) {
  return w != null && w.matches("\\S+");
}

Try / catch

try {
  api.addWord(userId, word);
} catch (BadRequestException e) {
  if (e.getMessage().contains("spaces")) {
    word.trim().split("\\s+").forEach(w -> api.addWord(userId, w));
  } else throw e;
}

Prevention

When it happens

Trigger: Calling addWord with a string containing a space, e.g. "hello world" or multi-word phrases.

Common situations: User pastes a phrase into an 'add to dictionary' UI field; splitting of comma-separated input done with the wrong delimiter; adding n-grams or phrases instead of single words.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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