Skip to content

Python: Fix Cosmos DB for MongoDB vector index kind#14105

Open
EsraaKamel11 wants to merge 1 commit into
microsoft:mainfrom
EsraaKamel11:EsraaKamel11-cosmos-mongodb-index-kind
Open

Python: Fix Cosmos DB for MongoDB vector index kind#14105
EsraaKamel11 wants to merge 1 commit into
microsoft:mainfrom
EsraaKamel11:EsraaKamel11-cosmos-mongodb-index-kind

Conversation

@EsraaKamel11

Copy link
Copy Markdown

Motivation and Context

CosmosMongoCollection._get_index_definitions populated cosmosSearchOptions["kind"] from the distance-function map
(a similarity code like "COS") instead of the index-kind map. As a result the created vector index used an invalid
kind, kind equalled similarity, and all HNSW/IVF/DiskANN tuning options (m, efConstruction, numList,
maxDegree, lBuild) were silently dropped because the match index_kind block could never match a vector-* case.

Cosmos DB for MongoDB vCore requires cosmosSearchOptions.kind ∈ {vector-ivf, vector-hnsw, vector-diskann}, so index
creation also fails against a live account.

Fixes #14104

Description

One-line fix: use INDEX_KIND_MAP_MONGODB[field.index_kind] for the index kind, keeping the distance-function map
for similarity (mirrors the NoSQL path, which already does this correctly). INDEX_KIND_MAP_MONGODB's value was
previously unused.

  • Before: kind="COS", similarity="COS", tuning options dropped
  • After: kind="vector-hnsw", similarity="COS", tuning options forwarded

Tests: updated the existing index test (which asserted the buggy "COS" value) and added a test verifying HNSW tuning
options (m, efConstruction) reach cosmosSearchOptions.

Contribution Checklist

CosmosMongoCollection._get_index_definitions set cosmosSearchOptions["kind"]
from DISTANCE_FUNCTION_MAP_MONGODB (a similarity code, e.g. "COS") instead of
INDEX_KIND_MAP_MONGODB (the index kind, e.g. "vector-hnsw"). As a result the
created vector index used an invalid kind, "kind" equalled "similarity", and the
HNSW/IVF/DiskANN tuning options (m, efConstruction, numList, maxDegree, lBuild)
were silently dropped because the `match index_kind` block could never match.

Use INDEX_KIND_MAP_MONGODB[field.index_kind] for the index kind and keep the
distance-function map for "similarity". Update the existing index test (which
asserted the buggy "COS" value) and add a test covering the tuning options.
Copilot AI review requested due to automatic review settings June 21, 2026 09:46
@EsraaKamel11 EsraaKamel11 requested a review from a team as a code owner June 21, 2026 09:46
@moonbox3 moonbox3 added the python Pull requests for the Python Semantic Kernel label Jun 21, 2026
@github-actions github-actions Bot changed the title Python: Fix Cosmos DB for MongoDB vector index kind Python: Python: Fix Cosmos DB for MongoDB vector index kind Jun 21, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes Azure Cosmos DB for MongoDB vector index creation by correctly populating cosmosSearchOptions["kind"] from the index-kind mapping (e.g., vector-hnsw) while keeping cosmosSearchOptions["similarity"] sourced from the distance-function mapping (e.g., COS). This aligns the MongoDB vCore path with the existing NoSQL path and ensures vector index tuning options are applied.

Changes:

  • Corrected _get_index_definitions to use INDEX_KIND_MAP_MONGODB[field.index_kind] for cosmosSearchOptions["kind"].
  • Updated the existing unit test to assert the correct kind/similarity values.
  • Added a unit test verifying HNSW tuning options (m, efConstruction) are forwarded into cosmosSearchOptions.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
python/semantic_kernel/connectors/azure_cosmos_db.py Fixes MongoDB vector index kind mapping so index creation uses valid vector-* kinds and tuning options apply.
python/tests/unit/connectors/memory/azure_cosmos_db/test_azure_cosmos_db_mongodb_collection.py Updates/extends tests to validate correct kind vs similarity and verify HNSW tuning options propagation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@EsraaKamel11

EsraaKamel11 commented Jun 21, 2026

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@EsraaKamel11 EsraaKamel11 changed the title Python: Python: Fix Cosmos DB for MongoDB vector index kind Python: Fix Cosmos DB for MongoDB vector index kind Jun 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Pull requests for the Python Semantic Kernel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: Bug: Cosmos DB for MongoDB vector index uses the similarity code as the index kind

3 participants