languagetool-org/languagetool · error · IllegalArgumentException

List of language models is empty

Error message

List of language models is empty

What it means

MultiLanguageModel's constructor requires at least one LanguageModel to combine; passing an empty list throws immediately. The class delegates probability lookups to the wrapped models, which is meaningless with none.

Source

Thrown at languagetool-core/src/main/java/org/languagetool/languagemodel/MultiLanguageModel.java:35

 * USA
 */
package org.languagetool.languagemodel;

import org.languagetool.rules.ngrams.Probability;

import java.util.List;

/**
 * Combines the results of several {@link LanguageModel}s.
 * @since 3.2
 */
public class MultiLanguageModel implements LanguageModel {

  private final List<LanguageModel> lms;

  public MultiLanguageModel(List<LanguageModel> lms) {
    if (lms.isEmpty()) {
      throw new IllegalArgumentException("List of language models is empty");
    }
    this.lms = lms;
  }

  @Override
  public Probability getPseudoProbability(List<String> context) {
    double prob = 0;
    float coverage = 0;
    long occurrences = 0;
    for (LanguageModel lm : lms) {
      Probability pProb = lm.getPseudoProbability(context);
      //System.out.println(i + ". " + pProb.getProb() + " (" + pProb.getCoverage() + ")");
      // TODO: decide what's the proper way to combine the probabilities
      prob += pProb.getProb();
      coverage += pProb.getCoverage();
      occurrences += pProb.getOccurrences();
    }
    return new Probability(prob, coverage/lms.size(), occurrences);

View on GitHub (pinned to 2e990059ce)

Solutions

  1. Pass a non-empty list of LanguageModel instances to the constructor.
  2. Before constructing, check list size and fail early with a clear configuration error.
  3. Fix the code that builds the list so model paths are validated/loaded before aggregation.
  4. If models are optional, fall back to a null/absent LanguageModel instead of wrapping an empty list.

Example fix

// before
LanguageModel lm = new MultiLanguageModel(models.stream().filter(m -> exists(m)).collect(toList()));
// after
if (models.isEmpty()) throw new IllegalStateException("No ngram index configured");
LanguageModel lm = new MultiLanguageModel(models);
Defensive patterns

Strategy: validation

Validate before calling

if (models == null || models.isEmpty())
  throw new IllegalArgumentException("At least one LanguageModel is required");
LanguageModel lm = new MultiLanguageModel(models);

Prevention

When it happens

Trigger: new MultiLanguageModel(List.of()) or building the list programmatically (e.g. filtered by existing directories) such that no language models survive before construction.

Common situations: Conditional loading of several ngram models where every path was missing/invalid, producing an empty list that is then handed to the constructor.

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/bbd3454764ce1a68. Report an issue: GitHub.