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

  1. Set language=auto when using preferredVariants
  2. Or add multilingual=true if you genuinely want multilingual checking
  3. Or remove preferredVariants when a concrete language is set
  4. 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

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


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