redis/redis-py · error · ValueError
collect fields must be '*' or a non-empty list of names
Error message
collect fields must be '*' or a non-empty list of names
What it means
Raised by the collect reducer constructor when the fields argument is neither the literal '*' nor a non-empty collection of field names. The COLLECT reducer must project at least one field (or all fields via '*'); an empty selection is rejected locally before reaching the server.
Solutions
- Use collect() or collect(fields='*') to project all fields.
- Pass a non-empty list of real field names: collect(fields=['price', 'name']).
- Guard dynamic field lists: if not fields: fields = '*'.
Example fix
// before r = collect(fields=field_list) # field_list == [] // after r = collect(fields=field_list or '*') # or r = collect(fields=['price'])
Defensive patterns
Strategy: validation
Validate before calling
fields = fields if (fields == '*' or (fields and all(str(f).strip() for f in fields))) else '*' collect(fields=fields)
Try / catch
try:
r = collect(fields=fields)
except ValueError:
r = collect(fields='*') Prevention
- Default to '*' when the field list is unknown/empty.
- Strip and validate field names before passing.
When it happens
Trigger: Calling collect(fields=[]), collect(fields=['']), collect(fields=[' ']), or collect(fields=()) — any iterable that yields no usable name.
Common situations: Dynamically building the field list from config/user input that resolved to empty. Whitespace-only field strings. Forgetting the '*' default.
Related errors
- collect sort_by must contain at least one field
- Cannot use FIELDNAME alias with no field
- AGGREGATION requires exactly one aggregation spec per key
- At least one tag must be specified
- Bad query type
AI-assisted analysis of redis/redis-py@6a6b581b48 (2026-08-10).
Data as JSON: /api/errors/bd11357e53e16767.
Report an issue: GitHub.
Appendix: source
Thrown at redis/commands/search/reducers.py:236
- **distinct**: When ``True``, emit ``DISTINCT`` to deduplicate entries
with identical projected fields. Forward-compatible: this option is
not yet implemented by the server and currently produces a server
error when sent.
- **sort_by**: An ``Asc``/``Desc`` instance or an iterable of them, used
to order the collected entries within each group.
- **limit**: An ``(offset, count)`` pair. Returns at most ``count``
entries per group after skipping ``offset``. With ``sort_by`` this
acts as a top-N selection.
"""
args: list[str] = []
# FIELDS (required)
if fields == "*":
args += ["FIELDS", "*"]
else:
names = [fields] if isinstance(fields, str) else list(fields)
if not names or any(not n.strip() for n in names):
raise ValueError(
"collect fields must be '*' or a non-empty list of names"
)
names = [_ensure_at_prefix(n) for n in names]
args += ["FIELDS", str(len(names))] + names
# DISTINCT (optional)
if distinct:
args += ["DISTINCT"]
# SORTBY (optional)
if sort_by is not None:
sort_fields = [sort_by] if isinstance(sort_by, (Asc, Desc)) else sort_by
sort_args: list[str] = []
for f in sort_fields:
sort_args += [_ensure_at_prefix(f.field), f.DIRSTRING]
if not sort_args:
raise ValueError("collect sort_by must contain at least one field")
args += ["SORTBY", str(len(sort_args))] + sort_argsView on GitHub (pinned to 6a6b581b48)