redis/go-redis · error

redis: got %d elements in the array, wanted %d

Error message

redis: got %d elements in the array, wanted %d

What it means

ReadFixedArrayLen asserts the reply array has an exact expected number of elements. The server returned an array with a different count, so the fixed-shape contract (e.g. a positional reply where element order matters) is broken. This guards against silently misinterpreting positionally-encoded data.

Source

Thrown at internal/proto/reader.go:716

	return s == "OK" || s == "1" || s == "true", nil
}

func (r *Reader) ReadSlice() ([]interface{}, error) {
	line, err := r.ReadLine()
	if err != nil {
		return nil, err
	}
	return r.readSlice(line)
}

// ReadFixedArrayLen read fixed array length.
func (r *Reader) ReadFixedArrayLen(fixedLen int) error {
	n, err := r.ReadArrayLen()
	if err != nil {
		return err
	}
	if n != fixedLen {
		return fmt.Errorf("redis: got %d elements in the array, wanted %d", n, fixedLen)
	}
	return nil
}

// ReadArrayLen Read and return the length of the array.
func (r *Reader) ReadArrayLen() (int, error) {
	line, err := r.ReadLine()
	if err != nil {
		return 0, err
	}
	switch line[0] {
	case RespArray, RespSet, RespPush:
		return replyLen(line)
	default:
		return 0, fmt.Errorf("redis: can't parse array/set/push reply: %.100q", line)
	}
}

View on GitHub (pinned to c5cad058c7)

Solutions

  1. Pin the Redis server version the reply shape was written for, or add version handling around the command
  2. Check server version with INFO and compare against the parser's expected fixed length
  3. If the reply legitimately varies, switch to ReadArrayLen and validate count dynamically instead of a fixed length
  4. Confirm no proxy/cluster middleware is dropping or duplicating reply elements

Example fix

// before: strict fixed length
err := reader.ReadFixedArrayLen(2)
// after: tolerant parse
n, err := reader.ReadArrayLen()
if err != nil { return err }
if n != 2 { return fmt.Errorf("unexpected position reply arity %d", n) }
Defensive patterns

Strategy: validation

Validate before calling

// check server version compatibility before fixed-arity commands
info, _ := client.Info(ctx, "server").Result()
// compare redisVersion against the version the reply shape was written for

Try / catch

err := reader.ReadFixedArrayLen(2)
if err != nil && strings.Contains(err.Error(), "wanted") {
  return fmt.Errorf("reply shape changed, server version mismatch: %w", err)
}

Prevention

When it happens

Trigger: Calling ReadFixedArrayLen(n) (via readReply for commands like GEOPOS-style or readPosition, readXInfoStreamGroupPending, readXInfoStreamConsumers) when the server returns an array of a different length — typically a different Redis version changed the reply, or an error/NIL reply changed shape.

Common situations: Redis server version differences (a command gained/lost reply fields); Redis Cluster/proxy rewriting replies; running a command against Redis Enterprise or modules with altered output.

Related errors


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