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 exactly one of start/num is provided. They form the LIMIT clause together (LIMIT start num), so specifying a start offset without a count (or vice versa) is ambiguous and rejected before the command is built.

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 da03cdc7e8)

Solutions

  1. Provide both start and num together (e.g. sort(key, start=0, num=10)).
  2. Omit both to return the full sorted set with no LIMIT.
  3. Normalize pagination inputs so start/num are always a pair.

Example fix

# before
await r.sort('mykey', start=0)
# after
await r.sort('mykey', start=0, num=10)
Defensive patterns

Strategy: validation

Validate before calling

def validate_sort_limit(start, num):
    if (start is None) != (num is None):
        raise ValueError('start and num must both be specified, or both omitted')
    return True

Prevention

When it happens

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

Common situations: Passing start from one config source and num from another where one is None; refactoring pagination code that dropped one argument.

Related errors


AI-assisted analysis of redis/redis-py@da03cdc7e8 (2026-08-04). Data as JSON: /data/errors/d7b709ff49e84e04.json. Report an issue: GitHub.