Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:CrewAIInc CrewAI MongoDB Vector Utils

From Leeroopedia
Knowledge Sources
Domains Tools, MongoDB, Vector_Search
Last Updated 2026-02-11 00:00 GMT

Overview

Utility functions for creating and managing MongoDB Atlas Vector Search indexes used by the MongoDB vector search tool.

Description

This module provides utility functions that abstract the complexity of MongoDB Atlas Vector Search index creation and management. The _vector_search_index_definition() function constructs index definitions with vector field specifications including dimensions, path, and similarity metric, plus optional filter fields for pre-filtering in $vectorSearch queries. The main create_vector_search_index() function creates SearchIndexModel instances with the generated definition, handles collection creation if the collection does not yet exist, and optionally waits for index readiness using a configurable timeout. The helper _is_index_ready() polls the search index status list until the READY state is reached. The generic _wait_for_predicate() function implements timeout-based polling with configurable interval, raising TimeoutError if the predicate is not satisfied within the specified duration.

Usage

Use these utilities when setting up MongoDB Atlas Vector Search infrastructure for the MongoDB vector search tool, particularly when creating new vector indexes and ensuring they are ready before performing search operations.

Code Reference

Source Location

  • Repository: CrewAI
  • File: lib/crewai-tools/src/crewai_tools/tools/mongodb_vector_search_tool/utils.py
  • Lines: 1-122

Signature

def _vector_search_index_definition(
    dimensions: int,
    path: str,
    similarity: str,
    filters: list[str] | None = None,
    **kwargs: Any,
) -> dict[str, Any]: ...

def create_vector_search_index(
    collection: Collection,
    index_name: str,
    dimensions: int,
    path: str,
    similarity: str,
    filters: list[str] | None = None,
    *,
    wait_until_complete: float | None = None,
    **kwargs: Any,
) -> None: ...

def _is_index_ready(collection: Collection, index_name: str) -> bool: ...

def _wait_for_predicate(
    predicate: Callable, err: str, timeout: float = 120, interval: float = 0.5
) -> None: ...

Import

from crewai_tools.tools.mongodb_vector_search_tool.utils import create_vector_search_index

I/O Contract

Inputs

Name Type Required Description
collection Collection Yes PyMongo Collection object for the target MongoDB collection
index_name str Yes Name to assign to the vector search index
dimensions int Yes Number of dimensions in the embedding vectors
path str Yes Field path containing the vector embeddings
similarity str Yes Similarity metric to use (e.g., "cosine", "euclidean", "dotProduct")
filters list[str] or None No Field paths to index for pre-filtering in $vectorSearch queries
wait_until_complete float or None No Seconds to wait for index readiness; None means do not wait

Outputs

Name Type Description
create_vector_search_index() returns None Creates the index as a side effect; raises TimeoutError if wait times out
_is_index_ready() returns bool True if the specified index has READY status

Usage Examples

Create Vector Search Index

from pymongo import MongoClient
from crewai_tools.tools.mongodb_vector_search_tool.utils import create_vector_search_index

client = MongoClient("mongodb+srv://...")
collection = client["mydb"]["documents"]

create_vector_search_index(
    collection=collection,
    index_name="vector_index",
    dimensions=1536,
    path="embedding",
    similarity="cosine",
    filters=["category", "date"],
    wait_until_complete=120.0,
)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment