redis/redis-py · error · AttributeError
Did not receive a SortByField.
Error message
Did not receive a SortByField.
What it means
Raised by Query._get_args_tags when the internal _sortby attribute is set but is not a SortbyField instance. The public sort_by() method always constructs a SortbyField, so this error signals that _sortby was assigned directly with a foreign object. It prevents emitting a malformed SORTBY clause.
Source
Thrown at redis/commands/search/query.py:237
if self._with_scores:
args.append("WITHSCORES")
if self._ids:
args.append("INKEYS")
args.append(len(self._ids))
args += self._ids
if self._slop >= 0:
args += ["SLOP", self._slop]
if self._timeout is not None:
args += ["TIMEOUT", self._timeout]
if self._in_order:
args.append("INORDER")
if self._return_fields:
args.append("RETURN")
args.append(len(self._return_fields))
args += self._return_fields
if self._sortby:
if not isinstance(self._sortby, SortbyField):
raise AttributeError("Did not receive a SortByField.")
args.append("SORTBY")
args += self._sortby.args
if self._language:
args += ["LANGUAGE", self._language]
if self._expander:
args += ["EXPANDER", self._expander]
if self._dialect:
args += ["DIALECT", self._dialect]
return args
def paging(self, offset: int, num: int) -> "Query":
"""
Set the paging for the query (defaults to 0..10).
- **offset**: Paging offset for the results. Defaults to 0
- **num**: How many results do we want
"""View on GitHub (pinned to da03cdc7e8)
Solutions
- Use the setter: query.sort_by('price', asc=True).
- If constructing a SortbyField manually, assign it via sort_by or ensure the object is redis.commands.search.query.SortbyField.
- Do not write to query._sortby directly.
Example fix
// before
query._sortby = 'price'
// after
query.sort_by('price', asc=True) Defensive patterns
Strategy: validation
Validate before calling
from redis.commands.search.query import SortbyField
def set_sort(query, field, asc=True):
sb = SortbyField(field, asc) if not isinstance(field, SortbyField) else field
query._sortby = sb # or prefer query.sort_by(field, asc) Type guard
from redis.commands.search.query import SortbyField
def is_sortbyfield(val) -> bool:
return val is None or isinstance(val, SortbyField) Try / catch
try:
query.get_args()
except AttributeError:
query.sort_by('price', asc=True) Prevention
- Always set sort order via query.sort_by(), never by attribute assignment.
- If copying query state, reconstruct SortbyField instances rather than aliasing.
- Avoid exposing _sortby in wrappers.
When it happens
Trigger: Directly assigning query._sortby = 'price' or query._sortby = ('price', 'ASC') instead of calling sort_by(); or a third-party wrapper overwriting the attribute.
Common situations: Attempting to set sort via attribute assignment; deserializing a query config into attributes; copying fields between Query objects without type checks.
Related errors
- Did not receive a Filter object.
- collect sort_by must contain at least one field
- index_type must be one of {list(IndexType)}
- prefix must be provided
- At least one tag must be specified
AI-assisted analysis of redis/redis-py@da03cdc7e8 (2026-08-04).
Data as JSON: /data/errors/033b57a2a8b6a380.json.
Report an issue: GitHub.