apache/cassandra · error · IllegalArgumentException
Cannot convert value
Error message
Cannot convert value %s of type %s
What it means
IllegalStateException from ByteBufferUtil.objectToBytes' fallback branch: the object's runtime type is not one of the supported Java/serialized types (Number, Date, UUID, Collection subtypes handled above), so no known conversion to a ByteBuffer exists. This is a generic terminal guard for unsupported input types in internal object-to-buffer conversion.
Solutions
- Convert the value to a supported type (ByteBuffer, String, number, Date, Boolean) before passing it
- Serialize the POJO yourself to a ByteBuffer using an appropriate type codec
- Extend your value-building code to map unsupported types explicitly
- Check which field/type of your UDT value is the offending class
Example fix
// before
udt.setObject("field", myPojo); // IllegalArgumentException
// after
udt.setObject("field", myPojo.toString()); // or serialize to ByteBuffer via codec Defensive patterns
Strategy: type-guard
Validate before calling
// whitelist supported types before calling objectToBytes
boolean supported = obj instanceof Map || obj instanceof Set || obj instanceof Date
|| obj instanceof ByteBuffer || obj instanceof String || obj instanceof Number
|| obj instanceof Boolean;
if (!supported) throw new IllegalArgumentException("unsupported UDT field type: " + obj.getClass()); Type guard
static boolean isConvertible(Object o) {
return o instanceof Map || o instanceof Set || o instanceof Date
|| o instanceof ByteBuffer || o instanceof String
|| o instanceof Number || o instanceof Boolean;
} Try / catch
try {
ByteBuffer bb = ByteBufferUtil.objectToBytes(value);
} catch (IllegalArgumentException e) {
// serialize manually with the field's codec
} Prevention
- Map POJOs to supported types or explicit ByteBuffers before UDT construction
- Add a unit test covering every field type in your UDTs
- Keep a type-mapping table for driver-to-Cassandra conversions
When it happens
Trigger: Passing an unsupported object (e.g., a POJO, a custom enum instance, or any type without a conversion branch) into objectToBytes via UDT/collection value construction.
Common situations: Application code stuffing arbitrary Java objects into UDT fields; missing a case for a type after adding new custom types.
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
- Expected 1 byte for a tinyint
- Expected 1 or 0 byte value
- Expected 8 or 0 byte long
- Expected instance of
- Invalid 16-bits integer value, expecting 2 bytes but got
AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10).
Data as JSON: /api/errors/180461ffb8f96351.
Report an issue: GitHub.
Appendix: source
Thrown at src/java/org/apache/cassandra/utils/ByteBufferUtil.java:661
}
// decompose/serializer doesn't use the isMultiCell, so safe to do this
return MapType.getInstance(BytesType.instance, BytesType.instance, false).decompose(bbs);
}
else if (obj instanceof Set)
{
Set<?> set = (Set<?>) obj;
// convert subtypes to BB
Set<ByteBuffer> bbs = new LinkedHashSet<>();
for (Object o : set)
if (!bbs.add(objectToBytes(o)))
throw new IllegalStateException("Object " + o + " maps to a buffer that already exists in the set");
// decompose/serializer doesn't use the isMultiCell, so safe to do this
return SetType.getInstance(BytesType.instance, false).decompose(bbs);
}
else if (obj instanceof Date)
return TimestampType.instance.decompose((Date) obj);
else
throw new IllegalArgumentException(String.format("Cannot convert value %s of type %s",
obj,
obj.getClass()));
}
public static ByteBuffer bytes(byte b)
{
return ByteBuffer.allocate(1).put(0, b);
}
public static ByteBuffer bytes(short s)
{
return ByteBuffer.allocate(2).putShort(0, s);
}
public static ByteBuffer bytes(int i)
{
return ByteBuffer.allocate(4).putInt(0, i);
}View on GitHub (pinned to 88fd0f6a0e)