languagetool-org/languagetool · error · BadRequestException
You specified 'preferredVariants' but you didn't specify 'la
Error message
You specified 'preferredVariants' but you didn't specify 'language=auto'
What it means
In the v2 API, 'preferredVariants' is only meaningful together with automatic language detection (language=auto) or multilingual mode. Specifying preferredVariants with a concrete language is contradictory, so V2TextChecker.getPreferredVariants() throws this BadRequestException.
Source
Thrown at languagetool-server/src/main/java/org/languagetool/server/V2TextChecker.java:138
DetectedLanguage detectedLang = detectLanguageOfString(text, null, preferredVariants, noopLangs, preferredLangs, forcePreferredLanguages);
Language givenLang;
if (getLanguageAutoDetect(parameters)) {
givenLang = detectedLang.getDetectedLanguage();
} else {
givenLang = parseLanguage(langParam);
}
return new DetectedLanguage(givenLang, detectedLang.getDetectedLanguage(), detectedLang.getDetectionConfidence(),
detectedLang.getDetectionSource());
}
@Override
@NotNull
protected List<String> getPreferredVariants(Map<String, String> parameters) {
List<String> preferredVariants;
if (parameters.get("preferredVariants") != null) {
preferredVariants = Arrays.asList(COMMA_WHITESPACE_PATTERN.split(parameters.get("preferredVariants")));
if (!"auto".equals(parameters.get("language")) && (parameters.get("multilingual") == null || parameters.get("multilingual").equals("false"))) {
throw new BadRequestException("You specified 'preferredVariants' but you didn't specify 'language=auto'");
}
} else {
preferredVariants = Collections.emptyList();
}
return preferredVariants;
}
}
View on GitHub (pinned to 2e990059ce)
Solutions
- Set language=auto when using preferredVariants
- Or add multilingual=true if you genuinely want multilingual checking
- Or remove preferredVariants when a concrete language is set
- Centralize request building so preferredVariants is only added for auto/multilingual mode
Example fix
// before
params.put("language", "en-US");
params.put("preferredVariants", "en-GB");
// after
params.put("language", "auto");
params.put("preferredVariants", "en-GB"); Defensive patterns
Strategy: validation
Validate before calling
if (params.preferredVariants && params.language !== 'auto' && params.multilingual !== 'true') {
throw new Error('preferredVariants requires language=auto or multilingual=true');
} Type guard
const canUsePreferredVariants = (p) => p.language === 'auto' || p.multilingual === 'true';
Try / catch
try { await check(params); } catch (e) { if (/didn't specify 'language=auto'/.test(e.message)) { delete params.preferredVariants; return check(params); } throw e; } Prevention
- Only attach preferredVariants when language=auto or multilingual=true
- Centralize request assembly to enforce the pairing
- Write an integration test covering the auto+preferredVariants combination
When it happens
Trigger: Calling /v2/check with preferredVariants=de-DE and language=en-US (a concrete language, no multilingual=true) — HTTP 400.
Common situations: Clients that always append preferredVariants to requests even when a fixed language is configured; template code combining parameters from different examples; multilingual mode flag misspelled or set to "false" string.
Understand the failure class
Background: "mutually exclusive" flag errors: what "can't supply both nx and xx", "--raw is not compatible with -i" and "cannot be used with" mean, and how to fix them — this error's family across 29 libraries.
Related errors
- Only either 'text' or 'markup' are supported in an object in
- 'text' cannot be used with 'interpretAs' (only 'markup' can)
- Only 'text' and 'markup' are supported in 'annotation' list:
- Invalid format for 'preferredVariants', expected a dash as i
- Invalid 'preferredVariants', no such language/variant found:
AI-assisted analysis of languagetool-org/languagetool@2e990059ce (2026-09-06).
Data as JSON: /api/errors/a5dd0e6b08172651.
Report an issue: GitHub.