Skip to content

Commit f3e0be9

Browse files
authored
Python: Disable mem0 telemetry by default (microsoft#3506)
* disable mem0 telemetry by default * test fix * addressed comments
1 parent 8b475af commit f3e0be9

4 files changed

Lines changed: 67 additions & 1 deletion

File tree

python/packages/mem0/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,14 @@ See the [Mem0 basic example](https://github.com/microsoft/agent-framework/tree/m
1818
- Teaching the agent user preferences
1919
- Retrieving information using remembered context across new threads
2020
- Persistent memory
21+
22+
## Telemetry
23+
24+
Mem0's telemetry is **disabled by default** when using this package. If you want to enable telemetry, set the environment variable before importing:
25+
26+
```python
27+
import os
28+
os.environ["MEM0_TELEMETRY"] = "true"
29+
30+
from agent_framework.mem0 import Mem0Provider
31+
```

python/packages/mem0/agent_framework_mem0/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# Copyright (c) Microsoft. All rights reserved.
22

33
import importlib.metadata
4+
import os
5+
6+
# Disable Mem0 telemetry by default to prevent usage data from being sent to telemetry provider.
7+
# Users can opt-in by setting MEM0_TELEMETRY=true before importing this package.
8+
if os.environ.get("MEM0_TELEMETRY") is None:
9+
os.environ["MEM0_TELEMETRY"] = "false"
410

511
from ._provider import Mem0Provider
612

python/packages/mem0/agent_framework_mem0/_provider.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ class MemorySearchResponse_v1_1(TypedDict):
3030

3131

3232
class Mem0Provider(ContextProvider):
33-
"""Mem0 Context Provider."""
33+
"""Mem0 Context Provider.
34+
35+
Note:
36+
Mem0's telemetry is disabled by default when using this package.
37+
To enable telemetry, set the environment variable ``MEM0_TELEMETRY=true`` before
38+
importing this package.
39+
"""
3440

3541
def __init__(
3642
self,

python/packages/mem0/tests/test_mem0_context_provider.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Copyright (c) Microsoft. All rights reserved.
22
# pyright: reportPrivateUsage=false
33

4+
import importlib
5+
import os
6+
import sys
47
from unittest.mock import AsyncMock, patch
58

69
import pytest
@@ -592,3 +595,43 @@ def test_build_filters_returns_empty_dict_when_no_parameters(self, mock_mem0_cli
592595

593596
filters = provider._build_filters()
594597
assert filters == {}
598+
599+
600+
class TestMem0Telemetry:
601+
"""Test telemetry configuration for Mem0."""
602+
603+
def test_mem0_telemetry_disabled_by_default(self, monkeypatch: pytest.MonkeyPatch) -> None:
604+
"""Test that MEM0_TELEMETRY is set to 'false' by default when importing the package."""
605+
# Ensure MEM0_TELEMETRY is not set before importing the module under test
606+
monkeypatch.delenv("MEM0_TELEMETRY", raising=False)
607+
608+
# Remove cached modules to force re-import and trigger module-level initialization
609+
modules_to_remove = [key for key in sys.modules if key.startswith("agent_framework_mem0")]
610+
for mod in modules_to_remove:
611+
del sys.modules[mod]
612+
613+
# Import (and reload) the module so that it can set MEM0_TELEMETRY when unset
614+
import agent_framework_mem0
615+
616+
importlib.reload(agent_framework_mem0)
617+
618+
# The environment variable should be set to "false" after importing
619+
assert os.environ.get("MEM0_TELEMETRY") == "false"
620+
621+
def test_mem0_telemetry_respects_user_setting(self, monkeypatch: pytest.MonkeyPatch) -> None:
622+
"""Test that user-set MEM0_TELEMETRY value is not overwritten."""
623+
# Remove cached modules to force re-import
624+
modules_to_remove = [key for key in sys.modules if key.startswith("agent_framework_mem0")]
625+
for mod in modules_to_remove:
626+
del sys.modules[mod]
627+
628+
# Set user preference before import
629+
monkeypatch.setenv("MEM0_TELEMETRY", "true")
630+
631+
# Re-import the module
632+
import agent_framework_mem0
633+
634+
importlib.reload(agent_framework_mem0)
635+
636+
# User setting should be preserved
637+
assert os.environ.get("MEM0_TELEMETRY") == "true"

0 commit comments

Comments
 (0)