mem0ai/mem0 · error · ValueError

Invalid index_type: {index_type}. Must be 'hnsw' or 'flat'

Error message

Invalid index_type: {index_type}. Must be 'hnsw' or 'flat'

What it means

Raised in Valkey.__init__ when the normalized index_type (lowercased at assignment) is neither 'hnsw' nor 'flat'. These are the only two algorithms Valkey's FT.CREATE supports for vector fields, so an unknown value cannot map to a 'VECTOR' schema algorithm; failing in the constructor prevents a confusing Redis error at collection-creation time.

Source

Thrown at mem0/vector_stores/valkey.py:96

            index_type (str, optional): Index type ('hnsw' or 'flat'). Defaults to "hnsw".
            hnsw_m (int, optional): HNSW M parameter (connections per node). Defaults to 16.
            hnsw_ef_construction (int, optional): HNSW ef_construction parameter. Defaults to 200.
            hnsw_ef_runtime (int, optional): HNSW ef_runtime parameter. Defaults to 10.
            cluster_mode (bool, optional): Enable cluster mode for Valkey cluster (CME) deployments. Defaults to False.
        """
        self.embedding_model_dims = embedding_model_dims
        self.collection_name = collection_name
        self.prefix = f"mem0:{collection_name}"
        self.timezone = timezone
        self.index_type = index_type.lower()
        self.hnsw_m = hnsw_m
        self.hnsw_ef_construction = hnsw_ef_construction
        self.hnsw_ef_runtime = hnsw_ef_runtime
        self.cluster_mode = cluster_mode

        # Validate index type
        if self.index_type not in ["hnsw", "flat"]:
            raise ValueError(f"Invalid index_type: {index_type}. Must be 'hnsw' or 'flat'")

        # Connect to Valkey
        try:
            if self.cluster_mode:
                from valkey.cluster import ValkeyCluster

                self.client = ValkeyCluster.from_url(valkey_url)
            else:
                self.client = valkey.from_url(valkey_url)
            logger.debug(f"Successfully connected to Valkey at {valkey_url} (cluster_mode={cluster_mode})")
        except Exception as e:
            logger.exception(f"Failed to connect to Valkey at {valkey_url}: {e}")
            raise

        # Create the index schema
        self._create_index(embedding_model_dims)

    def _build_index_schema(self, collection_name, embedding_dims, distance_metric, prefix):

View on GitHub (pinned to 001c235229)

Solutions

  1. Set index_type to exactly 'hnsw' (graph index, tunable m/ef_construction/ef_runtime) or 'flat' (brute-force, exact).
  2. Case does not matter ('HNSW' works) but spelling does — remove engine-specific suffixes like '_FLAT' or 'IVF'.
  3. Validate config at load time if it comes from user input or external files.

Example fix

# before
config = {"vector_store": {"provider": "valkey", "config": {"index_type": "ivfflat", ...}}}

# after
config = {"vector_store": {"provider": "valkey", "config": {"index_type": "hnsw", "hnsw_m": 16, ...}}}
Defensive patterns

Strategy: validation

Validate before calling

def valid_index_type(v) -> str:
    s = str(v).lower()
    if s not in ("hnsw", "flat"):
        raise ValueError(f"index_type must be 'hnsw' or 'flat', got {v!r}")
    return s

Type guard

def is_valid_index_type(v) -> bool:
    return isinstance(v, str) and v.lower() in ("hnsw", "flat")

Try / catch

try:
    store = Valkey(valkey_url=..., index_type=index_type, embedding_model_dims=1536)
except ValueError as e:
    if "index_type" in str(e):
        raise RuntimeError(f"Bad Valkey index_type {index_type!r}; use 'hnsw' or 'flat'") from e
    raise

Prevention

When it happens

Trigger: `"index_type": "HNSW"` is fine (lowercased) but `"index_type": "ivf"`, `"hnsw3"`, `"flat_"`, or a typo like `"flAT2"` raises; also passing an int or None which then explodes at .lower() or the membership check.

Common situations: Copy-pasting index algorithm names from other engines (FAISS 'IVF', Milvus 'IVF_FLAT', pgvector 'ivfflat/hnsw'); config drift between environments; YAML quoting issues that turn the value into a bool or number.

Related errors


AI-assisted analysis of mem0ai/mem0@001c235229 (2026-08-15). Data as JSON: /api/errors/fcce6d80e8dd27ad. Report an issue: GitHub.