{"record":{"id":"5135a562db6c8c3e","repo":"apache/cassandra","slug":"invalid-vector-literal-for-s-of-type-s-expected","errorCode":null,"errorMessage":"Invalid vector literal for %s of type %s; expected %d elements, but given %d","messagePattern":"Invalid vector literal for (.+?) of type (.+?); expected (.+?) elements, but given (.+?)","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/terms/Vectors.java","lineNumber":124,"sourceCode":"            if (!receiver.type.isVector())\n                return AssignmentTestable.TestResult.NOT_ASSIGNABLE;\n            VectorType<?> type = (VectorType<?>) receiver.type;\n            if (elements.size() != type.dimension)\n                return AssignmentTestable.TestResult.NOT_ASSIGNABLE;\n            ColumnSpecification valueSpec = valueSpecOf(receiver);\n            return AssignmentTestable.TestResult.testAll(receiver.ksName, valueSpec, elements);\n        }\n\n        @Override\n        public Term prepare(String keyspace, ColumnSpecification receiver) throws InvalidRequestException\n        {\n            AbstractType<?> unwrapped = receiver.type.unwrap();\n\n            if (!unwrapped.isVector())\n                throw new InvalidRequestException(String.format(\"Invalid vector literal for %s of type %s\", receiver.name, receiver.type.asCQL3Type()));\n            VectorType<?> type = (VectorType<?>) unwrapped;\n            if (elements.size() != type.dimension)\n                throw new InvalidRequestException(String.format(\"Invalid vector literal for %s of type %s; expected %d elements, but given %d\", receiver.name, receiver.type.asCQL3Type(), type.dimension, elements.size()));\n\n            ColumnSpecification valueSpec = valueSpecOf(receiver);\n            List<Term> values = new ArrayList<>(elements.size());\n            boolean allTerminal = true;\n            for (Term.Raw rt : elements)\n            {\n                if (!rt.testAssignment(keyspace, valueSpec).isAssignable())\n                    throw new InvalidRequestException(String.format(\"Invalid vector literal for %s: value %s is not of type %s\", receiver.name, rt, valueSpec.type.asCQL3Type()));\n\n                Term t = rt.prepare(keyspace, valueSpec);\n\n                if (t instanceof Term.NonTerminal)\n                    allTerminal = false;\n\n                values.add(t);\n            }\n            MultiElements.DelayedValue value = new MultiElements.DelayedValue(type, values);\n            return allTerminal ? value.bind(FunctionContext.NONE) : value;","sourceCodeStart":106,"sourceCodeEnd":142,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/terms/Vectors.java#L106-L142","documentation":"Thrown when a vector literal's element count does not equal the fixed dimension declared in the vector type (vector<type, N>). Vector types have a fixed dimension, so a literal with fewer or more elements is rejected at prepare time.","triggerScenarios":"`INSERT INTO t (embedding) VALUES [0.1, 0.2]` where embedding is vector<float, 3>; client-side embedding model dimension changed; truncation or padding of embeddings before sending.","commonSituations":"Switching embedding models (e.g. 384-dim to 768-dim) without altering the column dimension; batch pipelines with mixed-length embeddings; off-by-one in generating embeddings.","solutions":["Supply exactly `dimension` elements matching vector<t, N>.","ALTER TABLE ... to change the column to a vector type with the new dimension if the model changed.","Validate embedding lengths in client code before issuing the statement."],"exampleFix":"// before\nINSERT INTO t (id, embedding) VALUES (1, [0.1, 0.2]); // embedding is vector<float, 3>\n// after\nINSERT INTO t (id, embedding) VALUES (1, [0.1, 0.2, 0.3]);","handlingStrategy":"validation","validationCode":"VectorType<?> vt = (VectorType<?>) column.getType().unwrap();\nif (elements.size() != vt.dimension)\n    throw new IllegalArgumentException(\"Expected \" + vt.dimension + \" elements, got \" + elements.size());","typeGuard":null,"tryCatchPattern":"try { session.execute(stmt); } catch (InvalidRequestException e) { if (e.getMessage().contains(\"expected\") && e.getMessage().contains(\"elements\")) padOrTrimEmbedding(); else throw e; }","preventionTips":["Validate embedding lengths against the column dimension client-side","Re-check dimension when switching embedding models","Centralize vector construction in one helper that asserts length"],"tags":["cql","vector","dimension-mismatch"],"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"}