redis/redis-py · error · DataError

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

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, or has fewer than two entries. groups instructs Redis to return tuples built from multiple GET lookups, so at least two GET patterns are required to form each group.

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

Solutions

  1. Provide get as a list of at least two patterns, e.g. sort(key, groups=True, get=['obj_*->a', 'obj_*->b']).
  2. Drop groups=True if you only need a single GET pattern.
  3. Ensure get is a list/tuple (not a bare string) when groups is enabled.

Example fix

# before
await r.sort('mykey', groups=True, get='obj_*->name')
# after
await r.sort('mykey', groups=True, get=['obj_*->name', 'obj_*->age'])
Defensive patterns

Strategy: validation

Validate before calling

def validate_sort_groups(groups: bool, get):
    if groups:
        if not get or isinstance(get, (bytes, str)) or len(get) < 2:
            raise ValueError('groups=True requires get as a list of at least two patterns')
    return True

Prevention

When it happens

Trigger: Calling client.sort(key, groups=True) with no get, or client.sort(key, groups=True, get='field_*') with a single pattern, or get=['a'] with one element.

Common situations: Enabling groups for multi-field joins but supplying a single GET pattern; passing a string instead of a list for get when groups is set.

Related errors


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