apache/cassandra · error · InvalidTypeException
Not enough bytes to deserialize collection
Error message
Not enough bytes to deserialize collection
What it means
Thrown by TypeCodec's collection codecs when a received buffer contains fewer bytes than the declared collection size requires, i.e. the payload is truncated or corrupt. The codec catches BufferUnderflowException while deserializing elements and rethrows it as InvalidTypeException. It indicates the serialized bytes on the wire do not conform to the collection wire format (size count then element payloads).
Source
Thrown at src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java:2161
@Override
public C deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion)
{
if (bytes == null || bytes.remaining() == 0) return newInstance(0);
try
{
ByteBuffer input = bytes.duplicate();
int size = CodecUtils.readSize(input, protocolVersion);
C coll = newInstance(size);
for (int i = 0; i < size; i++)
{
ByteBuffer databb = CodecUtils.readValue(input, protocolVersion);
coll.add(eltCodec.deserialize(databb, protocolVersion));
}
return coll;
}
catch (BufferUnderflowException e)
{
throw new InvalidTypeException("Not enough bytes to deserialize collection", e);
}
}
@Override
public String format(C value)
{
if (value == null) return "NULL";
StringBuilder sb = new StringBuilder();
sb.append(getOpeningChar());
int i = 0;
for (E v : value)
{
if (i++ != 0) sb.append(',');
sb.append(eltCodec.format(v));
}
sb.append(getClosingChar());
return sb.toString();
}View on GitHub (pinned to 88fd0f6a0e)
Solutions
- Inspect and regenerate the source of the byte buffer — ensure it was produced by the same protocol version (pass the correct ProtocolVersion to deserialize)
- Verify no incorrect slicing (Buffer slice/position/limit) happened before calling deserialize
- Check that any custom codecs or external writers produce the canonical collection wire format (4-byte count then serialized elements)
- If it comes from storage, validate the data with sstable tools and rewrite the corrupted partition
Example fix
// before
ByteBuffer bad = row.getBytesUnsafe("tags").slice(0, 3); // truncated
List<String> tags = listCodec.deserialize(bad, ProtocolVersion.V4);
// after
ByteBuffer full = row.getBytesUnsafe("tags");
List<String> tags = listCodec.deserialize(full, protocolVersion); Defensive patterns
Strategy: validation
Validate before calling
// before deserialize
ByteBuffer buf = row.getBytesUnsafe("col");
if (buf == null || buf.remaining() < 4)
throw new IllegalStateException("Collection buffer too small to hold element count"); Type guard
static boolean looksLikeCollectionBytes(ByteBuffer b) {
if (b == null || b.remaining() < 4) return false;
int n = b.getInt(b.position());
return n >= 0 && n <= b.remaining();
} Try / catch
try {
return codec.deserialize(buffer, protocolVersion);
} catch (InvalidTypeException e) {
LOG.warn("Malformed/truncated collection bytes: {}", e.getMessage());
return Collections.emptyList(); // or rethrow as data-corruption
} Prevention
- Never slice collection buffers without keeping the full remaining() payload
- Pass the exact ProtocolVersion the bytes were encoded with
- Audit any custom codecs or ETL writers against the canonical collection wire format
When it happens
Trigger: deserialize() on a ByteBuffer that was truncated, sliced incorrectly, produced by a different/incompatible protocol version, or a corrupted UDT/collection value read from storage or a custom payload.
Common situations: Mixing driver protocol versions or codecs across services; manually slicing a row's buffer with wrong offsets; reading a value written by a buggy custom codec; damaged values from SSTable tooling or third-party ETL writing malformed collection bytes.
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
- Invalid type for %s element, expecting %s but got %s
- Cannot parse collection value from "%s", at character %d exp
- Cannot parse collection value from "%s", invalid CQL value a
- Cannot parse collection value from "%s", at character %d exp
- Corrupt flags value for clustering prefix (isStatic flag set
AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10).
Data as JSON: /api/errors/ab73c24a57a8033b.
Report an issue: GitHub.