stanfordnlp/CoreNLP · error · IllegalStateException

Unknown language

Error message

Unknown language: ${lang}

What it means

toProto converts a Language enum to its protobuf counterpart via a switch; if the switch hits an unmapped default (e.g. a newer Language constant not covered by the cases) it throws IllegalStateException 'Unknown language'.

Solutions

  1. Use matching CoreNLP versions for annotation and serialization
  2. Extend the switch in toProto to map the new Language constant to its CoreNLPProtos.Language equivalent
  3. Fall back to CoreNLPProtos.Language.Unknown for unmapped languages in custom serializer subclasses

Example fix

// before
case Any: return CoreNLPProtos.Language.Any;
default: throw new IllegalStateException("Unknown language: " + lang);
// after
case Any: return CoreNLPProtos.Language.Any;
case Chinese: return CoreNLPProtos.Language.Chinese; // map the new constant
default: return CoreNLPProtos.Language.Unknown; // graceful fallback
Defensive patterns

Strategy: try-catch

Validate before calling

Language lang = doc.get(CoreAnnotations.LanguageAnnotation.class); if (lang != null && !SUPPORTED_LANGS.contains(lang)) { doc.set(CoreAnnotations.LanguageAnnotation.class, Language.Unknown); }

Try / catch

try { proto = serializer.toProto(doc); } catch (IllegalStateException e) { if (e.getMessage().startsWith("Unknown language")) { doc.set(CoreAnnotations.LanguageAnnotation.class, Language.Unknown); proto = serializer.toProto(doc); } else throw e; }

Prevention

When it happens

Trigger: Serializing a document whose LanguageAnnotation value is a Language enum constant not handled by the switch — typically when mixing CoreNLP versions (document annotated with a newer language constant, serialized by an older serializer).

Common situations: Version mismatch between the jar that annotated the document and the jar serializing it; custom Language enum extensions; deserialized/resumed pipelines carrying languages added after the serializer's switch was written.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/da86ee5a38f8da3a. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/pipeline/ProtobufAnnotationSerializer.java:1221

        return CoreNLPProtos.Language.UniversalChinese;
      case English:
        return CoreNLPProtos.Language.English;
      case UniversalEnglish:
        return CoreNLPProtos.Language.UniversalEnglish;
      case German:
        return CoreNLPProtos.Language.German;
      case French:
        return CoreNLPProtos.Language.French;
      case Hebrew:
        return CoreNLPProtos.Language.Hebrew;
      case Spanish:
        return CoreNLPProtos.Language.Spanish;
      case Unknown:
        return CoreNLPProtos.Language.Unknown;
      case Any:
        return CoreNLPProtos.Language.Any;
      default:
        throw new IllegalStateException("Unknown language: " + lang);
    }
  }

  /**
   * Return a Protobuf operator from an OperatorSpec (Natural Logic).
   */
  public static CoreNLPProtos.Operator toProto(OperatorSpec op) {
    return CoreNLPProtos.Operator.newBuilder()
        .setName(op.instance.name()).setQuantifierSpanBegin(op.quantifierBegin).setQuantifierSpanEnd(op.quantifierEnd)
        .setSubjectSpanBegin(op.subjectBegin).setSubjectSpanEnd(op.subjectEnd)
        .setObjectSpanBegin(op.objectBegin).setObjectSpanEnd(op.objectEnd).build();
  }

  /**
   * Return a Protobuf polarity from a CoreNLP Polarity (Natural Logic).
   */
  public static CoreNLPProtos.Polarity toProto(Polarity pol) {
    return CoreNLPProtos.Polarity.newBuilder()

View on GitHub (pinned to 1b7edd19c4)