redis/redis-py · error · DataError

``start`` and ``num`` must both be specified

Error message

``start`` and ``num`` must both be specified

What it means

Raised by sort() when only one of start or num is provided. These two parameters together form the LIMIT clause for paging; specifying just one is invalid. The client checks that either both are set or both are None. It is a DataError.

Solutions

  1. Provide both start and num together (e.g. start=0, num=10).
  2. If you do not want paging, omit both start and num entirely.

Example fix

# before
client.sort('mylist', start=0)

# after
client.sort('mylist', start=0, num=10)
Defensive patterns

Strategy: validation

Validate before calling

if (start is None) != (num is None):
    raise ValueError('Provide both start and num, or neither')
client.sort(name, start=start, num=num)

Try / catch

from redis.exceptions import DataError
try:
    client.sort(name, start=start, num=num)
except DataError as e:
    if 'must both be specified' in str(e):
        start, num = (0, num) if num is not None else (start, 0)
        client.sort(name, start=start, num=num)

Prevention

When it happens

Trigger: Calling client.sort(name, start=0) without num, or client.sort(name, num=10) without start.

Common situations: Adding pagination and forgetting the second argument, or conditionally passing start but hard-coding num elsewhere. Defaulting one to 0 and leaving the other as None.

Related errors


AI-assisted analysis of redis/redis-py@6a6b581b48 (2026-08-10). Data as JSON: /api/errors/d7b709ff49e84e04. Report an issue: GitHub.

Appendix: source

Thrown at redis/commands/core.py:5429

        ``get`` allows for returning items from external keys rather than the
            sorted data itself.  Use an "*" to indicate where in the key
            the item value is located

        ``desc`` allows for reversing the sort

        ``alpha`` allows for sorting lexicographically rather than numerically

        ``store`` allows for storing the result of the sort into
            the key ``store``

        ``groups`` if set to True and if ``get`` contains at least two
            elements, sort will return a list of tuples, each containing the
            values fetched from the arguments to ``get``.

        For more information, see https://redis.io/commands/sort
        """
        if (start is not None and num is None) or (num is not None and start is None):
            raise DataError("``start`` and ``num`` must both be specified")

        pieces: list[EncodableT] = [name]
        if by is not None:
            pieces.extend([b"BY", by])
        if start is not None and num is not None:
            pieces.extend([b"LIMIT", start, num])
        if get is not None:
            # If get is a string assume we want to get a single value.
            # Otherwise assume it's an iterable and we want to get multiple
            # values. We can't just iterate blindly because strings are
            # iterable.
            if isinstance(get, (bytes, str)):
                pieces.extend([b"GET", get])
            else:
                for g in get:
                    pieces.extend([b"GET", g])
        if desc:
            pieces.append(b"DESC")

View on GitHub (pinned to 6a6b581b48)