go-redis/redis · error

redis: unexpected type=%T for Int64

Error message

redis: unexpected type=%T for Int64

What it means

Returned by toInt64 (command.go:680) when Cmd.Int64() is called on a reply value that is neither int64 nor string. int64 (integer reply) is returned directly; string is parsed via strconv.ParseInt; any other concrete type (array, nil, map) falls through to this error.

Source

Thrown at command.go:687

	}
}

func (cmd *Cmd) Int64() (int64, error) {
	cmd.await()
	if cmd.err != nil {
		return 0, cmd.err
	}
	return toInt64(cmd.val)
}

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

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)

View on GitHub (pinned to 36d97525cd)

Solutions

  1. Use the typed command method that matches the reply (IntCmd, Int64SliceCmd, etc.).
  2. Type-switch on cmd.Result() before converting when using Do.
  3. Handle redis.Nil and other errors before reading val.

Example fix

// before
v, err := client.Do(ctx, "GET", "counter").Int64() // GET returns bulk-string; works only if numeric, fails for non-numeric

// after — prefer typed Get + parse defensively
v, err := client.Get(ctx, "counter").Int64() // StringCmd.Int64 parses the bulk string
if err != nil { return 0, err }
Defensive patterns

Strategy: type-guard

Type guard

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

Try / catch

n, err := cmd.Int64()
if err != nil && strings.Contains(err.Error(), "unexpected type") {
    // switch to a typed command or type-switch on Result()
}

Prevention

When it happens

Trigger: Calling Int64() on a Cmd holding an array reply (e.g. raw Do of a command that returns a list) or a non-numeric bulk; calling Int64() on a Float-typed reply that arrived as something other than string/int64.

Common situations: Generic Cmd from Do mis-typed; wrong accessor for the command; expecting an integer from a command that embeds the count inside an array.

Related errors


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