stanfordnlp/CoreNLP · error · java.lang.UnsupportedOperationException

Cannot set from string

Error message

Cannot set from string

What it means

CoreLabel.setFromString always throws UnsupportedOperationException: a CoreLabel holds a map of key/value pairs and has no defined textual representation that can be parsed back, so the API refuses to implement string-based label construction. It exists only to satisfy the AbstractMapLabel/Label interface contract. Any call to setFromString on a CoreLabel is by definition unsupported.

Solutions

  1. Do not call setFromString on CoreLabel; construct it via new CoreLabel(String) is also unsupported — instead build the CoreLabel and set fields with setWord/setTag/setCategory/setValue.
  2. Use the label factory: new CoreLabelFactory().newLabelFromString(str) only if a string form was produced by that factory, otherwise parse the string yourself and set individual keys.
  3. If you have an existing Label, create a CoreLabel via new CoreLabel(existingLabel) (copy constructor) rather than a string round-trip.

Example fix

// before
CoreLabel cl = new CoreLabel();
cl.setFromString("word/PoS"); // throws UnsupportedOperationException
// after
String[] parts = "word/PoS".split("/");
CoreLabel cl = new CoreLabel();
cl.setWord(parts[0]);
cl.setTag(parts[1]);
Defensive patterns

Strategy: try-catch

Validate before calling

if (label instanceof CoreLabel) {
  throw new IllegalStateException("CoreLabel does not support setFromString; set fields individually");
}

Type guard

boolean supportsSetFromString(Label l) {
  return !(l instanceof CoreLabel) && !(l instanceof IndexedWord);
}

Try / catch

try {
  label.setFromString(str);
} catch (UnsupportedOperationException e) {
  // fall back to per-field construction: setWord/setTag/setCategory
  CoreLabel cl = new CoreLabel();
  cl.setWord(parseWord(str));
  cl.setTag(parseTag(str));
  label = cl;
}

Prevention

When it happens

Trigger: Calling CoreLabel.setFromString(String) directly, or routing a generic Label through code that calls label.setFromString(...) (e.g. LabelFactory-style utilities that reconstruct labels from strings) when the label is actually a CoreLabel.

Common situations: Generic serialization/deserialization code that round-trips labels via toString/setFromString; pipeline code that copies labels from a String token source into CoreLabels; migrating code written for StringLabel/TaggedWord (which support setFromString) to CoreLabel.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/ling/CoreLabel.java:362

  public <KEY extends Key<String>> String getString(Class<KEY> key) {
    return this.getString(key, "");
  }

  @Override
  public <KEY extends Key<String>> String getString(Class<KEY> key, String def) {
    String value = get(key);
    if (value == null) {
      return def;
    }
    return value;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void setFromString(String labelStr) {
    throw new UnsupportedOperationException("Cannot set from string");
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public final void setValue(String value) {
    set(CoreAnnotations.ValueAnnotation.class, value);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public final String value() {
    return get(CoreAnnotations.ValueAnnotation.class);
  }

View on GitHub (pinned to 1b7edd19c4)