go-redis/redis · error

redis: VectorScoreSliceCmd expects even number of elements,

Error message

redis: VectorScoreSliceCmd expects even number of elements, got %d

What it means

Returned by VectorScoreSliceCmd.readReply (command.go:8585) when parsing a RESP2 reply from VSimWithScores or VSimWithArgsWithScores. In RESP2 the server returns a flat array of [name, score, name, score, ...] pairs; an odd element count means the name/score pairing is broken. Under RESP3 the reply is a proper map and this code path is not reached.

Source

Thrown at command.go:8585

	typ, err := rd.PeekReplyType()
	if err != nil {
		return err
	}

	var n int
	if typ == proto.RespMap {
		n, err = rd.ReadMapLen()
		if err != nil {
			return err
		}
	} else {
		// RESP2 returns a flat array [name, score, name, score, ...]
		n, err = rd.ReadArrayLen()
		if err != nil {
			return err
		}
		if n%2 != 0 {
			return fmt.Errorf("redis: VectorScoreSliceCmd expects even number of elements, got %d", n)
		}
		n /= 2
	}

	cmd.val = make([]VectorScore, n)
	for i := 0; i < n; i++ {
		name, err := rd.ReadString()
		if err != nil {
			return err
		}
		cmd.val[i].Name = name

		score, err := rd.ReadFloat()
		if err != nil {
			return err
		}
		cmd.val[i].Score = score
	}

View on GitHub (pinned to 36d97525cd)

Solutions

  1. Set Protocol: 3 in redis.Options so VSIM returns a structured RESP3 map, bypassing flat-array pair parsing entirely
  2. Upgrade Redis and the VectorSet module to a version whose VSIM reply format matches this go-redis release
  3. Verify the server actually supports the VectorSet module and the WITHSCORES option

Example fix

// before
rdb := redis.NewClient(&redis.Options{Addr: ":6379"})
res, err := rdb.VSimWithScores(ctx, "mykey", vec).Result()

// after - use RESP3 to get map-structured replies
rdb := redis.NewClient(&redis.Options{
    Addr:     ":6379",
    Protocol: 3,
})
res, err := rdb.VSimWithScores(ctx, "mykey", vec).Result()
Defensive patterns

Strategy: validation

Validate before calling

// Use RESP3 to avoid flat-array pair parsing issues with all VSIM commands
opts := &redis.Options{
    Addr:     ":6379",
    Protocol: 3,
}
rdb := redis.NewClient(opts)

Try / catch

res, err := rdb.VSimWithScores(ctx, key, vec).Result()
if err != nil {
    if strings.Contains(err.Error(), "expects even number of elements") {
        log.Error("VSIM RESP2 format mismatch; switch to Protocol:3 or upgrade Redis VectorSet module")
    }
    return nil, err
}

Prevention

When it happens

Trigger: Calling VSimWithScores or VSimWithArgsWithScores (VSIM key ... WITHSCORES) while the client is using RESP2 (the default) and the server returns an array with an odd number of elements.

Common situations: VectorSet module version mismatch where the server's VSIM reply format differs from what go-redis expects; a RESP-intermediating proxy mangling the flat array; a server-side bug in the VECTORSET module's VSIM WITHSCORES serialization.

Related errors


AI-assisted analysis of go-redis/redis@36d97525cd (2026-08-06). Data as JSON: /data/errors/7cf7b8a2fc34fec7.json. Report an issue: GitHub.