{"record":{"id":"bd11357e53e16767","repo":"redis/redis-py","slug":"collect-fields-must-be-or-a-non-empty-list-of","errorCode":null,"errorMessage":"collect fields must be '*' or a non-empty list of names","messagePattern":"collect fields must be '\\*' or a non-empty list of names","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"redis/commands/search/reducers.py","lineNumber":236,"sourceCode":"        - **distinct**: When ``True``, emit ``DISTINCT`` to deduplicate entries\n            with identical projected fields. Forward-compatible: this option is\n            not yet implemented by the server and currently produces a server\n            error when sent.\n        - **sort_by**: An ``Asc``/``Desc`` instance or an iterable of them, used\n            to order the collected entries within each group.\n        - **limit**: An ``(offset, count)`` pair. Returns at most ``count``\n            entries per group after skipping ``offset``. With ``sort_by`` this\n            acts as a top-N selection.\n        \"\"\"\n        args: list[str] = []\n\n        # FIELDS (required)\n        if fields == \"*\":\n            args += [\"FIELDS\", \"*\"]\n        else:\n            names = [fields] if isinstance(fields, str) else list(fields)\n            if not names or any(not n.strip() for n in names):\n                raise ValueError(\n                    \"collect fields must be '*' or a non-empty list of names\"\n                )\n            names = [_ensure_at_prefix(n) for n in names]\n            args += [\"FIELDS\", str(len(names))] + names\n\n        # DISTINCT (optional)\n        if distinct:\n            args += [\"DISTINCT\"]\n\n        # SORTBY (optional)\n        if sort_by is not None:\n            sort_fields = [sort_by] if isinstance(sort_by, (Asc, Desc)) else sort_by\n            sort_args: list[str] = []\n            for f in sort_fields:\n                sort_args += [_ensure_at_prefix(f.field), f.DIRSTRING]\n            if not sort_args:\n                raise ValueError(\"collect sort_by must contain at least one field\")\n            args += [\"SORTBY\", str(len(sort_args))] + sort_args","sourceCodeStart":218,"sourceCodeEnd":254,"githubUrl":"https://github.com/redis/redis-py/blob/6a6b581b48225afa0b76912d1028c6035baee932/redis/commands/search/reducers.py#L218-L254","documentation":"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.","triggerScenarios":"Calling collect(fields=[]), collect(fields=['']), collect(fields=['   ']), or collect(fields=()) — any iterable that yields no usable name.","commonSituations":"Dynamically building the field list from config/user input that resolved to empty. Whitespace-only field strings. Forgetting the '*' default.","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 = '*'."],"exampleFix":"// before\nr = collect(fields=field_list)  # field_list == []\n// after\nr = collect(fields=field_list or '*')\n# or\nr = collect(fields=['price'])","handlingStrategy":"validation","validationCode":"fields = fields if (fields == '*' or (fields and all(str(f).strip() for f in fields))) else '*'\ncollect(fields=fields)","typeGuard":null,"tryCatchPattern":"try:\n    r = collect(fields=fields)\nexcept ValueError:\n    r = collect(fields='*')","preventionTips":["Default to '*' when the field list is unknown/empty.","Strip and validate field names before passing."],"tags":["redisearch","aggregation","reducer","argument-validation"],"backgroundTag":null,"analyzedSha":"6a6b581b48225afa0b76912d1028c6035baee932","analyzedAt":"2026-08-10T12:52:44.840Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}