WRONGTYPE error
A command was used on a key that holds a different data type.
Redis returns this error when you run a command on a key that holds a different data type than the command works with. Each key holds one data type, such as a string, a list, or a hash, and each command works with only one of them.
WRONGTYPE Operation against a key holding the wrong kind of value
Some commands word this error differently. JSON commands return Existing key has wrong Redis type. HyperLogLog commands return WRONGTYPE Key is not a valid HyperLogLog string value. for a key that holds a string but not a HyperLogLog.
In client libraries
| Client library | Exception or error type | Message |
|---|---|---|
| redis-py | ResponseError |
WRONGTYPE Operation against a key holding the wrong kind of value |
| Jedis | JedisDataException |
WRONGTYPE Operation against a key holding the wrong kind of value |
| node-redis | SimpleErrorTest with err.message.startsWith('WRONGTYPE') |
WRONGTYPE Operation against a key holding the wrong kind of value |
| Lettuce The async API wraps the exception in an ExecutionException (get()) or a CompletionException (join()). |
RedisCommandExecutionException |
WRONGTYPE Operation against a key holding the wrong kind of value |
| go-redis | redis.ErrorTest with redis.HasErrorPrefix(err, "WRONGTYPE") |
WRONGTYPE Operation against a key holding the wrong kind of value |
| ioredis | ReplyErrorTest with err.message.startsWith('WRONGTYPE') |
WRONGTYPE Operation against a key holding the wrong kind of value |
| StackExchange.Redis NRedisStack uses StackExchange.Redis, so it raises the same exceptions. The sync and async APIs raise the same exceptions. |
RedisServerException |
WRONGTYPE Operation against a key holding the wrong kind of value |
| Predis | Predis\Response\ServerExceptionTest with $e->getErrorType() === 'WRONGTYPE' |
WRONGTYPE Operation against a key holding the wrong kind of value |
| redis-rb | Redis::WrongTypeError |
WRONGTYPE Operation against a key holding the wrong kind of value (redis://<host>:<port>) |
| redis-rs The sync and async APIs raise the same errors. |
ErrorKind::ExtensionTest with err.code() == Some("WRONGTYPE") |
"WRONGTYPE": Operation against a key holding the wrong kind of value |
Example
GET works with strings, so it fails on a key that holds a list:
> RPUSH mylist "a"
(integer) 1
> GET mylist
(error) WRONGTYPE Operation against a key holding the wrong kind of value
How to fix it
Find out what the key holds with TYPE:
> TYPE mylist
list
Then check which of these causes applies:
- You used a command for a different type. Use a command for the key's type instead. For example, use
LRANGEto read a list, andHGETto read a field of a hash. See Data types for the commands each type supports. - Two parts of your app use the same key name for different data. Give each kind of data its own key prefix, such as
user:for hashes andqueue:for lists. - The key still holds data from an older version of your app. If you no longer need the old data, delete the key with
DELand write it again.