---
title: Azure AI Search
description: "Use Azure AI Search as a vector store in Mem0 for managed vector search with service name and API key setup."
---

[Azure AI Search](https://learn.microsoft.com/azure/search/search-what-is-azure-search/) (formerly known as "Azure Cognitive Search") provides secure information retrieval at scale over user-owned content in traditional and generative AI search applications.

## Usage

```python
import os
from mem0 import Memory

os.environ["OPENAI_API_KEY"] = "sk-xx"   # This key is used for embedding purpose

config = {
    "vector_store": {
        "provider": "azure_ai_search",
        "config": {
            "service_name": "<your-azure-ai-search-service-name>",
            "api_key": "<your-api-key>",
            "collection_name": "mem0", 
            "embedding_model_dims": 1536
        }
    }
}

m = Memory.from_config(config)
messages = [
    {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
    {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
    {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
    {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
m.add(messages, user_id="alice", metadata={"category": "movies"})
```

## Using binary compression for large vector collections

```python
config = {
    "vector_store": {
        "provider": "azure_ai_search",
        "config": {
            "service_name": "<your-azure-ai-search-service-name>",
            "api_key": "<your-api-key>",
            "collection_name": "mem0", 
            "embedding_model_dims": 1536,
            "compression_type": "binary",
            "use_float16": True  # Use half precision for storage efficiency
        }
    }
}
```

## Using hybrid search

```python
config = {
    "vector_store": {
        "provider": "azure_ai_search",
        "config": {
            "service_name": "<your-azure-ai-search-service-name>",
            "api_key": "<your-api-key>",
            "collection_name": "mem0", 
            "embedding_model_dims": 1536,
            "hybrid_search": True,
            "vector_filter_mode": "postFilter"
        }
    }
}
```

## Using Azure Identity for Authentication
As an alternative to using an API key, the Azure Identity credential chain can be used to authenticate with Azure OpenAI. The list below shows the order of precedence for credential application:

1. **Environment Credential:**
Azure client ID, secret, tenant ID, or certificate in environment variables for service principal authentication.

2. **Workload Identity Credential:**
Utilizes Azure Workload Identity (relevant for Kubernetes and Azure workloads).

3. **Managed Identity Credential:**
Authenticates as a Managed Identity (for apps/services hosted in Azure with Managed Identity enabled), this is the most secure production credential.

4. **Shared Token Cache Credential / Visual Studio Credential (Windows only):**
Uses cached credentials from Visual Studio sign-ins (and sometimes VS Code if SSO is enabled).

5. **Azure CLI Credential:**
Uses the currently logged-in user from the Azure CLI (`az login`), this is the most common development credential.

6. **Azure PowerShell Credential:**
Uses the identity from Azure PowerShell (`Connect-AzAccount`).

7. **Azure Developer CLI Credential:**
Uses the session from Azure Developer CLI (`azd auth login`).

<Note> If an API is provided, it will be used for authentication over an Azure Identity </Note>
To enable Role-Based Access Control (RBAC) for Azure AI Search, follow these steps:

1. In the Azure Portal, navigate to your **Azure AI Search** service.
2. In the left menu, select **Settings** > **Keys**.
3. Change the authentication setting to **Role-based access control**, or **Both** if you need API key compatibility. The default is “Key-based authentication”—you must switch it to use Azure roles.
4. **Go to Access Control (IAM):**
    - In the Azure Portal, select your Search service.
    - Click **Access Control (IAM)** on the left.
5. **Add a Role Assignment:**
    - Click **Add** > **Add role assignment**.
6. **Choose Role:**
    - Mem0 requires the **Search Index Data Contributor** and **Search Service Contributor** role.
7. **Choose Member**
    - To assign to a User, Group, Service Principal or Managed Identity:
        - For production it is recommended to use a service principal or managed identity.
            - For a service principal: select **User, group, or service principal** and search for the service principal.
            - For a managed identity: select **Managed identity** and choose the managed identity.
        - For development, you can assign the role to a user account.
            - For development: select **User, group, or service principal** and pick an Azure Entra ID account (the same used with `az login`).
8. **Complete the Assignment:**
    - Click **Review + Assign**.

If you are using Azure Identity, do not set the `api_key` in the configuration.
```python
config = {
    "vector_store": {
        "provider": "azure_ai_search",
        "config": {
            "service_name": "<your-azure-ai-search-service-name>",
            "collection_name": "mem0", 
            "embedding_model_dims": 1536,
            "compression_type": "binary",
            "use_float16": True  # Use half precision for storage efficiency
        }
    }
}
```

### Environment Variables to Use Azure Identity Credential
* For an Environment Credential, you will need to setup a Service Principal and set the following environment variables:
  - `AZURE_TENANT_ID`: Your Azure Active Directory tenant ID.
  - `AZURE_CLIENT_ID`: The client ID of your service principal or managed identity.
  - `AZURE_CLIENT_SECRET`: The client secret of your service principal.
* For a User-Assigned Managed Identity, you will need to set the following environment variable:
  - `AZURE_CLIENT_ID`: The client ID of the user-assigned managed identity.
* For a System-Assigned Managed Identity, no additional environment variables are needed.

### Developer Logins for Azure Identity Credential
* For an Azure CLI Credential, you need to have the Azure CLI installed and logged in with `az login`.
* For an Azure PowerShell Credential, you need to have the Azure PowerShell module installed and logged in with `Connect-AzAccount`.
* For an Azure Developer CLI Credential, you need to have the Azure Developer CLI installed and logged in with `azd auth login`.

Troubleshooting tips for [Azure Identity](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/identity/azure-identity/TROUBLESHOOTING.md#troubleshoot-environmentcredential-authentication-issues).


## Configuration Parameters

| Parameter | Description | Default Value | Options |
| --- | --- | --- | --- |
| `service_name` | Azure AI Search service name | Required | - |
| `api_key` | API key of the Azure AI Search service | Optional | If not present, the [Azure Identity](#using-azure-identity-for-authentication) credential chain will be used |
| `collection_name` | The name of the collection/index to store vectors | `mem0` | Any valid index name |
| `embedding_model_dims` | Dimensions of the embedding model | `1536` | Any integer value |
| `compression_type` | Type of vector compression to use | `none` | `none`, `scalar`, `binary` |
| `use_float16` | Store vectors in half precision (Edm.Half) | `False` | `True`, `False` |
| `vector_filter_mode` | Vector filter mode to use | `preFilter` | `postFilter`, `preFilter` |
| `hybrid_search` | Use hybrid search | `False` | `True`, `False` |

## Notes on Configuration Options

- **compression_type**: 
  - `none`: No compression, uses full vector precision
  - `scalar`: Scalar quantization with reasonable balance of speed and accuracy
  - `binary`: Binary quantization for maximum compression with some accuracy trade-off

- **vector_filter_mode**:
  - `preFilter`: Applies filters before vector search (faster)
  - `postFilter`: Applies filters after vector search (may provide better relevance)

- **use_float16**: Using half precision (float16) reduces storage requirements but may slightly impact accuracy. Useful for very large vector collections.

- **Filterable Fields**: The implementation automatically extracts `user_id`, `run_id`, and `agent_id` fields from payloads for filtering.