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

  1. Convert the value to a supported type (ByteBuffer, String, number, Date, Boolean) before passing it
  2. Serialize the POJO yourself to a ByteBuffer using an appropriate type codec
  3. Extend your value-building code to map unsupported types explicitly
  4. 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

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


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)