mem0 icon indicating copy to clipboard operation
mem0 copied to clipboard

Mem0 Timeout. Searching memories retrieves no results until reinstantiated.

Open hayescode opened this issue 7 months ago • 9 comments

🐛 Describe the bug

There seems to be some kind of timeout in the Mem0Memory client. If there is no activity for 5-10 minutes in the client then executing search() returns empty results. Only once re-instantiated are search results returned. I have tested this with the same queries and it only happens after about 5-10 minutes, but I cannot find any timeout logic or parameters in the code to prevent or extend this.

I'm using Azure AI Search and Azure OpenAI.

import logging
from datetime import datetime
from typing import Annotated, Optional

from mem0 import AsyncMemory as Mem0Memory
from mem0.configs.base import LlmConfig, MemoryConfig
from mem0.embeddings.configs import EmbedderConfig
from mem0.vector_stores.configs import VectorStoreConfig


class Memory:
    def __init__(
        self,
        llm_model: GeneralPurposeModel,
        embedding_model: EmbeddingModel,
        agent_name: AssistantName,
        user_details: UserDetails,
    ):
        self.agent_name: AssistantName = agent_name
        self.agent_index_name: MemoryIndexName = f"memory-{str(self.agent_name).lower()}"
        self.user_entra_uuid = user_details.object_id

        self.llm_config = LlmConfig(
            provider="azure_openai",
            config={
                "azure_kwargs": {
                    "azure_deployment": llm_model,
                    "api_version": creds.AZURE_OPENAI_API_VERSION,
                    "azure_endpoint": creds.AZURE_OPENAI_BASE,
                    "api_key": creds.AZURE_OPENAI_KEY,
                },
            },
        )
        self.embedder_config = EmbedderConfig(
            provider="azure_openai",
            config={
                "azure_kwargs": {
                    "azure_deployment": embedding_model,
                    "api_version": creds.AZURE_OPENAI_API_VERSION,
                    "azure_endpoint": creds.AZURE_OPENAI_BASE,
                    "api_key": creds.AZURE_OPENAI_KEY,
                },
            },
        )
        self.vector_store_config = VectorStoreConfig(
            provider="azure_ai_search",
            config={
                "service_name": creds.AZURE_SEARCH_SERVICE_NAME,
                "api_key": creds.AZURE_SEARCH_API_KEY,
                "collection_name": self.agent_index_name,
                "hybrid_search": True,
                "vector_filter_mode": "preFilter",
                "embedding_model_dims": 3072,
            },
        )
        try:
            self.client = Mem0Memory(
                MemoryConfig(
                    llm=self.llm_config,
                    embedder=self.embedder_config,
                    vector_store=self.vector_store_config,
                    custom_update_memory_prompt=custom_user_prompt,
                )
            )
        except Exception as e:
            raise ValueError(f"Error creating memory client: {e}")

    async def search_agent_docs(self, query: Annotated[str, "The brand keyword to search"]):
        limit = 10
        relevance_threshold = 0.025
        try:
            raw_result = await self.client.search(query=query, agent_id=self.agent_name, limit=limit)
            result = [
                result for result in raw_result.get("results", []) if result.get("score", 0) >= relevance_threshold
            ]

            if not result:
                print(f"No results found for query: {query}\n{raw_result}")
                # This gets hit if this class has not been hit in 5-10 minutes

hayescode avatar May 12 '25 20:05 hayescode

Hey @hayescode, can you please provide the code you are using with an example? cc @prateekchhikara

deshraj avatar May 12 '25 20:05 deshraj

Hey @hayescode, can you please provide the code you are using with an example? cc @prateekchhikara

@deshraj I added it to the PR description. search_agent_docs() is a custom function I pass to my LLM. It seems like if this class hasn't been interacted with for 5-10 minutes it just returns an empty results. If I reinstantiate then it works great, leading me to think there's a timeout somewhere but I don't get any logs/errors.

hayescode avatar May 12 '25 22:05 hayescode

@hayescode thanks for sharing, let me check and get back to you.

prateekchhikara avatar May 13 '25 06:05 prateekchhikara

Hi @hayescode, thanks for the detailed report!

We have looked into the issue, and based on your observation that it only occurs after 5–10 minutes of inactivity and specifically only with Azure backends, the most likely root cause is related to Azure’s connection or token handling, not Mem0 itself.

Mem0 appears to function correctly when using OpenAI directly, but Azure OpenAI and Azure AI Search can exhibit silent timeouts or stale client behavior in long-lived async sessions. In particular:

  1. Azure AI Search may maintain a stale TCP session that doesn’t automatically refresh.

  2. Azure OpenAI or Search credentials (if token-based) might expire silently and not raise an exception.

  3. The underlying SDKs sometimes return empty responses rather than throwing errors when connections go stale.

We suspect that after several minutes of inactivity, the Azure client becomes stale, and Mem0’s search() call receives a valid (but empty) response from the vector store.

prateekchhikara avatar May 13 '25 20:05 prateekchhikara

Hi @hayescode, thanks for the detailed report!

We have looked into the issue, and based on your observation that it only occurs after 5–10 minutes of inactivity and specifically only with Azure backends, the most likely root cause is related to Azure’s connection or token handling, not Mem0 itself.

Mem0 appears to function correctly when using OpenAI directly, but Azure OpenAI and Azure AI Search can exhibit silent timeouts or stale client behavior in long-lived async sessions. In particular:

  1. Azure AI Search may maintain a stale TCP session that doesn’t automatically refresh.

  2. Azure OpenAI or Search credentials (if token-based) might expire silently and not raise an exception.

  3. The underlying SDKs sometimes return empty responses rather than throwing errors when connections go stale.

We suspect that after several minutes of inactivity, the Azure client becomes stale, and Mem0’s search() call receives a valid (but empty) response from the vector store.

@prateekchhikara thank you for the detailed response! I'll add to my list of Azure complaints haha. If I can narrow it down I'll post the resolution here. Closing this now

hayescode avatar May 13 '25 21:05 hayescode

@prateekchhikara i think posthog is causing this issue. I get logs like below only when failure to retrieve memories happens. I even try re-instantiating the sAsyncMem0Memory client and then searching but still get no results.

2025-05-16T16:39:46.2259059Z 2025-05-16 16:39:46 - Backing off send_request(...) for 0.7s (requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='us.i.posthog.com', port=443): Read timed out. (read timeout=15))
2025-05-16T16:39:46.7172695Z 2025-05-16 16:39:46 - Backing off send_request(...) for 0.6s (requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='us.i.posthog.com', port=443): Read timed out. (read timeout=15))
2025-05-16T16:39:49.0627548Z 2025-05-16 16:39:49 - Backing off send_request(...) for 0.6s (requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='us.i.posthog.com', port=443): Read timed out. (read timeout=15))

hayescode avatar May 16 '25 16:05 hayescode

well setting the MEM0_TELEMETRY=False environment variable got rid of the failure logs, but the issue persists. I wonder it it's because of the AsyncMemory class simply wrapping the Azure AI Search sync client, even though an Azure AI Search async client exists. Here is the documentation.

from azure.search.documents.aio import SearchClient # Async
from azure.search.documents import SearchClient # Sync (what Mem0 uses even in AsyncMemory)

Can we (should we?) make an AsyncAzureAISearch in the vector_stores/azure_ai_search.py and point the AsyncMemory class to use that?

Edit I switched back to the sync Memory class instead of AsyncMemory and that fixed it, so it seems no using the .aio azure ai search SDK is the issue.

hayescode avatar May 16 '25 17:05 hayescode

@hayescode can you please raise a PR so I can review it?

prateekchhikara avatar May 19 '25 18:05 prateekchhikara

@hayescode can you please raise a PR so I can review it?

@prateekchhikara I just changed back to the sync memory which fixed it.

hayescode avatar May 19 '25 19:05 hayescode

Perfect, closing the issue.

prateekchhikara avatar May 30 '25 04:05 prateekchhikara