mem0ai/mem0 · error · LLMError
LLM extraction failed: {e}
Error message
LLM extraction failed: {e} What it means
Raised as LLMError by the memory-extraction helper when the underlying LLM call inside Memory.add() fails for any reason — rate limits (429), auth errors, timeouts, malformed provider responses, or network failures. The SDK deliberately re-raises (instead of returning an empty extraction) so callers can distinguish 'LLM unavailable' from 'no facts found' and implement retry or provider fallback; the original exception is chained via 'from e' and logged first.
Source
Thrown at mem0/memory/main.py:969
last_k_messages=last_messages,
custom_instructions=custom_instr,
)
try:
response = self.llm.generate_response(
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt},
],
response_format={"type": "json_object"},
)
except Exception as e:
# Re-raise so callers can implement provider fallback / retry.
# The original silent ``return []`` made upstream callers unable to
# distinguish "LLM unavailable" (429/5xx/timeout) from "LLM
# extracted no facts" -- both surfaced as an empty list.
logger.error(f"LLM extraction failed: {e}")
raise LLMError(f"LLM extraction failed: {e}") from e
# Parse response
try:
response = remove_code_blocks(response)
if not response or not response.strip():
extracted_memories = []
else:
try:
extracted_memories = json.loads(response, strict=False).get("memory", [])
except json.JSONDecodeError:
extracted_json = extract_json(response)
extracted_memories = json.loads(extracted_json, strict=False).get("memory", [])
except Exception as e:
logger.error(f"Error parsing extraction response: {e}")
extracted_memories = []
if not extracted_memories:
# Save messages even if nothing extractedView on GitHub (pinned to 001c235229)
Solutions
- Inspect the chained cause (exc.__cause__) — the fix differs for 429 (back off / raise limits) vs 401 (fix the key) vs timeout (increase timeout or use a faster model).
- Retry with exponential backoff for transient errors (429/5xx/timeouts); LLMError here is a reliable marker that extraction failed, not that no memories were found.
- Verify provider config in MemoryConfig (llm.provider and its api_key/model) and test connectivity independently.
- Add a fallback provider or queue the add() for later if the LLM is degraded.
- Catch mem0.memory.utils.LLMError (or LLMError from mem0.utils) specifically so validation errors are not swallowed.
Example fix
# before
m.add("user likes espresso", user_id="u1") # raises LLMError on 429, app crashes
# after
from mem0.memory.utils import LLMError
import time
for attempt in range(3):
try:
m.add("user likes espresso", user_id="u1")
break
except LLMError as e:
if attempt == 2 or "401" in str(e.__cause__):
raise
time.sleep(2 ** attempt) Defensive patterns
Strategy: retry
Validate before calling
# preflight: verify the configured LLM is reachable before relying on add()
# e.g. for openai-style providers
try:
m.llm.generate_response([{ "role": "user", "content": "ping" }])
except Exception as e:
raise RuntimeError(f"LLM provider unreachable: {e}") Type guard
def is_retryable_llm_error(exc) -> bool:
text = str(getattr(exc, "__cause__", exc)).lower()
return any(s in text for s in ("429", "rate limit", "timeout", "503", "502", "connection")) Try / catch
from mem0.memory.utils import LLMError
import time
def add_with_retry(m, msg, *, attempts=4, **kw):
last = None
for i in range(attempts):
try:
return m.add(msg, **kw)
except LLMError as e:
last = e
if not is_retryable_llm_error(e) or i == attempts - 1:
raise
time.sleep(2 ** i)
raise last Prevention
- Wrap add() in exponential-backoff retry for 429/5xx/timeout; re-raise immediately on auth errors.
- Health-check the LLM provider at service startup (bad keys fail fast there, not mid-conversation).
- Differentiate LLMError (extraction failed) from a normal 'no memories extracted' result — do not treat it as empty.
- Watch rate-limit headers from your provider and throttle add() calls below quota.
When it happens
Trigger: m.add(...) with an expired or wrong OPENAI_API_KEY; hitting provider rate limits under load; Ollama/vLLM local server down or timing out; a custom LLM provider whose generate_response raises on unexpected response shapes; transient 5xx from the provider.
Common situations: Production traffic spikes exhausting API quotas; local model server (Ollama, LM Studio) not started; mistyped or rotated API keys; flaky networks between the service and the provider; switching LLM provider configs without updating credentials.
Related errors
- DeepSeek LLM failed: ${message}
- LLM extraction failed: ${e}
- LiteLLM failed: ${message}
- LM Studio LLM failed: ${message}
- MiniMax LLM failed: ${message}
AI-assisted analysis of mem0ai/mem0@001c235229 (2026-08-15).
Data as JSON: /api/errors/2f3aa719cf9b1f88.
Report an issue: GitHub.