| title | Quickstart - Vector Search with Python | |||
|---|---|---|---|---|
| description | Learn how to use vector search in Azure DocumentDB with Python. Store and query vector data efficiently in your applications. | |||
| author | rotabor | |||
| ms.author | rotabor | |||
| ms.reviewer | khelanmodi | |||
| ms.devlang | python | |||
| ms.topic | quickstart-sdk | |||
| ms.date | 02/20/2026 | |||
| ai-usage | ai-assisted | |||
| ms.custom |
|
Use vector search in Azure DocumentDB with the Python client library. Store and query vector data efficiently.
This quickstart uses a sample hotel dataset in a JSON file with pre-calculated vectors from the text-embedding-3-small model. The dataset includes hotel names, locations, descriptions, and vector embeddings.
Find the sample code on GitHub.
[!INCLUDEPrerequisites - Vector Search Quickstart]
Note
The infrastructure deploys Azure OpenAI with the Standard SKU (not GlobalStandard). You can customize the SKU and model parameters using azd env set before deployment. See the sample's README for available parameters.
- Python 3.9 or greater
-
Create a new data directory for the hotels data file:
mkdir data
-
Copy the
Hotels_Vector.jsonraw data file with vectors to yourdatadirectory.
-
Create a new directory for your project and open it in Visual Studio Code:
mkdir vector-search-quickstart code vector-search-quickstart
-
In the terminal, create and activate a virtual environment:
For Windows:
python -m venv venv venv\\Scripts\\activate
For macOS/Linux:
python -m venv venv source venv/bin/activate -
Install the required packages:
pip install pymongo azure-identity openai python-dotenv
pymongo: MongoDB driver for Pythonazure-identity: Azure Identity library for passwordless authenticationopenai: OpenAI client library to create vectorspython-dotenv: Environment variable management from .env files
-
Create a
.envfile for environment variables invector-search-quickstart:# Identity for local developer authentication with Azure CLI AZURE_TOKEN_CREDENTIALS=AzureCliCredential # Azure OpenAI configuration AZURE_OPENAI_EMBEDDING_ENDPOINT= AZURE_OPENAI_EMBEDDING_MODEL=text-embedding-3-small AZURE_OPENAI_EMBEDDING_API_VERSION=2023-05-15 # Azure DocumentDB configuration MONGO_CLUSTER_NAME= # Data Configuration (defaults should work) DATA_FILE_WITH_VECTORS=../data/Hotels_Vector.json EMBEDDED_FIELD=DescriptionVector EMBEDDING_DIMENSIONS=1536 EMBEDDING_SIZE_BATCH=16 LOAD_SIZE_BATCH=50
For the passwordless authentication used in this article, replace the placeholder values in the
.envfile with your own information:AZURE_OPENAI_EMBEDDING_ENDPOINT: Your Azure OpenAI resource endpoint URLMONGO_CLUSTER_NAME: Your Azure DocumentDB resource name
You should always prefer passwordless authentication, but it will require additional setup. For more information on setting up managed identity and the full range of your authentication options, see Authenticate Python apps to Azure services by using the Azure SDK for Python.
Continue the project by creating code files for vector search. When you are done, the project structure should look like this:
├── data/
│ ├── Hotels.json # Source hotel data (without vectors)
│ └── Hotels_Vector.json # Hotel data with vector embeddings
└── vector-search-quickstart/
├── src/
│ ├── diskann.py # DiskANN vector search implementation
│ ├── hnsw.py # HNSW vector search implementation
│ ├── ivf.py # IVF vector search implementation
│ └── utils.py # Shared utility functions
├── requirements.txt # Python dependencies
├── .env # Environment variables template
Create a src directory for your Python files. Add two files: diskann.py and utils.py for the DiskANN index implementation:
mkdir src
touch src/diskann.py
touch src/utils.pyCreate a src directory for your Python files. Add two files: ivf.py and utils.py for the IVF index implementation:
mkdir src
touch src/ivf.py
touch src/utils.pyCreate a src directory for your Python files. Add two files: hnsw.py and utils.py for the HNSW index implementation:
mkdir src
touch src/hnsw.py
touch src/utils.pyTip
Unlike some databases, DocumentDB allows you to create and drop vector indexes at any time after container creation. You don't need to define the vector indexing policy at container creation time.
Paste the following code into the diskann.py file.
:::code language="python" source="~/../documentdb-samples/ai/vector-search-python/src/diskann.py" :::
Paste the following code into the ivf.py file.
:::code language="python" source="~/../documentdb-samples/ai/vector-search-python/src/ivf.py" :::
Paste the following code into the hnsw.py file.
:::code language="python" source="~/../documentdb-samples/ai/vector-search-python/src/hnsw.py" :::
This main module provides these features:
-
Includes utility functions
-
Creates a configuration object for environment variables
-
Creates clients for Azure OpenAI and Azure DocumentDB
-
Connects to MongoDB, creates a database and collection, inserts data, and creates standard indexes
-
Creates a vector index using IVF, HNSW, or DiskANN
-
Creates an embedding for a sample query text using the OpenAI client. You can change the query at the top of the file
-
Runs a vector search using the embedding and prints the results
Paste the following code into utils.py:
:::code language="python" source="~/../documentdb-samples/ai/vector-search-python/src/utils.py" :::
This utility module provides these features:
get_clients: Creates and returns clients for Azure OpenAI and Azure DocumentDBget_clients_passwordless: Creates and returns clients for Azure OpenAI and Azure DocumentDB using passwordless authenticationazure_identity_token_callback: Gets an Azure AD token used by MongoDB OIDC authenticationread_file_return_json: Reads a JSON file and returns its contents as an array of objectswrite_file_json: Writes an array of objects to a JSON fileinsert_data: Inserts data in batches into a MongoDB collection and creates standard indexes on specified fieldsdrop_vector_indexes: Drops existing vector indexes on the target vector fieldprint_search_results: Prints vector search results, including score and hotel name
Sign in to Azure CLI before you run the application so it can access Azure resources securely.
az loginThe code uses your local developer authentication to access Azure DocumentDB and Azure OpenAI. When you set AZURE_TOKEN_CREDENTIALS=AzureCliCredential, this setting tells the function to use Azure CLI credentials for authentication deterministically. The authentication relies on DefaultAzureCredential from azure-identity to find your Azure credentials in the environment. Learn more about how to Authenticate Python apps to Azure services using the Azure Identity library.
To run the Python scripts:
python src/diskann.pypython src/ivf.pypython src/hnsw.pyYou see the top five hotels that match the vector search query and their similarity scores.
-
Select the DocumentDB extension in Visual Studio Code to connect to your Azure DocumentDB account.
-
View the data and indexes in the Hotels database.
:::image type="content" source="./media/quickstart-nodejs-vector-search/visual-studio-code-documentdb.png" lightbox="./media/quickstart-nodejs-vector-search/visual-studio-code-documentdb.png" alt-text="Screenshot of DocumentDB extension showing the Azure DocumentDB collection.":::
Delete the resource group, Azure DocumentDB account, and Azure OpenAI resource when you don't need them to avoid extra costs.