stanfordnlp/CoreNLP · error · IllegalArgumentException

Unclosed bracket in encoded map: " + encoded

Error message

Unclosed bracket in encoded map: " + encoded

What it means

A malformed-input error in StringUtils.decodeMap: the encoded map string must wrap its entries in matching brackets; the parser reached the end (or hit inconsistency) without finding the expected closing bracket, so the encoding is corrupt or truncated.

Solutions

  1. Add the missing ']' as the last character
  2. Fix the string builder to pair the opening '[' with a closing ']'
  3. Validate delimiter balance before decoding
  4. Handle IllegalArgumentException where untrusted strings are decoded

Example fix

// before
StringUtils.decodeMap("[a->1");
// after
StringUtils.decodeMap("[a->1]");
Defensive patterns

Strategy: validation

Validate before calling

if (s != null && s.startsWith("[") && !s.endsWith("]")) throw new IllegalArgumentException("Encoded map must end with ']': " + s);

Type guard

static boolean isBracketClosed(String s) { return s != null && s.length() >= 2 && s.charAt(0)=='[' && s.charAt(s.length()-1)==']'; }

Prevention

When it happens

Trigger: Passing a map string starting with '[' whose last character is not ']' (e.g. "[a->1", "[a->1)").

Common situations: Bracket/paren mix-ups when writing encoded maps by hand; code that changed delimiters but not both ends; truncated strings from logs or config edits.

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

Appendix: source

Thrown at src/edu/stanford/nlp/util/StringUtils.java:2631

   * @param encoded The String encoded map
   * @return A String map corresponding to the encoded map
   */
  public static Map<String, String> decodeMap(String encoded){
    if (encoded.isEmpty()) return new HashMap<>();
    char[] chars = encoded.trim().toCharArray();

    //--Parse the String
    //(state)
    char quoteCloseChar = (char) 0;
    Map<String, String> map = new HashMap<>();
    String key = "";
    String value = "";
    boolean onKey = true;
    StringBuilder current = new StringBuilder();
    //(start/stop overhead)
    int start = 0; int end = chars.length;
    if(chars[0] == '('){ start += 1; end -= 1; if(chars[end] != ')') throw new IllegalArgumentException("Unclosed paren in encoded map: " + encoded); }
    if(chars[0] == '['){ start += 1; end -= 1; if(chars[end] != ']') throw new IllegalArgumentException("Unclosed bracket in encoded map: " + encoded); }
    if(chars[0] == '{'){ start += 1; end -= 1; if(chars[end] != '}') throw new IllegalArgumentException("Unclosed bracket in encoded map: " + encoded); }
    //(finite state automata)
    for(int i=start; i<end; i++){
      if (chars[i] == '\r') {
        // Ignore funny windows carriage return
        continue;
      } else if(quoteCloseChar != 0){
        //(case: in quotes)
        if(chars[i] == quoteCloseChar){
          quoteCloseChar = (char) 0;
        }else{
          current.append(chars[i]);
        }
      } else if(chars[i] == '\\'){
        //(case: escaped character)
        if(i == chars.length - 1) {
          throw new IllegalArgumentException("Last character of encoded pair is escape character: " + encoded);
        }

View on GitHub (pinned to 1b7edd19c4)