{"record":{"id":"e2d0246e7557ba5c","repo":"apache/cassandra","slug":"attempted-to-add-float-vector-of-dimension-d-to","errorCode":null,"errorMessage":"Attempted to add float vector of dimension %d to %s","messagePattern":"Attempted to add float vector of dimension (.+?) to (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/db/marshal/VectorType.java","lineNumber":175,"sourceCode":"\n    public ByteBuffer decompose(T... values)\n    {\n        return decompose(Arrays.asList(values));\n    }\n\n    public ByteBuffer decomposeAsFloat(float[] value)\n    {\n        return decomposeAsFloat(ByteBufferAccessor.instance, value);\n    }\n\n    public <V> V decomposeAsFloat(ValueAccessor<V> accessor, float[] value)\n    {\n        if (value == null)\n            rejectNullOrEmptyValue();\n        if (!(elementType instanceof FloatType))\n            throw new IllegalStateException(\"Attempted to read as float, but element type is \" + elementType.asCQL3Type());\n        if (value.length != dimension)\n            throw new IllegalArgumentException(String.format(\"Attempted to add float vector of dimension %d to %s\", value.length, asCQL3Type()));\n        // TODO : should we use TypeSizes to be consistent with other code?  Its the same value at the end of the day...\n        V buffer = accessor.allocate(Float.BYTES * dimension);\n        int offset = 0;\n        for (int i = 0; i < dimension; i++)\n        {\n            accessor.putFloat(buffer, offset, value[i]);\n            offset+= Float.BYTES;\n        }\n        return buffer;\n    }\n\n    public <V> V pack(List<V> elements, ValueAccessor<V> accessor)\n    {\n        return getSerializer().pack(elements, accessor);\n    }\n\n    @Override\n    public List<ByteBuffer> filterSortAndValidateElements(List<ByteBuffer> buffers)","sourceCodeStart":157,"sourceCodeEnd":193,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/db/marshal/VectorType.java#L157-L193","documentation":"decomposeAsFloat(float[]) serializes a Java float array into the vector's binary form and requires the array length to equal the vector type's declared dimension. Throwing IllegalArgumentException when they differ prevents silently writing a vector with the wrong element count.","triggerScenarios":"Calling vectorType.decomposeAsFloat(new float[n]) where n != the dimension the VectorType was created with (e.g. dimension 3 type given a float[4]).","commonSituations":"Embedding/vector-search code where model output dimension changed (different embedding model version) but the table schema still declares the old dimension.","solutions":["Ensure the float array length matches the declared dimension of the VectorType","ALTER the table's vector column to the new dimension if the model changed","Check the embedding model's output size before calling decomposeAsFloat"],"exampleFix":"// before\nfloat[] embedding = model.embed(text); // length 768\nbuffer = vectorType3.decomposeAsFloat(embedding); // dimension 3\n// after\nif (embedding.length == vectorType.getDimension())\n    buffer = vectorType.decomposeAsFloat(embedding);","handlingStrategy":"validation","validationCode":"if (value == null || value.length != vectorType.getDimension())\n    throw new IllegalArgumentException(\"expected dimension \" + vectorType.getDimension() + \", got \" + (value == null ? -1 : value.length));\nByteBuffer buf = vectorType.decomposeAsFloat(value);","typeGuard":null,"tryCatchPattern":"try { buf = vt.decomposeAsFloat(arr); } catch (IllegalArgumentException e) { /* dimension drift: reload schema or resize */ }","preventionTips":["Assert embedding length against schema dimension at startup","Re-check dimensions when upgrading embedding models"],"tags":["cassandra","vector-type","dimension-mismatch","arrays"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}