go-redis/redis · error

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

Error message

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

What it means

Error "redis: got %d elements in the map, wanted %d" thrown in go-redis/redis.

Source

Thrown at internal/proto/reader.go:742

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

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

// ReadMapLen reads the length of the map type.
// If responding to the array type (RespArray/RespSet/RespPush),
// it must be a multiple of 2 and return n/2.
// Other types will return an error.
func (r *Reader) ReadMapLen() (int, error) {
	line, err := r.ReadLine()
	if err != nil {
		return 0, err
	}
	switch line[0] {
	case RespMap:
		return replyLen(line)
	case RespArray, RespSet, RespPush:
		// Some commands and RESP2 protocol may respond to array types.

View on GitHub (pinned to 36d97525cd)

Solutions

  1. Verify the map reply has the expected number of elements; reconnect on desync
  2. Parse with the generic reader if the shape varies

When it happens

Trigger: Thrown at internal/proto/reader.go:742 when the library encounters an invalid state.

Common situations: Validate map element counts before iterating key/value pairs.


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