redis/redis-py · error · DataError

when using "groups" the "get" argument must be specified…

Error message

when using "groups" the "get" argument must be specified and contain at least two keys

What it means

Raised by sort() when groups=True but the get argument is missing, is a single string/bytes, or has fewer than two entries. The groups option restructures output into tuples built from multiple GET patterns, so at least two GET keys are required. It is a DataError.

Solutions

  1. Pass get as a list with at least two patterns, e.g. get=['name_*', 'score_*'], together with groups=True.
  2. If you only need a single GET projection, drop groups=True and accept a flat list.

Example fix

# before
client.sort('users', groups=True, get='name_*')

# after
client.sort('users', groups=True, get=['name_*', 'score_*'])
Defensive patterns

Strategy: validation

Validate before calling

if groups:
    if not get or isinstance(get, (str, bytes)) or len(list(get)) < 2:
        raise ValueError('groups=True requires get to be a list of >= 2 keys')
client.sort(name, get=get, groups=groups)

Try / catch

from redis.exceptions import DataError
try:
    client.sort(name, get=get, groups=groups)
except DataError as e:
    if 'must be specified' in str(e):
        groups = False
        client.sort(name, get=get, groups=groups)

Prevention

When it happens

Trigger: Calling client.sort(name, groups=True) with no get; client.sort(name, groups=True, get='field_*'); or client.sort(name, groups=True, get=['only_one']).

Common situations: Enabling groups for tuple-style results but providing a single GET pattern, or forgetting the get argument entirely after copying a groups example.

Related errors


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

Appendix: source

Thrown at redis/commands/core.py:5454

        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")
        if alpha:
            pieces.append(b"ALPHA")
        if store is not None:
            pieces.extend([b"STORE", store])
        if groups:
            if not get or isinstance(get, (bytes, str)) or len(get) < 2:
                raise DataError(
                    'when using "groups" the "get" argument '
                    "must be specified and contain at least "
                    "two keys"
                )

        options = {"groups": len(get) if groups else None}
        options["keys"] = [name]
        return self.execute_command("SORT", *pieces, **options)

    @overload
    def sort_ro(
        self: SyncClientProtocol,
        key: str,
        start: int | None = None,
        num: int | None = None,
        by: str | None = None,
        get: list[str] | None = None,
        desc: bool = False,

View on GitHub (pinned to 6a6b581b48)