mem0ai/mem0 · error · ValueError

Invalid distance_strategy. Must be one of: 'euclidean', 'inn

Error message

Invalid distance_strategy. Must be one of: 'euclidean', 'inner_product', 'cosine'

What it means

Raised by the Faiss vector store config when distance_strategy is set to a value outside the supported set. FAISS indexes in mem0 support exactly 'euclidean' (L2), 'inner_product', and 'cosine'; any other string fails validation at config construction, before any index is built.

Source

Thrown at mem0/configs/vector_stores/faiss.py:22


class FAISSConfig(BaseModel):
    collection_name: str = Field("mem0", description="Default name for the collection")
    path: Optional[str] = Field(None, description="Path to store FAISS index and metadata")
    distance_strategy: str = Field(
        "euclidean", description="Distance strategy to use. Options: 'euclidean', 'inner_product', 'cosine'"
    )
    normalize_L2: bool = Field(
        False, description="Whether to normalize L2 vectors (only applicable for euclidean distance)"
    )
    embedding_model_dims: int = Field(1536, description="Dimension of the embedding vector")

    @model_validator(mode="before")
    @classmethod
    def validate_distance_strategy(cls, values: Dict[str, Any]) -> Dict[str, Any]:
        distance_strategy = values.get("distance_strategy")
        if distance_strategy and distance_strategy not in ["euclidean", "inner_product", "cosine"]:
            raise ValueError("Invalid distance_strategy. Must be one of: 'euclidean', 'inner_product', 'cosine'")
        return values

    @model_validator(mode="before")
    @classmethod
    def validate_extra_fields(cls, values: Dict[str, Any]) -> Dict[str, Any]:
        allowed_fields = set(cls.model_fields.keys())
        input_fields = set(values.keys())
        extra_fields = input_fields - allowed_fields
        if extra_fields:
            raise ValueError(
                f"Extra fields not allowed: {', '.join(extra_fields)}. Please input only the following fields: {', '.join(allowed_fields)}"
            )
        return values

    model_config = ConfigDict(arbitrary_types_allowed=True)

View on GitHub (pinned to 001c235229)

Solutions

  1. Use one of the exact lowercase values: 'euclidean', 'inner_product', or 'cosine'
  2. For dot-product similarity use 'inner_product'; for L2 use 'euclidean' — not the FAISS abbreviations
  3. Omit distance_strategy to keep the 'euclidean' default if unsure
  4. Keep the mapping from your metric vocabulary to mem0's in one constant in your codebase

Example fix

# before
FaissConfig(distance_strategy="L2")

# after
FaissConfig(distance_strategy="euclidean")
Defensive patterns

Strategy: validation

Validate before calling

FAISS_STRATEGIES = {"euclidean", "inner_product", "cosine"}
def validate_faiss_strategy(cfg: dict) -> None:
    s = cfg.get("distance_strategy")
    if s is not None and s not in FAISS_STRATEGIES:
        raise RuntimeError(f"distance_strategy must be one of {sorted(FAISS_STRATEGIES)}, got {s!r}")

Type guard

def faiss_strategy_valid(cfg: dict) -> bool:
    s = cfg.get("distance_strategy")
    return s is None or s in {"euclidean", "inner_product", "cosine"}

Try / catch

from pydantic import ValidationError
try:
    FaissConfig(**cfg)
except ValidationError as e:
    if "Invalid distance_strategy" in str(e):
        # map to euclidean/inner_product/cosine, then retry
        ...

Prevention

When it happens

Trigger: Passing distance_strategy values like 'l2', 'dot', 'IP', 'manhattan', or 'EUCLIDEAN' (case-sensitive check) to FaissConfig. Empty/None is allowed and falls back to the default.

Common situations: Using FAISS-native or other-library metric names ('IP', 'L2', 'cs') instead of mem0's names; uppercase variants copied from docs of a different library; renaming of the option's accepted values across mem0 versions.

Related errors


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