redis/redis-py · error · TypeError
prefix must be provided
Error message
prefix must be provided
What it means
Raised by IndexDefinition._append_prefix when the prefix argument is exactly None. The constructor default is an empty list ([]), so this only triggers when a caller explicitly passes None, which the library treats as a programming mistake rather than 'no prefix'. Pass an empty list to index all keys, or a list of key-prefix strings.
Source
Thrown at redis/commands/search/index_definition.py:48
self._append_prefix(prefix)
self._append_filter(filter)
self._append_language(language_field, language)
self._append_score(score_field, score)
self._append_payload(payload_field)
def _append_index_type(self, index_type):
"""Append `ON HASH` or `ON JSON` according to the enum."""
if index_type is IndexType.HASH:
self.args.extend(["ON", "HASH"])
elif index_type is IndexType.JSON:
self.args.extend(["ON", "JSON"])
elif index_type is not None:
raise RuntimeError(f"index_type must be one of {list(IndexType)}")
def _append_prefix(self, prefix):
"""Append PREFIX."""
if prefix is None:
raise TypeError("prefix must be provided")
if len(prefix) > 0:
prefix = list_or_args(prefix, [])
self.args.append("PREFIX")
self.args.append(len(prefix))
for p in prefix:
self.args.append(p)
def _append_filter(self, filter):
"""Append FILTER."""
if filter is not None:
self.args.append("FILTER")
self.args.append(filter)
def _append_language(self, language_field, language):
"""Append LANGUAGE_FIELD and LANGUAGE."""
if language_field is not None:
self.args.append("LANGUAGE_FIELD")
self.args.append(language_field)View on GitHub (pinned to da03cdc7e8)
Solutions
- Pass an empty list for no prefix filtering: IndexDefinition(prefix=[]).
- If the value may be None, coalesce it: IndexDefinition(prefix=prefix or []).
- Provide the intended prefix list, e.g. IndexDefinition(prefix=['product:']).
Example fix
// before
IndexDefinition(prefix=config.get('prefix'))
// after
IndexDefinition(prefix=config.get('prefix') or []) Defensive patterns
Strategy: validation
Validate before calling
def safe_prefix(p):
return p if p is not None else []
# usage: IndexDefinition(prefix=safe_prefix(raw_prefix)) Type guard
def is_prefix_ok(p) -> bool:
return p is not None and hasattr(p, '__len__') Try / catch
try:
IndexDefinition(prefix=raw)
except TypeError:
IndexDefinition(prefix=[]) Prevention
- Never pass prefix=None; use [] for 'no prefix'.
- Coalesce optional values: prefix or [].
- Document that None is rejected while [] is valid.
When it happens
Trigger: IndexDefinition(prefix=None) or forwarding a variable that is None, e.g. IndexDefinition(prefix=config.get('prefix')) when the key is absent and dict.get returns None.
Common situations: Loading prefix from optional config/env that yields None; refactoring a call site that previously omitted prefix and then someone added prefix=None explicitly believing it means 'no prefix'.
Related errors
- index_type must be one of {list(IndexType)}
- Did not receive a Filter object.
- Did not receive a SortByField.
- At least one tag must be specified
- collect fields must be '*' or a non-empty list of names
AI-assisted analysis of redis/redis-py@da03cdc7e8 (2026-08-04).
Data as JSON: /data/errors/b6cc84a7d8c22622.json.
Report an issue: GitHub.