{"record":{"id":"07d8bcd92e49d3cb","repo":"microsoft/semantic-kernel","slug":"distance-function-field-distance-function-is-n-07d8bc","errorCode":null,"errorMessage":"Distance function '{field.distance_function}' is not supported by Azure Cosmos DB for MongoDB.","messagePattern":"Distance function '(.+?)' is not supported by Azure Cosmos DB for MongoDB\\.","errorType":"exception","errorClass":"VectorStoreModelException","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/azure_cosmos_db.py","lineNumber":398,"sourceCode":"        await self._get_database().command(command=self._get_index_definitions(**kwargs))\n\n    def _get_index_definitions(self, **kwargs: Any) -> dict[str, Any]:\n        \"\"\"Creates index definitions for the collection.\"\"\"\n        indexes = [\n            {\n                \"name\": f\"{field.storage_name or field.name}_\",\n                FieldTypes.KEY: {field.storage_name or field.name: 1},\n            }\n            for field in self.definition.data_fields\n            if field.is_indexed or field.is_full_text_indexed\n        ]\n        for field in self.definition.vector_fields:\n            if field.index_kind not in INDEX_KIND_MAP_MONGODB:\n                raise VectorStoreModelException(\n                    f\"Index kind '{field.index_kind}' is not supported by Azure Cosmos DB for MongoDB.\"\n                )\n            if field.distance_function not in DISTANCE_FUNCTION_MAP_MONGODB:\n                raise VectorStoreModelException(\n                    f\"Distance function '{field.distance_function}' is not supported by Azure Cosmos DB for MongoDB.\"\n                )\n            index_name = f\"{field.storage_name or field.name}_\"\n            index_kind = DISTANCE_FUNCTION_MAP_MONGODB[field.distance_function]\n            index: dict[str, Any] = {\n                \"name\": index_name,\n                FieldTypes.KEY: {field.storage_name or field.name: \"cosmosSearch\"},\n                \"cosmosSearchOptions\": {\n                    \"kind\": index_kind,\n                    \"similarity\": DISTANCE_FUNCTION_MAP_MONGODB[field.distance_function],\n                    \"dimensions\": field.dimensions,\n                },\n            }\n            match index_kind:\n                case \"vector-diskann\":\n                    if \"maxDegree\" in kwargs:\n                        index[\"cosmosSearchOptions\"][\"maxDegree\"] = kwargs[\"maxDegree\"]\n                    if \"lBuild\" in kwargs:","sourceCodeStart":380,"sourceCodeEnd":416,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/azure_cosmos_db.py#L380-L416","documentation":"The MongoDB index builder rejects vector fields whose distance function is not in the MongoDB map (COS, IP, L2 / cosine, dot product, euclidean, default). Raised at index-definition construction time, before any write to Cosmos. It is the MongoDB analog of error 1240.","triggerScenarios":"Raised in CosmosMongoCollection._get_index_definitions when field.distance_function not in DISTANCE_FUNCTION_MAP_MONGODB. Fires when a vector field uses a DistanceFunction value outside the four supported ones, e.g. MANHATTAN or a custom enum.","commonSituations":"Sharing one data-model definition between a NoSQL and a MongoDB collection where the distance function differs in support. Migrating from another store whose distance-function vocabulary is wider. Copying sample code that used an unsupported metric.","solutions":["Use DistanceFunction.COSINE_SIMILARITY, DOT_PROD, EUCLIDEAN_DISTANCE, or DEFAULT.","If the metric truly is unsupported, switch to cosine (most embeddings) or another connector.","Maintain per-store definitions rather than one shared model when metrics differ."],"exampleFix":"// before\nVectorStoreRecordVectorField(name=\"embedding\", dimensions=1536, distance_function=DistanceFunction.MANHATTAN)\n// after\nVectorStoreRecordVectorField(name=\"embedding\", dimensions=1536, distance_function=DistanceFunction.COSINE_SIMILARITY)","handlingStrategy":"validation","validationCode":"from semantic_kernel.connectors.azure_cosmos_db import DISTANCE_FUNCTION_MAP_MONGODB\n\nbad = [f for f in definition.vector_fields if f.distance_function not in DISTANCE_FUNCTION_MAP_MONGODB]\nif bad:\n    raise ValueError(f\"Unsupported distance functions: {[f.name for f in bad]}\")\n","typeGuard":"def is_supported_mongodb_distance(fn: DistanceFunction) -> bool:\n    return fn in DISTANCE_FUNCTION_MAP_MONGODB\n","tryCatchPattern":null,"preventionTips":["Maintain per-store data model definitions when distance functions differ.","Review vector field metadata during code review.","Centralize a 'supported metrics' reference table."],"tags":["azure-cosmos-db","mongodb","vector-store","data-model"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}