go-redis/redis · error

redis: unexpected type=%T for Float32

Error message

redis: unexpected type=%T for Float32

What it means

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.

Source

Thrown at command.go:731

	cmd.await()
	if cmd.err != nil {
		return 0, cmd.err
	}
	return toFloat32(cmd.val)
}

func toFloat32(val interface{}) (float32, error) {
	switch val := val.(type) {
	case int64:
		return float32(val), nil
	case string:
		f, err := strconv.ParseFloat(val, 32)
		if err != nil {
			return 0, err
		}
		return float32(f), nil
	default:
		err := fmt.Errorf("redis: unexpected type=%T for Float32", val)
		return 0, err
	}
}

func (cmd *Cmd) Float64() (float64, error) {
	cmd.await()
	if cmd.err != nil {
		return 0, cmd.err
	}
	return toFloat64(cmd.val)
}

func toFloat64(val interface{}) (float64, error) {
	switch val := val.(type) {
	case int64:
		return float64(val), nil
	case string:
		return strconv.ParseFloat(val, 64)

View on GitHub (pinned to 36d97525cd)

Solutions

  1. Use the typed command (e.g. ZScore → FloatCmd; GeoDist → FloatCmd) whose parser stores the right type.
  2. Type-switch on cmd.Result() when using Do.
  3. Validate cmd.Err() before reading the value.

Example fix

// before
f, err := client.Do(ctx, "ZSCORE", "s", "m").Float32()

// after
f64, err := client.ZScore(ctx, "s", "m").Result()
f := float32(f64)
Defensive patterns

Strategy: type-guard

Type guard

func asFloat32(cmd *redis.Cmd) (float32, error) {
    if err := cmd.Err(); err != nil { return 0, err }
    switch v := cmd.Val().(type) {
    case int64:
        return float32(v), nil
    case string:
        f, err := strconv.ParseFloat(v, 32)
        return float32(f), err
    default:
        return 0, fmt.Errorf("not a float32: %T", v)
    }
}

Try / catch

f, err := cmd.Float32()
if err != nil && strings.Contains(err.Error(), "unexpected type") {
    // use a typed float command (ZScore, GeoDist)
}

Prevention

When it happens

Trigger: 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.

Common situations: Generic Cmd from Do used where a FloatSliceCmd/FloatWithKeyCmd was needed; wrong accessor; module command whose reply shape changed across versions.

Related errors


AI-assisted analysis of go-redis/redis@36d97525cd (2026-08-06). Data as JSON: /data/errors/f12ebf5b1cd56961.json. Report an issue: GitHub.