{"record":{"id":"63c1c71467e79dda","repo":"redis/redis-py","slug":"cannot-use-fieldname-alias-with-no-field","errorCode":null,"errorMessage":"Cannot use FIELDNAME alias with no field","messagePattern":"Cannot use FIELDNAME alias with no field","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"redis/commands/search/aggregation.py","lineNumber":51,"sourceCode":"\n    def alias(self, alias: str) -> \"Reducer\":\n        \"\"\"\n        Set the alias for this reducer.\n\n        ### Parameters\n\n        - **alias**: The value of the alias for this reducer. If this is the\n            special value `aggregation.FIELDNAME` then this reducer will be\n            aliased using the same name as the field upon which it operates.\n            Note that using `FIELDNAME` is only possible on reducers which\n            operate on a single field value.\n\n        This method returns the `Reducer` object making it suitable for\n        chaining.\n        \"\"\"\n        if alias is FIELDNAME:\n            if not self._field:\n                raise ValueError(\"Cannot use FIELDNAME alias with no field\")\n            else:\n                # Chop off initial '@', which is optional in field names\n                alias = self._field.removeprefix(\"@\")\n        self._alias = alias\n        return self\n\n    @property\n    def args(self) -> Tuple[str, ...]:\n        return self._args\n\n\nclass SortDirection:\n    \"\"\"\n    This special class is used to indicate sort direction.\n    \"\"\"\n\n    DIRSTRING: Optional[str] = None\n","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/redis/redis-py/blob/6a6b581b48225afa0b76912d1028c6035baee932/redis/commands/search/aggregation.py#L33-L69","documentation":"Raised by Reducer.alias() (redis/commands/search/aggregation.py:51) as a ValueError when the special FIELDNAME sentinel is passed as the alias but the reducer has no single field set (self._field is None). FIELDNAME means 'name the output after the input field', which is only meaningful for single-field reducers like Count's peers; multi-field or fieldless reducers cannot derive a name.","triggerScenarios":"Calling reducer.alias(FIELDNAME) on a reducer constructed without a field, e.g. Count().alias(FIELDNAME) (Count takes no field) or a reducer whose _field was never set.","commonSituations":"Copying a FIELDNAME alias pattern from a single-field reducer example onto a zero/multi-field reducer, or refactoring a reducer and dropping its field argument while keeping the alias call.","solutions":["Only use FIELDNAME on reducers that operate on exactly one field (e.g. Sum, Min, Max on '@field').","For fieldless reducers like Count, pass an explicit alias string instead.","Ensure the reducer's _field is set before calling alias(FIELDNAME)."],"exampleFix":"# before\nfrom redis.commands.search.aggregation import Count, Asc, FIELDNAME\nCount().alias(FIELDNAME)\n# after\nCount().alias('total')","handlingStrategy":"validation","validationCode":"def safe_reducer_alias(reducer, alias):\n    from redis.commands.search.aggregation import FIELDNAME\n    if alias is FIELDNAME and not reducer._field:\n        raise ValueError('FIELDNAME alias requires a single-field reducer')\n    return reducer.alias(alias)","typeGuard":"def reducer_supports_fieldname(reducer) -> bool:\n    return bool(getattr(reducer, '_field', None))","tryCatchPattern":"try:\n    reducer.alias(FIELDNAME)\nexcept ValueError as e:\n    if 'FIELDNAME' in str(e):\n        reducer.alias('value')  # explicit alias\n    else:\n        raise","preventionTips":["Only use FIELDNAME on single-field reducers (Sum/Min/Max/etc. on '@field').","For Count and other fieldless reducers, pass an explicit alias string.","Construct reducers with their field argument before aliasing."],"tags":["search","aggregation","reducer","valueerror","redisearch"],"backgroundTag":null,"analyzedSha":"6a6b581b48225afa0b76912d1028c6035baee932","analyzedAt":"2026-08-10T12:52:44.840Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}