TencentCloud/TencentDB-Agent-Memory · error · ValueError
v3 MemoryClient.add_conversation requires session_id: pass i
Error message
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.
What it means
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.
Source
Thrown at sdk/memory-core/python/tencentdb_agent_memory/v3/client.py:148
"""L0/L1 调用:override > 构造时 session_id。
v3 服务端 session_id 可选:传入则按 session 收敛,缺则按 (team,agent,user)
跨 session 聚合查询/计数("agent 维度全量视图"语义,用于治理面板等场景)。
本方法返回最终生效的 session_id,缺即 None — 调用方应在 None 时
不把 session_id 字段塞入请求 body。
"""
return override or self.session_id
def resolve_session_for_write(self, override: Optional[str]) -> str:
"""写入路径专用:``add_conversation`` 必须拿到非空 session_id。
缺则抛 ``ValueError`` —— 避免服务端把无 session 的写入静默合并到默认
bucket,与其他调用方的数据混在一起。读取路径(query/search/count/
delete)仍走 ``resolve_session``,允许缺省以做跨 session 聚合。
"""
sid = override or self.session_id
if not sid:
raise ValueError(
"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."
)
return sid
# ---------------------------------------------------------------------------
# Synchronous client
# ---------------------------------------------------------------------------
class MemoryClient:
"""v3 同步客户端 — 严格 isolation L0–L3 数据面(含 count endpoint)。
构造必填:``team_id`` / ``agent_id`` / ``user_id``。
构造可选:``session_id``(不传时所有 L0–L3 接口都跨 session 聚合),``task_id``,
``user_key``(资产级接口如 ``clear_chat_memory`` 需要)。
"""View on GitHub (pinned to 3efcd317b8)
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
Example fix
// before client = MemoryClient(..., team_id=t, agent_id=a, user_id=u) # no session_id client.add_conversation(messages=msgs) // after client.add_conversation(messages=msgs, session_id="session-123") # or: client = client.with_isolation(session_id="session-123")
Defensive patterns
Strategy: validation
Validate before calling
def ensure_write_session(client, override=None):
sid = override or getattr(client, "session_id", None)
if not sid:
raise ValueError("session_id required for add_conversation")
return sid
# client.add_conversation(..., session_id=ensure_write_session(client)) Type guard
def has_session(c) -> bool:
return bool(getattr(c, "session_id", None)) Try / catch
try:
client.add_conversation(messages=msgs)
except ValueError as e:
if "requires session_id" in str(e):
client = client.with_isolation(session_id=new_session_id())
client.add_conversation(messages=msgs) Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- {field} must contain only non-empty strings
- {field} accepts at most {max_items} items, got {len(deduped)
- v3 MemoryClient requires non-empty {', '.join(missing)} at c
- service_id must be provided
- session_id must be a non-empty string
AI-assisted analysis of TencentCloud/TencentDB-Agent-Memory@3efcd317b8 (2026-09-01).
Data as JSON: /api/errors/70d0c4556617ba8a.
Report an issue: GitHub.