go-redis/redis · error
redis: unexpected type=%T for Bool
Error message
redis: unexpected type=%T for Bool
What it means
Returned by toBool (command.go:764) when Cmd.Bool() is called on a reply value that is neither bool, int64, nor string. bool is returned directly; int64 is true if non-zero; string is parsed via strconv.ParseBool; any other type (array, nil, map) yields this error. Means the command did not return a single boolean-like scalar.
Source
Thrown at command.go:773
func (cmd *Cmd) Bool() (bool, error) {
cmd.await()
if cmd.err != nil {
return false, cmd.err
}
return toBool(cmd.val)
}
func toBool(val interface{}) (bool, error) {
switch val := val.(type) {
case bool:
return val, nil
case int64:
return val != 0, nil
case string:
return strconv.ParseBool(val)
default:
err := fmt.Errorf("redis: unexpected type=%T for Bool", val)
return false, err
}
}
func (cmd *Cmd) Slice() ([]interface{}, error) {
cmd.await()
if cmd.err != nil {
return nil, cmd.err
}
switch val := cmd.val.(type) {
case []interface{}:
return val, nil
default:
return nil, fmt.Errorf("redis: unexpected type=%T for Slice", val)
}
}
func (cmd *Cmd) StringSlice() ([]string, error) {View on GitHub (pinned to 36d97525cd)
Solutions
- Use the typed command (Exists → BoolCmd; SetNX → BoolCmd; SIsMember → BoolCmd).
- Type-switch on cmd.Result() when using Do.
- Check cmd.Err() for redis.Nil before reading the value.
Example fix
// before ok, err := client.Do(ctx, "EXISTS", "k").Bool() // after ok, err := client.Exists(ctx, "k").Result()
Defensive patterns
Strategy: type-guard
Type guard
func asBool(cmd *redis.Cmd) (bool, error) {
if err := cmd.Err(); err != nil { return false, err }
switch v := cmd.Val().(type) {
case bool:
return v, nil
case int64:
return v != 0, nil
case string:
return strconv.ParseBool(v)
default:
return false, fmt.Errorf("not a bool: %T", v)
}
} Try / catch
ok, err := cmd.Bool()
if err != nil && strings.Contains(err.Error(), "unexpected type") {
// reply was not boolean-like — use Exists/SIsMember/SetNX
} Prevention
- Use BoolCmd-returning methods (Exists, SetNX, SIsMember) for boolean replies.
- Distinguish redis.Nil from a present false value before coercing.
- Type-switch on Result() for raw Do replies.
When it happens
Trigger: Calling Bool() on a raw Cmd holding an array reply; calling Bool() on a nil value when redis.Nil was not surfaced as cmd.err; using Bool() where Exists/BoolSliceCmd is the correct accessor.
Common situations: Generic Do Cmd used for a command returning arrays; expecting a bool from a command whose value came back as []interface{}; confusing cmd.Err()==redis.Nil with a present-but-false value.
Related errors
- redis: unexpected type=%T for String
- redis: unexpected type=%T for Int
- redis: unexpected type=%T for Int64
- redis: unexpected type=%T for Uint64
- redis: unexpected type=%T for Float32
AI-assisted analysis of go-redis/redis@36d97525cd (2026-08-06).
Data as JSON: /data/errors/ee1d9cd0d0e3a6b5.json.
Report an issue: GitHub.