microsoft/semantic-kernel · error · VectorStoreInitializationException
Distance function {field.distance_function} is not supported
Error message
Distance function {field.distance_function} is not supported. Supported distance functions are: {list(DISTANCE_FUNCTION_MAP.keys())} What it means
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.
Source
Thrown at python/semantic_kernel/connectors/mongodb.py:102
env_prefix: ClassVar[str] = "MONGODB_ATLAS_"
connection_string: SecretStr
database_name: str = DEFAULT_DB_NAME
index_name: str = DEFAULT_SEARCH_INDEX_NAME
def _create_vector_field(field: VectorStoreField) -> dict:
"""Create a vector field.
Args:
field (VectorStoreRecordVectorField): The vector field.
Returns:
dict: The vector field.
"""
if field.distance_function not in DISTANCE_FUNCTION_MAP:
raise VectorStoreInitializationException(
f"Distance function {field.distance_function} is not supported. "
f"Supported distance functions are: {list(DISTANCE_FUNCTION_MAP.keys())}"
)
return {
"type": "vector",
"numDimensions": field.dimensions,
"path": field.storage_name or field.name,
"similarity": DISTANCE_FUNCTION_MAP[field.distance_function],
}
def _create_index_definitions(
record_definition: VectorStoreCollectionDefinition, index_name: str
) -> list[SearchIndexModel]:
"""Create the index definitions."""
indexes = []
if record_definition.vector_fields:
vector_fields = [_create_vector_field(field) for field in record_definition.vector_fields]View on GitHub (pinned to c028a0c7dc)
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.
Example fix
// before vector=VectorStoreRecordVectorField(distance_function=DistanceFunction.MANHATTAN) // after vector=VectorStoreRecordVectorField(distance_function=DistanceFunction.COSINE_SIMILARITY)
Defensive patterns
Strategy: validation
Validate before calling
from semantic_kernel.connectors.mongodb import DISTANCE_FUNCTION_MAP
from semantic_kernel.data.vector import DistanceFunction
if field.distance_function not in DISTANCE_FUNCTION_MAP:
raise ValueError(f'use one of {list(DISTANCE_FUNCTION_MAP)}')
_create_vector_field(field) Type guard
from semantic_kernel.connectors.mongodb import DISTANCE_FUNCTION_MAP
def is_supported_distance(fn) -> bool:
return fn in DISTANCE_FUNCTION_MAP Try / catch
from semantic_kernel.exceptions import VectorStoreInitializationException
try:
_create_vector_field(field)
except VectorStoreInitializationException as e:
field.distance_function = DistanceFunction.COSINE_SIMILARITY
_create_vector_field(field) Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Failed to create MongoDB Atlas settings.
- Failed to create Azure OpenAI settings: {exc}
- Please provide an Azure OpenAI endpoint
- Please provide an Azure OpenAI deployment name
- Failed to create Anthropic settings.
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/090e9d7c9e89046e.
Report an issue: GitHub.