Skip to content

The agent that grows with you

fix(memory): truncate oversized mem0 sync messages at the source

liuhao1024040742d06bab
diff --git a/plugins/memory/mem0/__init__.py b/plugins/memory/mem0/__init__.py
index f45773ec5ec..4464a2917b4 100644
--- a/plugins/memory/mem0/__init__.py
+++ b/plugins/memory/mem0/__init__.py
@@ -33,6 +33,30 @@ _CLIENT_ERROR_TYPES = ("MemoryNotFoundError", "ValidationError")
# so legacy mem0.json files written by the wizard don't override gateway-native ids.
_DEFAULT_USER_ID = "hermes-user"
+# sync_turn sends the whole turn to the backend for fact extraction. OSS embedding
+# models often have small context windows (bge-small-zh-v1.5: 512 tokens ≈ 500 chars;
+# jina-embeddings-v3: 8192), and oversized turns make backend.add() raise — Ollama
+# answers HTTP 500, hosted APIs return INPUT_TOKEN_LIMIT_EXCEEDED — which _try only
+# logs, silently dropping the turn's memory extraction. Cap each message up front.
+_SYNC_MSG_MAX_CHARS = 450
+
+
+def _truncate_for_sync(text: str, max_len: int = _SYNC_MSG_MAX_CHARS) -> str:
+ """Cap a synced message at its last sentence boundary within ``max_len``.
+
+ Short messages pass through unchanged; long ones keep the last complete
+ sentence inside the window so fact extraction still sees coherent statements,
+ with a hard cut as fallback when no boundary exists (or one only appears in
+ the first third of the window, which usually means unsegmented input).
+ """
+ if len(text) <= max_len:
+ return text
+ for sep in ("。", "!", "?", ".\n", ".", "!", "?"):
+ cut = text[:max_len].rfind(sep)
+ if cut > max_len // 3:
+ return text[:cut + 1]
+ return text[:max_len]
+
def _is_client_error(exc: Exception) -> bool:
"""True for user-caused errors (bad ID, not found) that should NOT trip circuit breaker."""
@@ -266,7 +290,10 @@ class Mem0MemoryProvider(MemoryProvider):
def _sync():
if self._backend is not None:
- messages = [{"role": "user", "content": user_content}, {"role": "assistant", "content": assistant_content}]
+ messages = [
+ {"role": "user", "content": _truncate_for_sync(user_content)},
+ {"role": "assistant", "content": _truncate_for_sync(assistant_content)},
+ ]
self._try(lambda: self._add(messages, infer=True), logger.warning, "Mem0 sync failed: %s")
with self._sync_lock:
diff --git a/tests/plugins/memory/test_mem0_v3.py b/tests/plugins/memory/test_mem0_v3.py
index 48d66d480b2..bb72d87615c 100644
--- a/tests/plugins/memory/test_mem0_v3.py
+++ b/tests/plugins/memory/test_mem0_v3.py
@@ -146,6 +146,41 @@ class TestMem0V3Internal:
assert call[2]["infer"] is True
+class TestSyncTurnTruncation:
+ """sync_turn must cap messages before ingestion so small-context embedding
+ backends (OSS Ollama bge-small-zh-v1.5: 512 tokens; jina-embeddings-v3 token
+ limits) don't fail the whole extraction — a failure _try only logs."""
+
+ def _make_provider(self, monkeypatch, backend):
+ provider = Mem0MemoryProvider()
+ provider.initialize("test-session")
+ provider._user_id = "u123"
+ provider._agent_id = "hermes"
+ provider._backend = backend
+ return provider
+
+ def test_small_context_backend_never_sees_oversized_input(self, monkeypatch):
+ """Regression for #106235/#37421: an OSS embedding backend with a small
+ context window raises on oversized input; truncation up front keeps the
+ extraction from being silently dropped (no breaker failures)."""
+
+ class SmallContextBackend(FakeBackend):
+ def add(self, messages, **kwargs):
+ if any(len(m["content"]) > mem0_plugin._SYNC_MSG_MAX_CHARS for m in messages):
+ raise RuntimeError("HTTP 500: embedding input exceeds model context")
+ return super().add(messages, **kwargs)
+
+ backend = SmallContextBackend()
+ provider = self._make_provider(monkeypatch, backend)
+ provider.sync_turn("Short question?", "".join(f"Fact {i}. " for i in range(200)), session_id="s1")
+ provider._sync_thread.join(timeout=2)
+ assert len(backend.captured) == 1
+ sent = backend.captured[0][1]
+ assert sent[0]["content"] == "Short question?" # under the cap: untouched
+ assert len(sent[1]["content"]) <= mem0_plugin._SYNC_MSG_MAX_CHARS and sent[1]["content"].endswith(".")
+ assert provider._consecutive_failures == 0
+
+
class TestMem0Prefetch:
"""prefetch() must recall on the CURRENT question, synchronously.