{"id":"befc22cc7e97b9f4","repo":"redis/redis-py","slug":"index-type-must-be-one-of-list-indextype","errorCode":null,"errorMessage":"index_type must be one of {list(IndexType)}","messagePattern":"index_type must be one of (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"redis/commands/search/index_definition.py","lineNumber":43,"sourceCode":"        payload_field=None,\n        index_type=None,\n    ):\n        self.args = []\n        self._append_index_type(index_type)\n        self._append_prefix(prefix)\n        self._append_filter(filter)\n        self._append_language(language_field, language)\n        self._append_score(score_field, score)\n        self._append_payload(payload_field)\n\n    def _append_index_type(self, index_type):\n        \"\"\"Append `ON HASH` or `ON JSON` according to the enum.\"\"\"\n        if index_type is IndexType.HASH:\n            self.args.extend([\"ON\", \"HASH\"])\n        elif index_type is IndexType.JSON:\n            self.args.extend([\"ON\", \"JSON\"])\n        elif index_type is not None:\n            raise RuntimeError(f\"index_type must be one of {list(IndexType)}\")\n\n    def _append_prefix(self, prefix):\n        \"\"\"Append PREFIX.\"\"\"\n        if prefix is None:\n            raise TypeError(\"prefix must be provided\")\n        if len(prefix) > 0:\n            prefix = list_or_args(prefix, [])\n            self.args.append(\"PREFIX\")\n            self.args.append(len(prefix))\n            for p in prefix:\n                self.args.append(p)\n\n    def _append_filter(self, filter):\n        \"\"\"Append FILTER.\"\"\"\n        if filter is not None:\n            self.args.append(\"FILTER\")\n            self.args.append(filter)\n","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/redis/redis-py/blob/da03cdc7e8731092b13e395605c3c1fb2de25de1/redis/commands/search/index_definition.py#L25-L61","documentation":"Raised by IndexDefinition._append_index_type when the index_type argument is not None and is not a member of the IndexType enum (HASH or JSON). The library refuses to emit an 'ON <type>' clause for an unknown type because Redis would reject the FT.CREATE anyway with a less helpful error. Pass an IndexType enum member or omit the argument.","triggerScenarios":"Constructing IndexDefinition with a plain string or integer instead of the enum, e.g. IndexDefinition(prefix=['doc:'], index_type='HASH') or IndexDefinition(index_type=1). The check fires in __init__ before any Redis round-trip.","commonSituations":"Copy-pasting a string literal 'HASH'/'JSON' from documentation or examples that show the wire token rather than the Python enum; passing a value read from a config file/YAML as a raw string; upgrading code that previously used magic strings.","solutions":["Import the enum and pass a member: from redis.commands.search.index_definition import IndexDefinition, IndexType, then index_type=IndexType.HASH.","If you do not care about the ON clause, omit index_type entirely (defaults to None, server picks HASH).","If building from dynamic input, normalize it first: IndexType[str(val).upper()] to convert a string to the enum."],"exampleFix":"// before\nIndexDefinition(prefix=['doc:'], index_type='HASH')\n// after\nfrom redis.commands.search.index_definition import IndexDefinition, IndexType\nIndexDefinition(prefix=['doc:'], index_type=IndexType.HASH)","handlingStrategy":"type-guard","validationCode":"from redis.commands.search.index_definition import IndexType\n\ndef coerce_index_type(val):\n    if val is None or isinstance(val, IndexType):\n        return val\n    if isinstance(val, str):\n        return IndexType[val.upper()]\n    raise ValueError(f\"Cannot coerce {val!r} to IndexType\")\n\n# usage: IndexDefinition(prefix=p, index_type=coerce_index_type(raw))","typeGuard":"from redis.commands.search.index_definition import IndexType\n\ndef is_index_type(val) -> bool:\n    return val is None or isinstance(val, IndexType)","tryCatchPattern":"try:\n    IndexDefinition(prefix=p, index_type=raw)\nexcept RuntimeError:\n    # fall back to default or re-raise with guidance\n    IndexDefinition(prefix=p)","preventionTips":["Always pass IndexType.HASH or IndexType.JSON, never a raw string.","Centralize IndexDefinition construction in one helper that validates the type.","If loading config values, normalize strings to the enum before construction."],"tags":["redistimeseries","search","search-and-query","enum","validation","argument-error"],"analyzedSha":"da03cdc7e8731092b13e395605c3c1fb2de25de1","analyzedAt":"2026-08-04T20:26:47.563Z","schemaVersion":2}