stanfordnlp/CoreNLP · error · IllegalArgumentException

Invalid value for type

Error message

Invalid value ${value} for type ${typeName}

What it means

When a typed value with TYPE "NUMBER" is evaluated, the string must parse as Long or Double. If the stored value's string contains characters that break numeric parsing, or the value is not a String/Number at all, the evaluator throws this IllegalArgumentException naming the value and requested type.

Solutions

  1. Ensure the string is a valid Java numeric literal: digits with optional '.' and sign; strip units/symbols first.
  2. Use '.' for decimals (Long if no dot, Double if dot) — replace locale commas with dots before evaluation.
  3. Trim the string; empty/whitespace strings will fail parsing.
  4. If the value comes from a capture group, validate it matches -?\d+(\.\d+)? before feeding it into a NUMBER-typed value.

Example fix

// before
(NUMBER) { value: "3px" }
// after
(NUMBER) { value: "3" }  // or Double: (NUMBER) { value: "3.0" }
Defensive patterns

Strategy: validation

Validate before calling

static boolean isNumericLiteral(String s) {
  return s != null && s.trim().matches("-?\\d+(\\.\\d+)?");
}

Type guard

static boolean isValidNumberValue(Object v) {
  if (v instanceof Number) return true;
  return v instanceof String && ((String) v).trim().matches("-?\\d+(\\.\\d+)?");
}

Try / catch

try { Value<?> v = numberExpr.evaluate(env, args); }
catch (IllegalArgumentException ex) {
  if (ex.getMessage().startsWith("Invalid value")) {
    throw new IllegalArgumentException("NUMBER-typed value must parse as Long/Double: " + ex.getMessage(), ex);
  }
  throw ex;
}

Prevention

When it happens

Trigger: Evaluating (NUMBER) { value: "12abc" } or a non-numeric string; whitespace/locale characters (e.g. comma decimal separator); passing a non-String object to a NUMBER-typed value.

Common situations: Rule authors putting units or text in numeric fields ("3px", "1,5"), capture-group substitutions that yield empty or partial strings at match time.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/ling/tokensregex/types/Expressions.java:1342

            }
            case TYPE_STRING:
              return new PrimitiveValue<>(TYPE_STRING, (String) value.get());
            case TYPE_REGEX:
              return new RegexValue((String) value.get());
            /* } else if (TYPE_TOKEN_REGEX.equals(type)) {
       return new PrimitiveValue<TokenSequencePattern>(TYPE_TOKEN_REGEX, (TokenSequencePattern) value.get()); */
            case TYPE_NUMBER:
              if (value.get() instanceof Number) {
                return new PrimitiveValue<>(TYPE_NUMBER, (Number) value.get());
              } else if (value.get() instanceof String) {
                String str = (String) value.get();
                if (str.contains(".")) {
                  return new PrimitiveValue<Number>(TYPE_NUMBER, Double.valueOf(str));
                } else {
                  return new PrimitiveValue<Number>(TYPE_NUMBER, Long.valueOf(str));
                }
              } else {
                throw new IllegalArgumentException("Invalid value " + value + " for type " + typeName);
              }
            default:
              // TODO: support other types
              return new PrimitiveValue(typeName, value.get());
              //throw new UnsupportedOperationException("Cannot convert type " + typeName);
          }
        }
      }
      return null;
    }

    public CompositeValue simplifyNoTypeConversion(Env env, Object... args) {
      Map<String, Expression> m = value;
      Map<String, Expression> res = new HashMap<>(m.size());//Generics.newHashMap (m.size());
      for (Map.Entry<String, Expression> stringExpressionEntry : m.entrySet()) {
        res.put(stringExpressionEntry.getKey(), stringExpressionEntry.getValue().simplify(env));
      }
      return new CompositeValue(res, true);

View on GitHub (pinned to 1b7edd19c4)