{"record":{"id":"4ab10d08a77ae6fb","repo":"agentscope-ai/agentscope","slug":"mem0middleware-requires-a-non-empty-user-id","errorCode":null,"errorMessage":"\"Mem0Middleware requires a non-empty `user_id`.\"","messagePattern":"\"Mem0Middleware requires a non-empty `user_id`\\.\"","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/agentscope/middleware/_longterm_memory/_mem0/_middleware.py","lineNumber":254,"sourceCode":"                the agent that created them. When ``False`` search uses\n                ``user_id`` only, so a user's memories are shared across\n                agents.\n            await_write:\n                When ``True`` (default) the post-turn ``add`` call is\n                awaited inline. When ``False`` it's fire-and-forget —\n                faster response but exceptions only surface in logs.\n            memory_section_header, memory_section_intro:\n                Strings used when injecting retrieved memories into\n                the model's messages list (``static_control`` /\n                ``both`` modes).\n            tool_instructions:\n                Markdown block appended to the agent's system prompt\n                in ``agent_control`` / ``both`` modes, advertising the\n                ``search_memory`` / ``add_memory`` tools to the LLM.\n        \"\"\"\n        is_empty_user_id = isinstance(user_id, str) and not user_id.strip()\n        if user_id is None or is_empty_user_id:\n            raise ValueError(\n                \"Mem0Middleware requires a non-empty `user_id`.\",\n            )\n        if mode not in (\"static_control\", \"agent_control\", \"both\"):\n            raise ValueError(\n                f\"Unknown mode {mode!r}; expected one of \"\n                f\"'static_control', 'agent_control', 'both'.\",\n            )\n\n        client = self._resolve_client(\n            client=client,\n            chat_model=chat_model,\n            embedding_model=embedding_model,\n            mem0_config=mem0_config,\n        )\n        self._client = client\n\n        self._user_id = user_id\n        self._agent_id = agent_id","sourceCodeStart":236,"sourceCodeEnd":272,"githubUrl":"https://github.com/agentscope-ai/agentscope/blob/e90f1c7592896cc95f6e5ee506194f533378247d/src/agentscope/middleware/_longterm_memory/_mem0/_middleware.py#L236-L272","documentation":"Mem0Middleware namespaces all memory operations by user_id; a None or whitespace-only string has no valid namespace, so the constructor rejects it immediately.","triggerScenarios":"Mem0Middleware(user_id='') or user_id='   ' or omitting user_id (default None); passing a dynamically computed id that came back empty.","commonSituations":"Deriving user_id from request headers/session objects that are missing in dev; copy-pasted examples without filling in the id.","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"],"exampleFix":"// before\nmw = Mem0Middleware(user_id=get_user(request))\n// after\nuid = get_user(request) or f'anon-{request.client}'\nmw = Mem0Middleware(user_id=uid)","handlingStrategy":"validation","validationCode":"if user_id is None or (isinstance(user_id, str) and not user_id.strip()):\n    raise ValueError('user_id required')\n# or normalize:\nuser_id = (user_id or '').strip() or 'anonymous'","typeGuard":"def is_valid_user_id(uid) -> bool:\n    return isinstance(uid, str) and bool(uid.strip())","tryCatchPattern":"try:\n    mw = Mem0Middleware(user_id=uid, ...)\nexcept ValueError as e:\n    if 'user_id' in str(e):\n        mw = Mem0Middleware(user_id='anonymous', ...)\n    else:\n        raise","preventionTips":["Never pass session-derived ids unchecked; default them","Unit-test middleware construction with empty-string user_id"],"tags":["mem0","user-id","constructor","validation"],"backgroundTag":"missing-required-argument","analyzedSha":"e90f1c7592896cc95f6e5ee506194f533378247d","analyzedAt":"2026-08-28T18:24:12.087Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}