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
- Provide both start and num together (e.g. sort(key, start=0, num=10)).
- Omit both to return the full sorted set with no LIMIT.
- 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
- Always pass start and num as a pair.
- Build pagination as a (start, num) tuple and unpack together.
- Omit both when you want the full result set.
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
- when using "groups" the "get" argument must be specified and
- CLIENT KILL type must be one of {client_types!r}
- CLIENT KILL skipme must be a bool
- CLIENT KILL <filter> <value> ... ... <filter> <value> must s
- CLIENT LIST _type must be one of {client_types!r}
AI-assisted analysis of redis/redis-py@da03cdc7e8 (2026-08-04).
Data as JSON: /data/errors/d7b709ff49e84e04.json.
Report an issue: GitHub.