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
- Split multi-word input on whitespace and add each token individually.
- Validate client-side that the word matches ^\S+$ before calling addWord.
- Show a UI validation message preventing spaces in the input field.
- 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
- Split phrases into tokens before adding to the dictionary
- Constrain dictionary UI inputs to single tokens
- Normalize input (trim + collapse whitespace) before validation
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
- Invalid word, cannot be empty or whitespace only
- No rules are active. Please make sure your rule ids (<option
- Probability must be >= 0:
- <antipattern>s can only contain <example>s without errors (i
- 'skip' should be between -1 and ${Byte.MAX_VALUE}
AI-assisted analysis of languagetool-org/languagetool@2e990059ce (2026-09-06).
Data as JSON: /api/errors/b911c47bf7497b73.
Report an issue: GitHub.