Skip to content

Python: Fix Cosmos DB MongoDB vector index kind mapping (#14104) - #14249

Open
MeiSiristhebest wants to merge 2 commits into
microsoft:mainfrom
MeiSiristhebest:fix/python-cosmos-mongo-index-kind
Open

Python: Fix Cosmos DB MongoDB vector index kind mapping (#14104)#14249
MeiSiristhebest wants to merge 2 commits into
microsoft:mainfrom
MeiSiristhebest:fix/python-cosmos-mongo-index-kind

Conversation

@MeiSiristhebest

Copy link
Copy Markdown

Motivation and Context

Fixes #14104.

In python/semantic_kernel/connectors/azure_cosmos_db.py, CosmosMongoCollection._get_index_definitions assigned index_kind from DISTANCE_FUNCTION_MAP_MONGODB[field.distance_function] instead of INDEX_KIND_MAP_MONGODB[field.index_kind].

This caused three issues:

  1. cosmosSearchOptions["kind"] was sent as a similarity string (e.g. "COS") rather than a valid vector index kind ("vector-hnsw", "vector-ivf", "vector-diskann"), leading to index creation failure on Azure Cosmos DB for MongoDB vCore.
  2. The match index_kind: block never matched any vector-* pattern, so HNSW/IVF tuning parameters (m, efConstruction, numList) were silently dropped.
  3. INDEX_KIND_MAP_MONGODB was validated for membership but its mapped value was never used.

Description

Updated CosmosMongoCollection._get_index_definitions to correctly map field.index_kind via INDEX_KIND_MAP_MONGODB[field.index_kind].

Also updated the unit test in test_azure_cosmos_db_mongodb_collection.py to assert that cosmosSearchOptions["kind"] == "vector-hnsw" and cosmosSearchOptions["similarity"] == "COS".

Contribution Checklist

Copilot AI review requested due to automatic review settings August 2, 2026 14:03
@MeiSiristhebest
MeiSiristhebest requested a review from a team as a code owner August 2, 2026 14:03

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

This PR fixes a bug in the Python Azure Cosmos DB for MongoDB connector where the vector index kind was incorrectly populated with the distance-function similarity code, causing invalid index definitions and preventing index-kind-specific tuning options from being applied.

Changes:

  • Correctly map field.index_kind via INDEX_KIND_MAP_MONGODB when building MongoDB vector index definitions.
  • Update the MongoDB collection unit test to assert the expected cosmosSearchOptions["kind"] and ["similarity"] values.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
python/semantic_kernel/connectors/azure_cosmos_db.py Fixes vector index kind mapping to use the index-kind map (enabling correct vector-* kinds and match-based option handling).
python/tests/unit/connectors/memory/azure_cosmos_db/test_azure_cosmos_db_mongodb_collection.py Updates assertions to validate kind="vector-hnsw" and similarity="COS".

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

Comment on lines +101 to 103
assert command_args["indexes"][1]["cosmosSearchOptions"]["kind"] == "vector-hnsw"
assert command_args["indexes"][1]["cosmosSearchOptions"]["similarity"] == "COS"
assert command_args["indexes"][1]["cosmosSearchOptions"]["dimensions"] == 5

@github-actions github-actions Bot 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.

Automated Code Review

Reviewers: 5 | Confidence: 86% | Result: All clear

Reviewed: Correctness, Security Reliability, Test Coverage, Failure Modes, Design Approach


Automated review by MeiSiristhebest's agents

@MeiSiristhebest
MeiSiristhebest force-pushed the fix/python-cosmos-mongo-index-kind branch from 6f7b86c to 02fd264 Compare August 2, 2026 18:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

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

2 participants