{"record":{"id":"7544ef8fad745753","repo":"mem0ai/mem0","slug":"unsupported-metric-type-distance","errorCode":null,"errorMessage":"Unsupported metric_type: {distance}","messagePattern":"Unsupported metric_type: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"mem0/vector_stores/baidu.py","lineNumber":133,"sourceCode":"            vector_size (int): Dimension of the vector.\n            distance (str): Metric type for similarity search.\n        \"\"\"\n        # Check if table already exists\n        try:\n            tables = self._database.list_table()\n            table_exists = any(table.table_name == name for table in tables)\n            if table_exists:\n                logger.info(f\"Table {name} already exists. Skipping creation.\")\n                self._table = self._database.describe_table(name)\n                return\n\n            # Convert distance string to MetricType enum\n            metric_type = None\n            for k, v in MetricType.__members__.items():\n                if k == distance:\n                    metric_type = v\n            if metric_type is None:\n                raise ValueError(f\"Unsupported metric_type: {distance}\")\n\n            # Define table schema\n            fields = [\n                Field(\n                    \"id\", FieldType.STRING, primary_key=True, partition_key=True, auto_increment=False, not_null=True\n                ),\n                Field(\"vector\", FieldType.FLOAT_VECTOR, dimension=vector_size),\n                Field(\"metadata\", FieldType.JSON),\n            ]\n\n            # Create vector index\n            indexes = [\n                VectorIndex(\n                    index_name=\"vector_idx\",\n                    index_type=IndexType.HNSW,\n                    field=\"vector\",\n                    metric_type=metric_type,\n                    params=HNSWParams(m=16, efconstruction=200),","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0/vector_stores/baidu.py#L115-L151","documentation":"BaiduDB.create_col raises ValueError when the distance string does not exactly match any pymochow MetricType enum member NAME. The loop compares the raw string against MetricType.__members__ keys (e.g. 'COSINE', 'L2', 'IP'), so it is case-sensitive: 'cosine' fails while 'COSINE' passes, and unsupported metrics like 'euclidean' fail outright.","triggerScenarios":"Calling create_col (or constructing BaiduDB with a new collection name) with distance='cosine' (lowercase), 'euclidean', or any string that is not an exact MetricType member name. Note many other mem0 vector stores accept lowercase distance strings, so config copied between providers often trips this.","commonSituations":"Reusing a distance='cosine' config written for Qdrant/Chroma against the Baidu backend; upgrading pymochow where MetricType member names changed; passing a distance value sourced from user input or a shared config file.","solutions":["Pass the exact enum member name in uppercase, e.g. distance='COSINE' (check pymochow's MetricType for the accepted names such as COSINE/L2/IP).","If the string comes from shared config, uppercase/normalize it before it reaches BaiduDB: distance.strip().upper().","Verify available names at runtime: python -c \"from pymochow.model.table import MetricType; print(list(MetricType.__members__))\"."],"exampleFix":"# before\nBaiduDB(..., distance_metric or config with distance=\"cosine\")  # ValueError\n\n# after\nconfig = {\"distance\": \"COSINE\", ...}  # exact MetricType member name","handlingStrategy":"validation","validationCode":"from pymochow.model.table import MetricType\n\nVALID_METRICS = set(MetricType.__members__)\n\ndef normalize_distance(distance: str) -> str:\n    d = distance.strip().upper()\n    if d not in VALID_METRICS:\n        raise ValueError(f\"distance must be one of {sorted(VALID_METRICS)}, got {distance!r}\")\n    return d\n\ndistance = normalize_distance(cfg[\"distance\"])  # before creating BaiduDB","typeGuard":"def is_valid_baidu_metric(distance: str) -> bool:\n    return isinstance(distance, str) and distance.strip().upper() in MetricType.__members__","tryCatchPattern":null,"preventionTips":["Keep provider-specific distance strings in the provider's own config, not in shared config.","Uppercase distance values at the config boundary.","Log the valid MetricType names in setup errors to make misconfiguration self-explanatory."],"tags":["baidu","validation","vector-store","config"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}