{"id":"f12ebf5b1cd56961","repo":"go-redis/redis","slug":"redis-unexpected-type-t-for-float32","errorCode":null,"errorMessage":"redis: unexpected type=%T for Float32","messagePattern":"redis: unexpected type=%T for Float32","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"command.go","lineNumber":731,"sourceCode":"\tcmd.await()\n\tif cmd.err != nil {\n\t\treturn 0, cmd.err\n\t}\n\treturn toFloat32(cmd.val)\n}\n\nfunc toFloat32(val interface{}) (float32, error) {\n\tswitch val := val.(type) {\n\tcase int64:\n\t\treturn float32(val), nil\n\tcase string:\n\t\tf, err := strconv.ParseFloat(val, 32)\n\t\tif err != nil {\n\t\t\treturn 0, err\n\t\t}\n\t\treturn float32(f), nil\n\tdefault:\n\t\terr := fmt.Errorf(\"redis: unexpected type=%T for Float32\", val)\n\t\treturn 0, err\n\t}\n}\n\nfunc (cmd *Cmd) Float64() (float64, error) {\n\tcmd.await()\n\tif cmd.err != nil {\n\t\treturn 0, cmd.err\n\t}\n\treturn toFloat64(cmd.val)\n}\n\nfunc toFloat64(val interface{}) (float64, error) {\n\tswitch val := val.(type) {\n\tcase int64:\n\t\treturn float64(val), nil\n\tcase string:\n\t\treturn strconv.ParseFloat(val, 64)","sourceCodeStart":713,"sourceCodeEnd":749,"githubUrl":"https://github.com/go-redis/redis/blob/36d97525cd8076aed67cddf54778e9ea84550929/command.go#L713-L749","documentation":"Returned by toFloat32 (command.go:720) when Cmd.Float32() is called on a reply value that is neither int64 nor string. int64 is cast to float32; string is parsed via strconv.ParseFloat(..., 32); any other concrete type yields this error. Typically seen when the command did not actually return a float scalar.","triggerScenarios":"Calling Float32() on a raw Cmd holding an array reply (e.g. GESEARCH wrapped shapes) or a non-numeric bulk; using Float32() on an error-class reply that was stored as something other than string/int64.","commonSituations":"Generic Cmd from Do used where a FloatSliceCmd/FloatWithKeyCmd was needed; wrong accessor; module command whose reply shape changed across versions.","solutions":["Use the typed command (e.g. ZScore → FloatCmd; GeoDist → FloatCmd) whose parser stores the right type.","Type-switch on cmd.Result() when using Do.","Validate cmd.Err() before reading the value."],"exampleFix":"// before\nf, err := client.Do(ctx, \"ZSCORE\", \"s\", \"m\").Float32()\n\n// after\nf64, err := client.ZScore(ctx, \"s\", \"m\").Result()\nf := float32(f64)","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"func asFloat32(cmd *redis.Cmd) (float32, error) {\n    if err := cmd.Err(); err != nil { return 0, err }\n    switch v := cmd.Val().(type) {\n    case int64:\n        return float32(v), nil\n    case string:\n        f, err := strconv.ParseFloat(v, 32)\n        return float32(f), err\n    default:\n        return 0, fmt.Errorf(\"not a float32: %T\", v)\n    }\n}","tryCatchPattern":"f, err := cmd.Float32()\nif err != nil && strings.Contains(err.Error(), \"unexpected type\") {\n    // use a typed float command (ZScore, GeoDist)\n}","preventionTips":["Prefer ZScore/GeoDist FloatCmd accessors for float replies.","Type-switch on Result() for raw Do replies.","Handle redis.Nil before coercing."],"tags":["command","type-conversion","reply-parsing","api-misuse"],"analyzedSha":"36d97525cd8076aed67cddf54778e9ea84550929","analyzedAt":"2026-08-06T01:08:27.376Z","schemaVersion":2}