redis/go-redis · error

redis: VectorScoreSliceCmd expects even number of elements,

Error message

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

What it means

VectorScoreSliceCmd parses VEMB/VSIM-style replies as name/score pairs. Under RESP2 the reply is a flat array that must contain an even number of elements; an odd count means the server sent something the client cannot pair up, so it fails before building the []VectorScore.

Source

Thrown at command.go:8596

	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 c5cad058c7)

Solutions

  1. Use RESP3 (set Protocol: 3 in Options) so vector replies arrive as properly shaped maps/arrays.
  2. Check that you are calling the intended command (VSIM with WITHSCORES vs WITHSCORES+WITHATTRIBS produce different arities).
  3. Upgrade go-redis and Redis server to matching, current versions.
  4. Inspect the raw reply with MONITOR or redis-cli to see what the server actually returns.

Example fix

// before: RESP2 flat array parsing fails on odd length
rdb := redis.NewClient(&redis.Options{Protocol: 2})
cmd := rdb.VSimWithScores(ctx, "pts", vector)

// after: use RESP3
rdb := redis.NewClient(&redis.Options{Protocol: 3})
cmd := rdb.VSimWithScores(ctx, "pts", vector)
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure RESP3 is configured for vector-set commands
if rdb.Options().Protocol < 3 {
    // recreate client with Protocol: 3
}

Try / catch

cmd := rdb.VSimWithScores(ctx, "pts", vec)
if err := cmd.Err(); err != nil {
    if strings.Contains(err.Error(), "expects even number of elements") {
        // inspect raw reply and/or switch to RESP3 client
    }
    return err
}

Prevention

When it happens

Trigger: Executing a vector-set command returning a flat [name, score, ...] array (RESP2 protocol) where the array length is odd — e.g. a server bug, truncated reply, or calling the parser against an unexpected command output.

Common situations: Using Protocol: 2 (RESP2) with Redis vector-set commands; custom/modified server returning unexpected element counts; mixing up commands that return 1 vs 3 fields per element.

Related errors


AI-assisted analysis of redis/go-redis@c5cad058c7 (2026-09-01). Data as JSON: /api/errors/417a8f09872d57c7. Report an issue: GitHub.