microsoft/semantic-kernel · error · VectorSearchExecutionException

Failed to search the collection.

Error message

Failed to search the collection.

What it means

CosmosMongoCollection._inner_vector_search wraps the MongoDB aggregation pipeline in a try/except. Any exception from collection.aggregate (network, auth, server-side aggregation error, malformed pipeline) is caught and re-raised as VectorSearchExecutionException with the generic message. The original exception is chained via 'from exc'.

Source

Thrown at python/semantic_kernel/connectors/azure_cosmos_db.py:469

        }
        if filter := self._build_filter(options.filter):  # type: ignore
            vector_search_query["filter"] = filter if isinstance(filter, dict) else {"$and": filter}

        projection_query: dict[str, int | dict] = {
            field: 1
            for field in self.definition.get_names(
                include_vector_fields=options.include_vectors,
                include_key_field=False,  # _id is always included
            )
        }
        projection_query[MONGODB_SCORE_FIELD] = {"$meta": "searchScore"}
        try:
            raw_results = await collection.aggregate([
                {"$search": {"cosmosSearch": vector_search_query}},
                {"$project": projection_query},
            ])
        except Exception as exc:
            raise VectorSearchExecutionException("Failed to search the collection.") from exc
        return KernelSearchResults(
            results=self._get_vector_search_results_from_results(raw_results, options),
            total_count=None,  # no way to get a count before looping through the result cursor
        )


# region: Mongo Store


@release_candidate
class CosmosMongoStore(MongoDBAtlasStore):
    """Azure Cosmos DB for MongoDB store."""

    def __init__(
        self,
        connection_string: str | None = None,
        database_name: str | None = None,
        mongo_client: AsyncMongoClient | None = None,

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Inspect the chained __cause__ for the real server error and HTTP status.
  2. Verify the vector index exists and is built (vector-ivf/hnsw/diskann) before querying.
  3. Check connectivity, credentials, and Cosmos DB throughput/RU provisioning.
  4. Ensure the vector field path and dimensions in the query match the index definition.
Defensive patterns

Strategy: retry

Try / catch

try:
    results = await collection.search(...)
except VectorSearchExecutionException as e:
    cause = e.__cause__
    # classify: 429/network -> retry with backoff; auth -> refresh; bad pipeline -> fix filter

Prevention

When it happens

Trigger: Raised in CosmosMongoCollection._inner_vector_search when collection.aggregate([...]) raises. Common causes: expired credentials, throttling (429), the cosmosSearch index not yet built, a filter pipeline that the server rejects, network outage, or an indexing/policy mismatch detected server-side.

Common situations: Running a search immediately after creating a collection before the vector index is ready. RU exhaustion under load. Expired Entra ID tokens. Network blips. A bug in _build_filter producing an invalid $match that the server rejects.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/105084344f2077de. Report an issue: GitHub.