redis/redis-py · error · ValueError

Cannot use FIELDNAME alias with no field

Error message

Cannot use FIELDNAME alias with no field

What it means

Raised by Reducer.alias() when called with the special sentinel aggregation.FIELDNAME but the reducer has no field bound to it. The FIELDNAME alias copies the reducer's operating field name, so it requires a single-field reducer (e.g. Count does not have one).

Source

Thrown at redis/commands/search/aggregation.py:51

    def alias(self, alias: str) -> "Reducer":
        """
        Set the alias for this reducer.

        ### Parameters

        - **alias**: The value of the alias for this reducer. If this is the
            special value `aggregation.FIELDNAME` then this reducer will be
            aliased using the same name as the field upon which it operates.
            Note that using `FIELDNAME` is only possible on reducers which
            operate on a single field value.

        This method returns the `Reducer` object making it suitable for
        chaining.
        """
        if alias is FIELDNAME:
            if not self._field:
                raise ValueError("Cannot use FIELDNAME alias with no field")
            else:
                # Chop off initial '@', which is optional in field names
                alias = self._field.removeprefix("@")
        self._alias = alias
        return self

    @property
    def args(self) -> Tuple[str, ...]:
        return self._args


class SortDirection:
    """
    This special class is used to indicate sort direction.
    """

    DIRSTRING: Optional[str] = None

View on GitHub (pinned to da03cdc7e8)

Solutions

  1. Provide an explicit alias string instead of FIELDNAME for field-less reducers.
  2. Ensure the reducer is constructed with a field before calling alias(FIELDNAME).
  3. Guard: call alias(FIELDNAME) only when reducer._field is set.

Example fix

// before
Count().alias(aggregation.FIELDNAME)
// after
Count().alias("total")
Defensive patterns

Strategy: validation

Validate before calling

import redis.commands.search.aggregation as agg

def safe_alias(reducer, alias):
    if alias is agg.FIELDNAME and not getattr(reducer, "_field", None):
        raise ValueError("FIELDNAME alias requires a single-field reducer")
    return reducer.alias(alias)

Type guard

def reducer_has_field(reducer) -> bool:
    return bool(getattr(reducer, "_field", None))

Try / catch

try:
    reducer.alias(agg.FIELDNAME)
except ValueError as e:
    if "FIELDNAME" in str(e):
        reducer.alias("value")  # explicit fallback
    else:
        raise

Prevention

When it happens

Trigger: Call reducer.alias(aggregation.FIELDNAME) on a reducer constructed without a field, such as Count() or any reducer whose _field attribute is None.

Common situations: Reusing a FIELDNAME alias template across all reducers in a pipeline including field-less reducers; refactoring that drops a reducer's field argument.

Related errors


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