{"record":{"id":"090e9d7c9e89046e","repo":"microsoft/semantic-kernel","slug":"distance-function-field-distance-function-is-not-090e9d","errorCode":null,"errorMessage":"Distance function {field.distance_function} is not supported. Supported distance functions are: {list(DISTANCE_FUNCTION_MAP.keys())}","messagePattern":"Distance function (.+?) is not supported\\. Supported distance functions are: (.+?)","errorType":"exception","errorClass":"VectorStoreInitializationException","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/mongodb.py","lineNumber":102,"sourceCode":"\n    env_prefix: ClassVar[str] = \"MONGODB_ATLAS_\"\n\n    connection_string: SecretStr\n    database_name: str = DEFAULT_DB_NAME\n    index_name: str = DEFAULT_SEARCH_INDEX_NAME\n\n\ndef _create_vector_field(field: VectorStoreField) -> dict:\n    \"\"\"Create a vector field.\n\n    Args:\n        field (VectorStoreRecordVectorField): The vector field.\n\n    Returns:\n        dict: The vector field.\n    \"\"\"\n    if field.distance_function not in DISTANCE_FUNCTION_MAP:\n        raise VectorStoreInitializationException(\n            f\"Distance function {field.distance_function} is not supported. \"\n            f\"Supported distance functions are: {list(DISTANCE_FUNCTION_MAP.keys())}\"\n        )\n    return {\n        \"type\": \"vector\",\n        \"numDimensions\": field.dimensions,\n        \"path\": field.storage_name or field.name,\n        \"similarity\": DISTANCE_FUNCTION_MAP[field.distance_function],\n    }\n\n\ndef _create_index_definitions(\n    record_definition: VectorStoreCollectionDefinition, index_name: str\n) -> list[SearchIndexModel]:\n    \"\"\"Create the index definitions.\"\"\"\n    indexes = []\n    if record_definition.vector_fields:\n        vector_fields = [_create_vector_field(field) for field in record_definition.vector_fields]","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/mongodb.py#L84-L120","documentation":"Raised by _create_vector_field (VectorStoreInitializationException) when a vector field's distance_function is not one MongoDB Atlas supports. The supported set is fixed in DISTANCE_FUNCTION_MAP: EUCLIDEAN_DISTANCE, COSINE_SIMILARITY, DOT_PROD, and DEFAULT (mapped to euclidean). Any other DistanceFunction value (or a value added in a newer SK release) is rejected while building the Atlas vector search index definition.","triggerScenarios":"Annotating a VectorStoreRecordVectorField with distance_function=DistanceFunction.MANHATTAN (or HAMMING/COSINE_DISTANCE/etc.), or a None/unset distance function, then creating/registering the MongoDB collection which calls _create_index_definitions -> _create_vector_field.","commonSituations":"Copying a model from another connector that uses a distance function MongoDB doesn't map; version skew where the DistanceFunction enum grew but this connector map didn't; explicitly setting an unsupported metric.","solutions":["Use one of the supported distance functions: COSINE_SIMILARITY, DOT_PROD, EUCLIDEAN_DISTANCE, or DEFAULT.","Remove an explicit distance_function so the default applies (DEFAULT -> euclidean).","Upgrade semantic-kernel so the connector map and your enum version align."],"exampleFix":"// before\nvector=VectorStoreRecordVectorField(distance_function=DistanceFunction.MANHATTAN)\n// after\nvector=VectorStoreRecordVectorField(distance_function=DistanceFunction.COSINE_SIMILARITY)","handlingStrategy":"validation","validationCode":"from semantic_kernel.connectors.mongodb import DISTANCE_FUNCTION_MAP\nfrom semantic_kernel.data.vector import DistanceFunction\n\nif field.distance_function not in DISTANCE_FUNCTION_MAP:\n    raise ValueError(f'use one of {list(DISTANCE_FUNCTION_MAP)}')\n_create_vector_field(field)","typeGuard":"from semantic_kernel.connectors.mongodb import DISTANCE_FUNCTION_MAP\n\ndef is_supported_distance(fn) -> bool:\n    return fn in DISTANCE_FUNCTION_MAP","tryCatchPattern":"from semantic_kernel.exceptions import VectorStoreInitializationException\ntry:\n    _create_vector_field(field)\nexcept VectorStoreInitializationException as e:\n    field.distance_function = DistanceFunction.COSINE_SIMILARITY\n    _create_vector_field(field)","preventionTips":["Restrict vector field annotations to COSINE_SIMILARITY, DOT_PROD, EUCLIDEAN_DISTANCE, or DEFAULT.","Keep semantic-kernel version aligned between model definitions and the mongodb connector.","Add a startup assertion that all vector fields use a supported distance function."],"tags":["mongodb","vector-store","initialization","distance-function","config"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}