agentscope-ai/agentscope · error · ValueError
"Mem0Middleware requires a non-empty `user_id`."
Error message
"Mem0Middleware requires a non-empty `user_id`."
What it means
Mem0Middleware namespaces all memory operations by user_id; a None or whitespace-only string has no valid namespace, so the constructor rejects it immediately.
Source
Thrown at src/agentscope/middleware/_longterm_memory/_mem0/_middleware.py:254
the agent that created them. When ``False`` search uses
``user_id`` only, so a user's memories are shared across
agents.
await_write:
When ``True`` (default) the post-turn ``add`` call is
awaited inline. When ``False`` it's fire-and-forget —
faster response but exceptions only surface in logs.
memory_section_header, memory_section_intro:
Strings used when injecting retrieved memories into
the model's messages list (``static_control`` /
``both`` modes).
tool_instructions:
Markdown block appended to the agent's system prompt
in ``agent_control`` / ``both`` modes, advertising the
``search_memory`` / ``add_memory`` tools to the LLM.
"""
is_empty_user_id = isinstance(user_id, str) and not user_id.strip()
if user_id is None or is_empty_user_id:
raise ValueError(
"Mem0Middleware requires a non-empty `user_id`.",
)
if mode not in ("static_control", "agent_control", "both"):
raise ValueError(
f"Unknown mode {mode!r}; expected one of "
f"'static_control', 'agent_control', 'both'.",
)
client = self._resolve_client(
client=client,
chat_model=chat_model,
embedding_model=embedding_model,
mem0_config=mem0_config,
)
self._client = client
self._user_id = user_id
self._agent_id = agent_idView on GitHub (pinned to e90f1c7592)
Solutions
- Pass a concrete non-empty string id, e.g. user_id='user-123'
- When deriving from sessions, default it: user_id = session_user or 'anonymous'
- Strip and check the value before constructing the middleware
Example fix
// before
mw = Mem0Middleware(user_id=get_user(request))
// after
uid = get_user(request) or f'anon-{request.client}'
mw = Mem0Middleware(user_id=uid) Defensive patterns
Strategy: validation
Validate before calling
if user_id is None or (isinstance(user_id, str) and not user_id.strip()):
raise ValueError('user_id required')
# or normalize:
user_id = (user_id or '').strip() or 'anonymous' Type guard
def is_valid_user_id(uid) -> bool:
return isinstance(uid, str) and bool(uid.strip()) Try / catch
try:
mw = Mem0Middleware(user_id=uid, ...)
except ValueError as e:
if 'user_id' in str(e):
mw = Mem0Middleware(user_id='anonymous', ...)
else:
raise Prevention
- Never pass session-derived ids unchecked; default them
- Unit-test middleware construction with empty-string user_id
When it happens
Trigger: Mem0Middleware(user_id='') or user_id=' ' or omitting user_id (default None); passing a dynamically computed id that came back empty.
Common situations: Deriving user_id from request headers/session objects that are missing in dev; copy-pasted examples without filling in the id.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- "AgentScopeLLM requires `model` in the config to be an Agent
- "AgentScopeLLM received no usable messages (empty list or al
- "build_mem0_config requires `chat_model` and `embedding_mode
- "Mem0Middleware: `chat_model` and `embedding_model` must be
- The table_format must be one of 'markdown' or 'json', got {t
AI-assisted analysis of agentscope-ai/agentscope@e90f1c7592 (2026-08-28).
Data as JSON: /api/errors/4ab10d08a77ae6fb.
Report an issue: GitHub.