go-redis/redis · error

redis: unexpected type=%T for Float64

Error message

redis: unexpected type=%T for Float64

What it means

Returned by toFloat64 (command.go:744) when Cmd.Float64() is called on a reply value that is neither int64 nor string. int64 is cast to float64; string is parsed via strconv.ParseFloat(..., 64); any other concrete type yields this error. Usually means the accessor was used against a reply that did not carry a single float scalar.

Source

Thrown at command.go:751

	}
}

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)
	default:
		err := fmt.Errorf("redis: unexpected type=%T for Float64", val)
		return 0, err
	}
}

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

View on GitHub (pinned to 36d97525cd)

Solutions

  1. Use the typed command (ZScore/GeoDist → FloatCmd; ZDiffWithScores → ZSliceCmd).
  2. Type-switch on cmd.Result() when using Do.
  3. Handle redis.Nil before coercing the value.

Example fix

// before
f, err := client.Do(ctx, "GEODIST", "g", "a", "b").Float64()

// after
d, err := client.GeoDist(ctx, "g", "a", "b", "m").Result()
Defensive patterns

Strategy: type-guard

Type guard

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

Try / catch

f, err := cmd.Float64()
if err != nil && strings.Contains(err.Error(), "unexpected type") {
    // switch to a typed float command
}

Prevention

When it happens

Trigger: Calling Float64() on a raw Cmd holding an array (e.g. Do of a command that returns a list of scores); calling Float64() on a nil/empty reply without a prior nil-check; using Float64() where a FloatSliceCmd accessor is required.

Common situations: Generic Cmd from Do mis-typed; module reply shape change across versions; wrong accessor for a sorted-set command that returns members+scores.

Related errors


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