dgraph-io/dgraph · error

The length of vectors must match

Error message

The length of vectors must match

What it means

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.

Source

Thrown at query/math.go:32

	"github.com/dgraph-io/dgraph/v25/types"
)

type mathTree struct {
	Fn    string
	Var   string
	Const types.Val // If its a const value node.
	Val   *types.ShardedMap
	Child []*mathTree
}

var (
	ErrorIntOverflow     = errors.New("Integer overflow")
	ErrorFloat32Overflow = errors.New("Float32 overflow")
	ErrorDivisionByZero  = errors.New("Division by zero")
	ErrorFractionalPower = errors.New("Fractional power of negative number")
	ErrorNegativeLog     = errors.New("Log of negative number")
	ErrorNegativeRoot    = errors.New("Root of negative number")
	ErrorVectorsNotMatch = errors.New("The length of vectors must match")
	ErrorArgsDisagree    = errors.New("Left and right arguments must match")
	ErrorShouldBeVector  = errors.New("Type should be []float, but is not. Cannot determine type.")
	ErrorBadVectorMult   = errors.New("Cannot multiply vector by vector")
)

// processBinary handles the binary operands like
// +, -, *, /, %, max, min, logbase, dot
func processBinary(mNode *mathTree) error {
	aggName := mNode.Fn

	mpl := mNode.Child[0].Val
	mpr := mNode.Child[1].Val
	cl := mNode.Child[0].Const
	cr := mNode.Child[1].Const

	f := func(k uint64, lshard, rshard, destMapi *map[uint64]types.Val) error {
		ag := aggregator{
			name: aggName,

View on GitHub (pinned to 759e242be6)

Solutions

  1. Re-embed vectors so all data uses the same model/dimensionality
  2. Pad or truncate shorter vectors to a common length before querying
  3. Filter queries to vectors of one dimensionality (e.g. via a version predicate) before applying vector math

Example fix

// before
math(dot(embedding, queryVector)) // 384 vs 768 dims
// after
// re-embed data with the 768-dim model, then:
math(dot(embedding, queryVector)) // both 768 dims
Defensive patterns

Strategy: type-guard

Validate before calling

// Validate stored vectors share the same dimensionality before vector math:
function sameDim(a, b) {
  return Array.isArray(a) && Array.isArray(b) && a.length === b.length;
}
// check after fetching vectors used in math(dot(a, b))

Type guard

function isVectorOfDim(v, dim) {
  return Array.isArray(v) && v.length === dim && v.every((x) => typeof x === 'number');
}

Try / catch

try {
  const res = await dgraph.newTxn().query(queryWithVectorMath);
} catch (e) {
  if (String(e).includes('length of vectors must match')) {
    // re-embed or pad/truncate vectors to matching dimensionality
  } else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01). Data as JSON: /api/errors/0964336384c0ced7. Report an issue: GitHub.