{"record":{"id":"70d0c4556617ba8a","repo":"TencentCloud/TencentDB-Agent-Memory","slug":"v3-memoryclient-add-conversation-requires-session","errorCode":null,"errorMessage":"v3 MemoryClient.add_conversation requires session_id: pass it in the constructor or per call. Reads (query/search/count) may omit it to aggregate across sessions.","messagePattern":"v3 MemoryClient\\.add_conversation requires session_id: pass it in the constructor or per call\\. Reads \\(query/search/count\\) may omit it to aggregate across sessions\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdk/memory-core/python/tencentdb_agent_memory/v3/client.py","lineNumber":148,"sourceCode":"        \"\"\"L0/L1 调用：override > 构造时 session_id。\n\n        v3 服务端 session_id 可选：传入则按 session 收敛，缺则按 (team,agent,user)\n        跨 session 聚合查询/计数（\"agent 维度全量视图\"语义，用于治理面板等场景）。\n        本方法返回最终生效的 session_id，缺即 None — 调用方应在 None 时\n        不把 session_id 字段塞入请求 body。\n        \"\"\"\n        return override or self.session_id\n\n    def resolve_session_for_write(self, override: Optional[str]) -> str:\n        \"\"\"写入路径专用：``add_conversation`` 必须拿到非空 session_id。\n\n        缺则抛 ``ValueError`` —— 避免服务端把无 session 的写入静默合并到默认\n        bucket，与其他调用方的数据混在一起。读取路径（query/search/count/\n        delete）仍走 ``resolve_session``，允许缺省以做跨 session 聚合。\n        \"\"\"\n        sid = override or self.session_id\n        if not sid:\n            raise ValueError(\n                \"v3 MemoryClient.add_conversation requires session_id: \"\n                \"pass it in the constructor or per call. \"\n                \"Reads (query/search/count) may omit it to aggregate across sessions.\"\n            )\n        return sid\n\n\n# ---------------------------------------------------------------------------\n# Synchronous client\n# ---------------------------------------------------------------------------\n\nclass MemoryClient:\n    \"\"\"v3 同步客户端 — 严格 isolation L0–L3 数据面（含 count endpoint）。\n\n    构造必填：``team_id`` / ``agent_id`` / ``user_id``。\n    构造可选：``session_id``（不传时所有 L0–L3 接口都跨 session 聚合），``task_id``，\n    ``user_key``（资产级接口如 ``clear_chat_memory`` 需要）。\n    \"\"\"","sourceCodeStart":130,"sourceCodeEnd":166,"githubUrl":"https://github.com/TencentCloud/TencentDB-Agent-Memory/blob/3efcd317b84146d6a08518ac0f7ee7c8a8d200ec/sdk/memory-core/python/tencentdb_agent_memory/v3/client.py#L130-L166","documentation":"Writes via add_conversation must target a specific session; resolve_session_for_write raises ValueError when neither the per-call session_id nor the constructor session_id is set. Reads (query/search/count) intentionally allow an absent session_id to aggregate across sessions, so this guard only applies to the write path.","triggerScenarios":"Calling add_conversation on a client constructed without session_id and without passing session_id=... in the call; clearing a previously set client.session_id to None then writing.","commonSituations":"Reusing a read-oriented client (built for cross-session search) for writes; omitting session_id because an older SDK version accepted it; builder/factory code that conditionally sets session_id.","solutions":["Pass session_id=... directly in the add_conversation call","Set a non-empty session_id in the MemoryClient constructor for write-heavy clients","Use with_isolation(session_id=...) to derive a write-scoped client","Keep separate clients: one for cross-session reads, one with a fixed session for writes"],"exampleFix":"// before\nclient = MemoryClient(..., team_id=t, agent_id=a, user_id=u)  # no session_id\nclient.add_conversation(messages=msgs)\n// after\nclient.add_conversation(messages=msgs, session_id=\"session-123\")\n# or: client = client.with_isolation(session_id=\"session-123\")","handlingStrategy":"validation","validationCode":"def ensure_write_session(client, override=None):\n    sid = override or getattr(client, \"session_id\", None)\n    if not sid:\n        raise ValueError(\"session_id required for add_conversation\")\n    return sid\n# client.add_conversation(..., session_id=ensure_write_session(client))","typeGuard":"def has_session(c) -> bool:\n    return bool(getattr(c, \"session_id\", None))","tryCatchPattern":"try:\n    client.add_conversation(messages=msgs)\nexcept ValueError as e:\n    if \"requires session_id\" in str(e):\n        client = client.with_isolation(session_id=new_session_id())\n        client.add_conversation(messages=msgs)","preventionTips":["Always construct write-clients with a session_id","Generate a session id per logical conversation and store it alongside state","Keep read clients (no session) separate from write clients","Re-check session_id after any client reconfiguration"],"tags":["python","validation","session"],"backgroundTag":"missing-session-id","analyzedSha":"3efcd317b84146d6a08518ac0f7ee7c8a8d200ec","analyzedAt":"2026-09-01T05:44:22.276Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}