Skip to content

prepare_reference_data crashes on dict entries with missing or non-string id #2448

Closed as not planned

Description

@simpleqt

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.

Activity

  1. added
    ai:taskDispatched to AI coding agent | 已派发给 AI 编码任务
    area:coreMOS 编排层 / 框架底座 / 跨模块问题
    status:in-progressSomeone or AI is working on it | 人工或 AI 正在处理
    types:bugSomething isn't working | 功能异常
    on Sep 30, 2026
  2. self-assigned this
    on Sep 30, 2026
  3. Memtensor-AI commented on Sep 30, 2026

    @Memtensor-AI
    Collaborator

    🤖 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.

  4. Memtensor-AI commented on Sep 30, 2026

    @Memtensor-AI
    Collaborator

    ✅ 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: #2451
    Assigned to: @syzsunshine219
    Reviewers: @WeiminLee

  5. added
    ai:testingAI agent is running tests | AI 正在运行测试
    and removed
    ai:codingAI agent is coding | AI 正在编码
    on Sep 30, 2026
  6. github-actions commented on Nov 4, 2026

    @github-actions
    Contributor

    This issue has been automatically marked as stale due to inactivity.

  7. added
    status:staleAuto-marked stale after 30d inactivity; closes 7d later unless exempt.
    on Nov 4, 2026
  8. github-actions commented on Nov 14, 2026

    @github-actions
    Contributor

    This issue has been automatically closed due to inactivity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

ai:taskDispatched to AI coding agent | 已派发给 AI 编码任务ai:testingAI agent is running tests | AI 正在运行测试area:coreMOS 编排层 / 框架底座 / 跨模块问题status:in-progressSomeone or AI is working on it | 人工或 AI 正在处理status:staleAuto-marked stale after 30d inactivity; closes 7d later unless exempt.types:bugSomething isn't working | 功能异常

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions