{"record":{"id":"033b57a2a8b6a380","repo":"redis/redis-py","slug":"did-not-receive-a-sortbyfield","errorCode":null,"errorMessage":"Did not receive a SortByField.","messagePattern":"Did not receive a SortByField\\.","errorType":"validation","errorClass":"AttributeError","httpStatus":null,"severity":"error","filePath":"redis/commands/search/query.py","lineNumber":241,"sourceCode":"        if self._with_scores:\n            args.append(\"WITHSCORES\")\n        if self._ids:\n            args.append(\"INKEYS\")\n            args.append(len(self._ids))\n            args += self._ids\n        if self._slop >= 0:\n            args += [\"SLOP\", self._slop]\n        if self._timeout is not None:\n            args += [\"TIMEOUT\", self._timeout]\n        if self._in_order:\n            args.append(\"INORDER\")\n        if self._return_fields:\n            args.append(\"RETURN\")\n            args.append(len(self._return_fields))\n            args += self._return_fields\n        if self._sortby:\n            if not isinstance(self._sortby, SortbyField):\n                raise AttributeError(\"Did not receive a SortByField.\")\n            args.append(\"SORTBY\")\n            args += self._sortby.args\n        if self._language:\n            args += [\"LANGUAGE\", self._language]\n        if self._expander:\n            args += [\"EXPANDER\", self._expander]\n        if self._dialect:\n            args += [\"DIALECT\", self._dialect]\n\n        return args\n\n    def paging(self, offset: int, num: int) -> \"Query\":\n        \"\"\"\n        Set the paging for the query (defaults to 0..10).\n\n        - **offset**: Paging offset for the results. Defaults to 0\n        - **num**: How many results do we want\n        \"\"\"","sourceCodeStart":223,"sourceCodeEnd":259,"githubUrl":"https://github.com/redis/redis-py/blob/6a6b581b48225afa0b76912d1028c6035baee932/redis/commands/search/query.py#L223-L259","documentation":"Raised during Query._get_args_tags() when Query._sortby is set to something that is not a redis.commands.search.query.SortbyField instance. The public Query.sort_by() method always constructs a SortbyField, so this only fires when _sortby is assigned directly with the wrong type.","triggerScenarios":"Directly assigning query._sortby = ('price', 'ASC') or a string instead of using Query.sort_by(). Subclassing Query and overriding sort handling without producing a SortbyField.","commonSituations":"Internal code or tests that bypass the sort_by() setter. Serialization-time discovery of a wrongly typed internal field.","solutions":["Use the public setter: query.sort_by('price', asc=False).","If assigning _sortby directly, wrap it: query._sortby = SortbyField('price', asc=False).","Audit custom Query subclasses to ensure they emit SortbyField instances."],"exampleFix":"// before\nquery._sortby = ('price', 'DESC')\n// after\nquery.sort_by('price', asc=False)\n# or\nfrom redis.commands.search.query import SortbyField\nquery._sortby = SortbyField('price', asc=False)","handlingStrategy":"validation","validationCode":"# Always use the public setter\nquery.sort_by('price', asc=False)\n# If assigning _sortby directly:\nfrom redis.commands.search.query import SortbyField\nassert isinstance(query._sortby, SortbyField)","typeGuard":"from redis.commands.search.query import SortbyField\n\ndef is_sortby_field(s) -> bool:\n    return isinstance(s, SortbyField)","tryCatchPattern":"try:\n    query.get_args()\nexcept AttributeError as e:\n    if 'SortByField' in str(e):\n        query.sort_by('price', asc=False)\n    else:\n        raise","preventionTips":["Use Query.sort_by() rather than mutating _sortby.","In subclasses, always store SortbyField instances."],"tags":["redisearch","query","sort","argument-validation"],"backgroundTag":null,"analyzedSha":"6a6b581b48225afa0b76912d1028c6035baee932","analyzedAt":"2026-08-10T12:52:44.840Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}