{"id":"91fd68d935274d48","repo":"go-redis/redis","slug":"redis-unexpected-type-t-for-slice","errorCode":null,"errorMessage":"redis: unexpected type=%T for Slice","messagePattern":"redis: unexpected type=%T for Slice","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"command.go","lineNumber":787,"sourceCode":"\t\treturn val != 0, nil\n\tcase string:\n\t\treturn strconv.ParseBool(val)\n\tdefault:\n\t\terr := fmt.Errorf(\"redis: unexpected type=%T for Bool\", val)\n\t\treturn false, err\n\t}\n}\n\nfunc (cmd *Cmd) Slice() ([]interface{}, error) {\n\tcmd.await()\n\tif cmd.err != nil {\n\t\treturn nil, cmd.err\n\t}\n\tswitch val := cmd.val.(type) {\n\tcase []interface{}:\n\t\treturn val, nil\n\tdefault:\n\t\treturn nil, fmt.Errorf(\"redis: unexpected type=%T for Slice\", val)\n\t}\n}\n\nfunc (cmd *Cmd) StringSlice() ([]string, error) {\n\tslice, err := cmd.Slice()\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tss := make([]string, len(slice))\n\tfor i, iface := range slice {\n\t\tval, err := toString(iface)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t\tss[i] = val\n\t}\n\treturn ss, nil","sourceCodeStart":769,"sourceCodeEnd":805,"githubUrl":"https://github.com/go-redis/redis/blob/36d97525cd8076aed67cddf54778e9ea84550929/command.go#L769-L805","documentation":"Returned by Cmd.Slice() (command.go:778) when the reply value is not []interface{}. Slice() expects an array reply already coerced to a Go slice; any scalar type (string, int64, bool, nil) yields this error. Indicates the command did not return an array, or the wrong accessor was used.","triggerScenarios":"Calling Slice() on a Cmd whose reply is a scalar (e.g. Do INCR then Slice()); calling Slice() on a nil reply; using Slice() where the typed accessor (StringSliceCmd, Int64SliceCmd) is required.","commonSituations":"Generic Do Cmd mis-typed; expecting an array from a command that returns a scalar on a special case (e.g. LRANGE on a non-list returns WRONGTYPE, but a nil/empty case returns nil val); confusing redis.Nil with an empty array.","solutions":["Use the typed command method that returns the correct slice shape (LRange → StringSliceCmd; ZRange → StringSliceCmd).","Type-switch on cmd.Result() when using Do.","Handle redis.Nil and empty-array cases explicitly before calling Slice()."],"exampleFix":"// before\nv, err := client.Do(ctx, \"LRANGE\", \"q\", 0, -1).Slice()\n\n// after\nelems, err := client.LRange(ctx, \"q\", 0, -1).Result()","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"func asSlice(cmd *redis.Cmd) ([]interface{}, error) {\n    if err := cmd.Err(); err != nil { return nil, err }\n    if s, ok := cmd.Val().([]interface{}); ok {\n        return s, nil\n    }\n    return nil, fmt.Errorf(\"not a slice: %T\", cmd.Val())\n}","tryCatchPattern":"s, err := cmd.Slice()\nif err != nil && strings.Contains(err.Error(), \"unexpected type\") {\n    // reply was not an array — switch to the typed accessor\n}","preventionTips":["Use StringSliceCmd/Int64SliceCmd-returning methods for array replies.","Type-switch on Result() for raw Do replies.","Handle redis.Nil vs empty-array explicitly."],"tags":["command","type-conversion","reply-parsing","api-misuse"],"analyzedSha":"36d97525cd8076aed67cddf54778e9ea84550929","analyzedAt":"2026-08-06T01:08:27.376Z","schemaVersion":2}