agentscope-ai/agentscope · error · ValueError
record.user_id does not match the given user_id.
Error message
record.user_id does not match the given user_id.
What it means
Raised by SQLStorage.upsert_knowledge_base when the user_id argument and record.user_id differ. The KB record's owner must match the caller-supplied user id to prevent cross-user writes.
Source
Thrown at src/agentscope/app/storage/_sql/_storage.py:1717
in one transaction (see :meth:`_delete_team_impl`).
"""
async with self._session() as sess:
ok = await self._delete_team_impl(sess, user_id, team_id)
await sess.commit()
return ok
# ------------------------------------------------------------------
# Knowledge bases
# ------------------------------------------------------------------
async def upsert_knowledge_base(
self,
user_id: str,
record: KnowledgeBaseRecord,
) -> KnowledgeBaseRecord:
"""Create or update a KB record; enforces ``record.user_id``."""
if record.user_id != user_id:
raise ValueError(
"record.user_id does not match the given user_id.",
)
await self._write_row(KnowledgeBaseRow, record)
return record
async def get_knowledge_base(
self,
user_id: str,
knowledge_base_id: str,
) -> KnowledgeBaseRecord | None:
"""One KB; owner-scoped."""
async with self._session() as sess:
row = await sess.get(KnowledgeBaseRow, knowledge_base_id)
if row is None or row.user_id != user_id:
return None
return _to_record(row, KnowledgeBaseRecord)
async def list_knowledge_bases(View on GitHub (pinned to e90f1c7592)
Solutions
- Set record.user_id = user_id before calling upsert_knowledge_base
- If the record is intentionally being transferred, update user_id explicitly first
- Audit serialization/deserialization of KB records to ensure user_id survives round-trips
Example fix
// before kb = KnowledgeBaseRecord(name="docs") await storage.upsert_knowledge_base(uid, kb) // after kb = KnowledgeBaseRecord(name="docs", user_id=uid) await storage.upsert_knowledge_base(uid, kb)
Defensive patterns
Strategy: validation
Validate before calling
if record.user_id != user_id:\n record.user_id = user_id # or raise before hitting storage
Type guard
def owned_by(rec, uid: str) -> bool:\n return rec.user_id == uid
Try / catch
try:\n await storage.upsert_knowledge_base(uid, rec)\nexcept ValueError as e:\n if \"user_id does not match\" in str(e): rec.user_id = uid; await storage.upsert_knowledge_base(uid, rec)\n else: raise
Prevention
- Always construct records with user_id set from the request context
- Unit-test ownership fields in storage wrappers
When it happens
Trigger: Calling upsert_knowledge_base(user_id, record) with record.user_id unset (None) or set to another user's id.
Common situations: Creating a KnowledgeBaseRecord without setting user_id and expecting the storage to fill it in; copying KB records between users; refactors that changed where user_id is populated.
Related errors
- Invalid logging level: {level}. Must be one of 'INFO', 'DEBU
- The 'reserve_ratio' of the context config must be smaller th
- The 'context_buffer_ratio' of the injection config must be s
- Input validation failed for tool '{tool_call.name}': {e.mess
- The injection template must contain the '{runtime_state}' pl
AI-assisted analysis of agentscope-ai/agentscope@e90f1c7592 (2026-08-28).
Data as JSON: /api/errors/c6cc9311970fa434.
Report an issue: GitHub.