redis/go-redis · error

unexpected type %T, want map or key/value array

Error message

unexpected type %T, want map or key/value array

What it means

parseCollectEntry accepts only a map[string]interface{} (RESP3) or a flat even-length []interface{} (RESP2) as a COLLECT entry. Any other type (string, int64, nil inside the array, nested arrays) is rejected with this error naming the actual type found.

Source

Thrown at search_collect.go:233

		}
		return out, nil
	case map[string]interface{}: // already string-keyed
		return m, nil
	case []interface{}: // RESP2 flat [field, value, field, value, ...]
		if len(m)%2 != 0 {
			return nil, fmt.Errorf("odd-length key/value array of length %d", len(m))
		}
		out := make(CollectEntry, len(m)/2)
		for i := 0; i < len(m); i += 2 {
			key, ok := m[i].(string)
			if !ok {
				key = fmt.Sprint(m[i])
			}
			out[key] = m[i+1]
		}
		return out, nil
	default:
		return nil, fmt.Errorf("unexpected type %T, want map or key/value array", e)
	}
}

View on GitHub (pinned to c5cad058c7)

Solutions

  1. Inspect the %T in the message and correct the reply source (server version, proxy) to emit maps or flat arrays
  2. Use RESP3 so entries decode as map[string]interface{}
  3. Fix test fixtures to use realistic map or flat-array entries

Example fix

// before
arr := []interface{}{"just-a-string"} // entry is not a map or kv array
// after
arr := []interface{}{map[string]interface{}{"name": "v"}}
Defensive patterns

Strategy: type-guard

Type guard

func isCollectEntry(v interface{}) bool {
    switch v.(type) {
    case map[string]interface{}:
        return true
    case []interface{}:
        arr := v.([]interface{})
        return len(arr)%2 == 0
    default:
        return false
    }
}

Prevention

When it happens

Trigger: A COLLECT reply array containing an element of an unexpected Go type (e.g. a scalar or nested array) passed to parseCollectEntry from parseCollectValue.

Common situations: Proxies or non-standard module versions emitting entries in an unlisted shape; test fixtures with hand-built reply values of the wrong type.

Related errors


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