apache/cassandra · error · InvalidTypeException
Malformed collection value "%s", missing closing '%s'
Error message
Malformed collection value "%s", missing closing '%s'
What it means
This InvalidTypeException is thrown by the collection codec's parse() method when a CQL literal string representing a collection (list/set) ends before the expected closing character is found. The parser consumed all opening values but ran out of characters, meaning brackets are unbalanced. The library throws it because the string cannot correspond to any valid CQL collection literal.
Source
Thrown at src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java:2227
"Cannot parse collection value from \"%s\", invalid CQL value at character %d",
value, idx),
e);
}
l.add(eltCodec.parse(value.substring(idx, n)));
idx = n;
idx = ParseUtils.skipSpaces(value, idx);
if (value.charAt(idx) == getClosingChar()) return l;
if (value.charAt(idx++) != ',')
throw new InvalidTypeException(
String.format(
"Cannot parse collection value from \"%s\", at character %d expecting ',' but got '%c'",
value, idx, value.charAt(idx)));
idx = ParseUtils.skipSpaces(value, idx);
}
throw new InvalidTypeException(
String.format(
"Malformed collection value \"%s\", missing closing '%s'", value, getClosingChar()));
}
@Override
public boolean accepts(Object value)
{
if (getJavaType().getRawType().isAssignableFrom(value.getClass()))
{
// runtime type ok, now check element type
Collection<?> coll = (Collection<?>) value;
if (coll.isEmpty()) return true;
Object elt = coll.iterator().next();
return eltCodec.accepts(elt);
}
return false;
}
View on GitHub (pinned to 88fd0f6a0e)
Solutions
- Print the full value being parsed and add the missing closing character
- Escape or quote string elements correctly (e.g. 'a''b' for embedded quotes) so brackets are not consumed by a quoted token
- Validate the literal balance (count opening/closing chars) before calling parse()
- Use format() on a typed collection instead of hand-writing literals
Example fix
// before
codec.parse("[1,2,3"); // InvalidTypeException
// after
codec.parse("[1,2,3]"); Defensive patterns
Strategy: validation
Validate before calling
static boolean isBalancedCollection(String s) {
int depth = 0; boolean inQuote = false;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '\'' ) inQuote = !inQuote;
else if (!inQuote) {
if (c == '[' || c == '<') depth++;
if (c == ']' || c == '>') depth--;
}
}
return depth == 0 && !inQuote;
}
if (!isBalancedCollection(value)) throw new IllegalArgumentException("unbalanced collection literal: " + value);
codec.parse(value); Type guard
boolean isClosedCollection(String s) {
return s != null && !s.isEmpty() && isBalancedCollection(s);
} Try / catch
try {
T v = codec.parse(literal);
} catch (InvalidTypeException e) {
log.error("Malformed collection literal: {}", e.getMessage());
throw new IllegalArgumentException("Fix collection literal syntax", e);
} Prevention
- Count opening/closing brackets before parsing hand-built literals
- Always escape single quotes inside string elements (double them)
- Prefer codec.format(typedCollection) over hand-concatenating literals
When it happens
Trigger: Calling codec.parse() (or deserializing a string via TypeCodec) with a truncated or unbalanced collection literal such as "[1,2,3" or "['a','b'" — the closing ']' (or '>' for sets) never appears before end-of-string.
Common situations: Manually built CQL literal strings that were truncated by string-slicing or logging; user-supplied input missing a bracket; code that formats collections without escaping/quoting elements properly so a quote swallows the closing bracket.
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.
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- Malformed map value "%s", missing closing '}'
- text or varchar values must be enclosed by single quotes
- cannot parse map value from "%s", at character %d expecting
- Cannot parse map value from "%s", invalid CQL value at chara
- Cannot parse map value from "%s", at character %d expecting
AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10).
Data as JSON: /api/errors/86eadf05023661d3.
Report an issue: GitHub.