prepare_reference_data crashes on dict entries with missing or non-string id
Bug Description
prepare_reference_data in src/memos/mem_os/utils/reference_utils.py handles TextualMemoryItem objects and raw dicts, but the dict branch assumes every key exists and that id is a string:
>>> prepare_reference_data([{"metadata": {"memory": "m"}}])
KeyError: 'id'
>>> prepare_reference_data([{"id": 12345, "memory": "m", "metadata": {}}])
AttributeError: 'int' object has no attribute 'split'
Both come from memory entries that are not TextualMemoryItem instances (e.g. already-serialized dicts from a cache or an MCP payload), and the exception escapes the streaming pipeline.
Expected Behavior
Dict entries should be normalized safely: a missing id should skip the ref_id derivation (or use the raw value), and a non-string id should be stringified before .split("-").
Environment
MemOS main, python 3.14.
Additional Context
Fix + red/green-verified test ready; PR to follow referencing this issue.
🤖 AutoDev has picked up this issue and started working on it.
Task ID: faf2339ea46ecf16
Working branch: bugfix/autodev-2448-20260930155922241
Target branch: latest dev* branch
Workflow: opsp (analysis → coding → testing → PR)
I will post the PR link here once done. If I need more information, I will ask in the comments.
✅ AutoDev task faf2339ea46ecf16 completed.
Summary: Fix #2448: prepare_reference_data no longer crashes on dict memory entries with a missing or non-string id.
Root cause: the dict branch of prepare_reference_data in src/memos/mem_os/utils/reference_utils.py assumed every entry carried an id key and that the id was a string, so cached search results / MCP payloads that had already been serialized would raise KeyError('id') or AttributeError: 'int' object has no attribute 'split', and the exception escaped the streaming pipeline. The TextualMemoryItem branch was unaffected because pydantic guarantees the invariant there.
Fix: normalize dict entries defensively — auto-create metadata if missing, skip ref_id derivation when id is absent (keeping the id slot explicit as None), and coerce non-string ids (int, uuid.UUID, ...) with str() before the prefix split. The original id value is preserved in metadata["id"] unchanged so downstream consumers can still round-trip it. The TextualMemoryItem branch is untouched.
Tests: added 9 regression cases in tests/mem_os/utils/test_reference_utils.py covering both crash repros from the issue, string / UUID / int id shapes, missing metadata, missing memory, and the TextualMemoryItem baseline. All 9 pass; the wider tests/mem_os/ suite (45 tests) also passes with no regressions. ruff format and ruff check are clean on the touched files. Local commit created and branch pushed to origin as bugfix/autodev-2448-20260930155922241 (commit 22d8e980).
Base branch: main
Branch: bugfix/autodev-2448-20260930155922241
Commit: 22d8e98
PR: https://github.com/MemTensor/MemOS/pull/2451
Assigned to: @syzsunshine219
Reviewers: @WeiminLee
This issue has been automatically marked as stale due to inactivity.
This issue has been automatically closed due to inactivity.