redis/redis-rb · error · ArgumentError

count argument must be specified

Error message

count argument must be specified

What it means

hrandfield mirrors the server syntax HRANDFIELD key [count [WITHVALUES]]: WITHVALUES only exists on the count form, because with count omitted the command returns a single field and pairing values makes no sense. Passing with_values: true (or the legacy withvalues: alias) while count is nil raises ArgumentError 'count argument must be specified' before anything is sent.

Source

Thrown at lib/redis/commands/hashes.rb:140

      #   redis.hrandfield("hash", 2)
      #     # => ["f1, "f2"]
      # @example Get multiple random fields with values
      #   redis.hrandfield("hash", 2, with_values: true)
      #     # => [["f1", "s1"], ["f2", "s2"]]
      #
      # @param [String] key
      # @param [Integer] count
      # @param [Hash] options
      #   - `:with_values => true`: include values in output
      #
      # @return [nil, String, Array<String>, Array<[String, Float]>]
      #   - when `key` does not exist, `nil`
      #   - when `count` is not specified, a field name
      #   - when `count` is specified and `:with_values` is not specified, an array of field names
      #   - when `:with_values` is specified, an array with `[field, value]` pairs
      def hrandfield(key, count = nil, withvalues: false, with_values: withvalues)
        if with_values && count.nil?
          raise ArgumentError, "count argument must be specified"
        end

        args = [:hrandfield, key]
        args << count if count
        args << "WITHVALUES" if with_values

        parser = Pairify if with_values
        send_command(args, &parser)
      end

      # Delete one or more hash fields.
      #
      # @param [String] key
      # @param [String, Array<String>] field
      # @return [Integer] the number of fields that were removed from the hash
      def hdel(key, *fields)
        fields.flatten!(1)
        send_command([:hdel, key].concat(fields))

View on GitHub (pinned to 2ba9010b91)

Solutions

  1. Pass a count: redis.hrandfield('hash', 5, with_values: true) returns [[field, value], ...] pairs
  2. For a single random field, drop with_values entirely: redis.hrandfield('hash')
  3. When count is dynamic, default it: with_values ? (count || 1) : count

Example fix

# before
redis.hrandfield('hash', with_values: true)  # ArgumentError

# after
redis.hrandfield('hash', 5, with_values: true)  # => [["f1", "v1"], ...]
Defensive patterns

Strategy: validation

Validate before calling

count ||= 1 if with_values
redis.hrandfield(key, count, with_values: with_values)

Prevention

When it happens

Trigger: redis.hrandfield('hash', with_values: true); redis.hrandfield('hash', withvalues: true); count passed conditionally so it ends up nil while with_values is set.

Common situations: Porting from CLI/HRANDFIELD usage; method signatures where count is optional but with_values was defaulted on; older code using the withvalues: spelling.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of redis/redis-rb@2ba9010b91 (2026-08-23). Data as JSON: /api/errors/fe4b58d9755ad69c. Report an issue: GitHub.