mem0ai/mem0 · error · ValueError

Invalid index_to_id key type: {type(key)}, expected int

Error message

Invalid index_to_id key type: {type(key)}, expected int

What it means

Raised while validating index_to_id entries: a key (FAISS row number) is not an int. Rows must be integers because they are used to reconstruct/delete vectors by position. ValueError raised at load.

Source

Thrown at mem0/vector_stores/faiss.py:114

    docstore, index_to_id = data

    if not isinstance(docstore, dict):
        raise ValueError("Invalid docstore format: docstore must be a dict")

    if not isinstance(index_to_id, dict):
        raise ValueError("Invalid docstore format: index_to_id must be a dict")

    # Validate docstore entries
    for key, value in docstore.items():
        if not isinstance(key, str):
            raise ValueError(f"Invalid docstore key type: {type(key)}, expected str")
        if not isinstance(value, dict):
            raise ValueError(f"Invalid docstore value type: {type(value)}, expected dict")

    # Validate index_to_id entries
    for key, value in index_to_id.items():
        if not isinstance(key, int):
            raise ValueError(f"Invalid index_to_id key type: {type(key)}, expected int")
        if not isinstance(value, str):
            raise ValueError(f"Invalid index_to_id value type: {type(value)}, expected str")

    return docstore, index_to_id


class OutputData(BaseModel):
    id: Optional[str]  # memory id
    score: Optional[float]  # distance
    payload: Optional[Dict]  # metadata


class FAISS(VectorStoreBase):
    def __init__(
        self,
        collection_name: str,
        path: Optional[str] = None,
        distance_strategy: str = "euclidean",

View on GitHub (pinned to 001c235229)

Solutions

  1. Re-serialize with integer keys: {int(k): v for k, v in index_to_id.items()}
  2. Regenerate the whole store through mem0 to rebuild a consistent index_to_id
  3. Avoid manual edits to the persisted files

Example fix

# before
index_to_id = data[1]  # keys are '0','1',... after JSON round-trip

# after
index_to_id = {int(k): v for k, v in data[1].items()}
data[1] = index_to_id
Defensive patterns

Strategy: validation

Validate before calling

data[1] = {int(k): v for k, v in data[1].items()}  # repair string keys from JSON round-trips

Type guard

def index_to_id_keys_are_int(x) -> bool:
    return isinstance(x, dict) and all(isinstance(k, int) for k in x)

Prevention

When it happens

Trigger: A persisted index_to_id dict whose keys are strings — the classic result of round-tripping the tuple through JSON, where integer keys are coerced to strings.

Common situations: Externally editing or re-serializing the JSON docstore; a migration script that did json.load/json.dump without int() key conversion.

Related errors


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