apache/cassandra · error · InvalidTypeException
Invalid bytes for inet value, got bytes
Error message
Invalid bytes for inet value, got bytes
What it means
Thrown by InetCodec.deserialize when converting the raw bytes to an InetAddress via InetAddress.getByAddress fails with UnknownHostException. This happens when the byte array length is not 4 (IPv4) or 16 (IPv6), since Java only accepts those lengths as raw IP addresses.
Source
Thrown at src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java:1534
}
@Override
public ByteBuffer serialize(InetAddress value, ProtocolVersion protocolVersion)
{
return value == null ? null : ByteBuffer.wrap(value.getAddress());
}
@Override
public InetAddress deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion)
{
if (bytes == null || bytes.remaining() == 0) return null;
try
{
return InetAddress.getByAddress(Bytes.getArray(bytes));
}
catch (UnknownHostException e)
{
throw new InvalidTypeException(
"Invalid bytes for inet value, got " + bytes.remaining() + " bytes");
}
}
}
/**
* This codec maps a CQL {@link DataType#tinyint()} to a Java {@link Byte}.
*/
private static class TinyIntCodec extends PrimitiveByteCodec
{
private static final TinyIntCodec instance = new TinyIntCodec();
private TinyIntCodec()
{
super(tinyint());
}
View on GitHub (pinned to 88fd0f6a0e)
Solutions
- Verify the column type is 'inet' and the payload is 4 or 16 bytes; log bytes.remaining() to see the actual size.
- Use the codec matching the real type (e.g. BytesType/ByteBuffer for blobs, UUIDCodec for uuids).
- Fix upstream writers to store properly sized addresses.
- If deserializing from a custom format, pad/truncate appropriately only after confirming the source encoding.
Example fix
// before
InetAddress a = new InetCodec().deserialize(bytes, ProtocolVersion.V4);
// after
int n = bytes.remaining();
if (n != 4 && n != 16)
throw new IllegalArgumentException("Expected 4 or 16 bytes for inet, got " + n);
InetAddress a = new InetCodec().deserialize(bytes, ProtocolVersion.V4); Defensive patterns
Strategy: validation
Validate before calling
int n = bytes == null ? 0 : bytes.remaining();
if (n != 0 && n != 4 && n != 16)
throw new IllegalArgumentException("inet needs 4 or 16 bytes, got " + n); Type guard
boolean isInetSized(ByteBuffer b) { int n = b == null ? 0 : b.remaining(); return n == 0 || n == 4 || n == 16; } Try / catch
try { return codec.deserialize(bytes, protocolVersion); }
catch (InvalidTypeException e) {
log.warn("Inet deserialization failed, {} bytes", bytes.remaining());
return null;
} Prevention
- Verify column type is inet before decoding.
- Never decode blobs/uuids/varints with InetCodec.
- Check schema consistency across environments.
- Log byte length on failure.
When it happens
Trigger: Calling InetCodec.deserialize(bytes, protocolVersion) with a ByteBuffer whose remaining byte count is not 4 or 16, e.g. deserializing a varint, uuid, or truncated value as inet.
Common situations: Schema mismatches after ALTER TYPE changes, storing raw byte blobs and misreading them as inet, or legacy migration data with non-standard address lengths.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- Invalid 64-bits double value, expecting 8 bytes but got
- Invalid 32-bits float value, expecting 4 bytes but got
- Invalid 8-bits integer value, expecting 1 byte but got %d
- Invalid 16-bits integer value, expecting 2 bytes but got %d
- Not enough bytes to read size of %dth field %s
AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10).
Data as JSON: /api/errors/c32dc3736e2cd809.
Report an issue: GitHub.