go-redis/redis · error

redis: got %d elements in GEOSEARCH reply, expected at least

Error message

redis: got %d elements in GEOSEARCH reply, expected at least %d

What it means

Thrown by GeoSearchLocationCmd.readReply (command.go:5140). For each result entry the parser expects at least one element per requested WITH* flag (WITHDist/WITHHash/WITHCoord), since Redis echoes back exactly those fields. If an entry is shorter than the expected minimum the parser aborts to avoid consuming the next entry's bytes.

Source

Thrown at command.go:5140

	// parser read into the next reply; extra elements are drained below so a
	// longer entry (e.g. from a newer server) can't leave frames on the wire.
	withLen := 1
	if cmd.opt.WithDist {
		withLen++
	}
	if cmd.opt.WithHash {
		withLen++
	}
	if cmd.opt.WithCoord {
		withLen++
	}
	for i := 0; i < n; i++ {
		nn, err := rd.ReadArrayLen()
		if err != nil {
			return err
		}
		if nn < withLen {
			return fmt.Errorf("redis: got %d elements in GEOSEARCH reply, expected at least %d", nn, withLen)
		}

		var loc GeoLocation

		loc.Name, err = rd.ReadString()
		if err != nil {
			return err
		}
		if cmd.opt.WithDist {
			loc.Dist, err = rd.ReadFloat()
			if err != nil {
				return err
			}
		}
		if cmd.opt.WithHash {
			loc.GeoHash, err = rd.ReadInt()
			if err != nil {
				return err

View on GitHub (pinned to 36d97525cd)

Solutions

  1. Run the equivalent GEOSEARCH ... WITHCOORD WITHDIST WITHHASH in redis-cli and confirm each entry has the requested number of fields.
  2. Remove any proxy that may be trimming the reply, or simplify the query to drop WITH* flags and confirm the error disappears.
  3. Upgrade go-redis; the parser drains extra fields but still enforces the minimum.
  4. If transient, retry on a healthy connection.

Example fix

// before
q := &redis.GeoSearchLocationQuery{GeoSearchQuery: redis.GeoSearchQuery{...}, Sort: "ASC"}
q.WithCoord = true; q.WithDist = true; q.WithHash = true
res, err := client.GeoSearchLocation(ctx, "geo", q).Result()
// after: confirm the server really returns the requested fields; if a proxy strips them,
// bypass the proxy or drop the WITH* flags you don't strictly need.
Defensive patterns

Strategy: try-catch

Try / catch

res, err := client.GeoSearchLocation(ctx, key, q).Result()
if err != nil {
    // optionally retry without WITH* flags to confirm a server/proxy shape issue
    q.WithCoord, q.WithDist, q.WithHash = false, false, false
    res, err = client.GeoSearchLocation(ctx, key, q).Result()
    return res, err
}

Prevention

When it happens

Trigger: client.GeoSearchLocation(ctx, key, q) where the GEOSEARCH reply's per-entry array is shorter than the WITH* flags requested. In practice this means a non-conformant server/proxy that drops requested fields, or a corrupted/truncated reply.

Common situations: A RESP-rewriting proxy between client and Redis; a Redis fork that returns a different GEOSEARCH shape; rare corruption during heavy load or a flaky connection.

Related errors


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