go-redis/redis · error

redis: unexpected type=%T for Uint64

Error message

redis: unexpected type=%T for Uint64

What it means

Returned by toUint64 (command.go:700) when Cmd.Uint64() is called on a reply value that is neither int64 nor string. int64 is cast to uint64; string is parsed via strconv.ParseUint; any other type (array/nil/etc.) yields this error. Common when reading unsigned counters (LPOS, SLOWLOG, some module replies).

Source

Thrown at command.go:707

	}
}

func (cmd *Cmd) Uint64() (uint64, error) {
	cmd.await()
	if cmd.err != nil {
		return 0, cmd.err
	}
	return toUint64(cmd.val)
}

func toUint64(val interface{}) (uint64, error) {
	switch val := val.(type) {
	case int64:
		return uint64(val), nil
	case string:
		return strconv.ParseUint(val, 10, 64)
	default:
		err := fmt.Errorf("redis: unexpected type=%T for Uint64", val)
		return 0, err
	}
}

func (cmd *Cmd) Float32() (float32, error) {
	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)

View on GitHub (pinned to 36d97525cd)

Solutions

  1. Use the typed command whose reply parser stores int64 directly (e.g. IntCmd then cast, or a dedicated Uint64Cmd if provided).
  2. Type-switch on cmd.Result() when using Do.
  3. Confirm cmd.Err() (including redis.Nil) before reading the value.

Example fix

// before
u, err := client.Do(ctx, "STRLEN", "k").Uint64() // STRLEN is integer → ok, but only for int-shaped replies

// after
n, err := client.StrLen(ctx, "k").Result() // Int64
var u uint64 = uint64(n)
Defensive patterns

Strategy: type-guard

Type guard

func asUint64(cmd *redis.Cmd) (uint64, error) {
    if err := cmd.Err(); err != nil { return 0, err }
    switch v := cmd.Val().(type) {
    case int64:
        if v < 0 { return 0, fmt.Errorf("negative: %d", v) }
        return uint64(v), nil
    case string:
        return strconv.ParseUint(v, 10, 64)
    default:
        return 0, fmt.Errorf("not a uint64: %T", v)
    }
}

Try / catch

u, err := cmd.Uint64()
if err != nil && strings.Contains(err.Error(), "unexpected type") {
    // reply was not a numeric scalar — switch accessor
}

Prevention

When it happens

Trigger: Calling Uint64() on a raw Cmd holding an array or non-numeric reply; using Uint64() on a reply that is actually a bulk status string; calling Uint64() when the underlying value is nil.

Common situations: Do-based generic Cmd mis-typed as unsigned; reading a counter from a command that wraps the value inside a sub-array; mismatched accessor vs. command.

Related errors


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