{"record":{"id":"0964336384c0ced7","repo":"dgraph-io/dgraph","slug":"the-length-of-vectors-must-match","errorCode":null,"errorMessage":"The length of vectors must match","messagePattern":"The length of vectors must match","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"query/math.go","lineNumber":32,"sourceCode":"\t\"github.com/dgraph-io/dgraph/v25/types\"\n)\n\ntype mathTree struct {\n\tFn    string\n\tVar   string\n\tConst types.Val // If its a const value node.\n\tVal   *types.ShardedMap\n\tChild []*mathTree\n}\n\nvar (\n\tErrorIntOverflow     = errors.New(\"Integer overflow\")\n\tErrorFloat32Overflow = errors.New(\"Float32 overflow\")\n\tErrorDivisionByZero  = errors.New(\"Division by zero\")\n\tErrorFractionalPower = errors.New(\"Fractional power of negative number\")\n\tErrorNegativeLog     = errors.New(\"Log of negative number\")\n\tErrorNegativeRoot    = errors.New(\"Root of negative number\")\n\tErrorVectorsNotMatch = errors.New(\"The length of vectors must match\")\n\tErrorArgsDisagree    = errors.New(\"Left and right arguments must match\")\n\tErrorShouldBeVector  = errors.New(\"Type should be []float, but is not. Cannot determine type.\")\n\tErrorBadVectorMult   = errors.New(\"Cannot multiply vector by vector\")\n)\n\n// processBinary handles the binary operands like\n// +, -, *, /, %, max, min, logbase, dot\nfunc processBinary(mNode *mathTree) error {\n\taggName := mNode.Fn\n\n\tmpl := mNode.Child[0].Val\n\tmpr := mNode.Child[1].Val\n\tcl := mNode.Child[0].Const\n\tcr := mNode.Child[1].Const\n\n\tf := func(k uint64, lshard, rshard, destMapi *map[uint64]types.Val) error {\n\t\tag := aggregator{\n\t\t\tname: aggName,","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/query/math.go#L14-L50","documentation":"ErrorVectorsNotMatch is a sentinel error in Dgraph's math package returned when vector operations (+, -, dot product, and the generic Apply) receive two vectors of different lengths. Element-wise operations require equal-length inputs.","triggerScenarios":"math(v1 + v2), math(v1 - v2), or math(dot(v1, v2)) where the []float vector values have different lengths; returned by applyAdd, applySub, applyDot, and Apply.","commonSituations":"Comparing embeddings from different models (different dimensionalities, e.g. 384-dim vs 768-dim); truncated vectors stored before a model change; mixing pre- and post-migration vector data.","solutions":["Re-embed vectors so all data uses the same model/dimensionality","Pad or truncate shorter vectors to a common length before querying","Filter queries to vectors of one dimensionality (e.g. via a version predicate) before applying vector math"],"exampleFix":"// before\nmath(dot(embedding, queryVector)) // 384 vs 768 dims\n// after\n// re-embed data with the 768-dim model, then:\nmath(dot(embedding, queryVector)) // both 768 dims","handlingStrategy":"type-guard","validationCode":"// Validate stored vectors share the same dimensionality before vector math:\nfunction sameDim(a, b) {\n  return Array.isArray(a) && Array.isArray(b) && a.length === b.length;\n}\n// check after fetching vectors used in math(dot(a, b))","typeGuard":"function isVectorOfDim(v, dim) {\n  return Array.isArray(v) && v.length === dim && v.every((x) => typeof x === 'number');\n}","tryCatchPattern":"try {\n  const res = await dgraph.newTxn().query(queryWithVectorMath);\n} catch (e) {\n  if (String(e).includes('length of vectors must match')) {\n    // re-embed or pad/truncate vectors to matching dimensionality\n  } else throw e;\n}","preventionTips":["Pin one embedding model/dimensionality per predicate","Record vector dimension/version in a predicate and filter on it","Reject vectors of unexpected dimension at ingestion time"],"tags":["dgraph","math","vector","dimension-mismatch"],"backgroundTag":"vector-dimension-mismatch","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}