MemOS icon indicating copy to clipboard operation
MemOS copied to clipboard

clean_json_response corrupts JSON values containing code fences

Open simpleqt opened this issue 2 months ago • 4 comments

Bug Description

clean_json_response in src/memos/mem_os/utils/format_utils.py strips markdown code fences with a global response.replace("```json", "").replace("```", ""). Any JSON string value that itself contains a fenced code block is silently corrupted before json.loads:

clean_json_response('```json\n{"text": "```py\\ncode```"}\n```')
# actual:   '{"text": "py\\ncode"}'          <- fences stripped from the value
# expected: '{"text": "```py\\ncode```"}'

Both api/handlers/chat_handler.py and api/handlers/suggestion_handler.py pipe LLM responses through this function before json.loads, so suggestions and chat metadata carrying markdown snippets come back with mangled content.

Expected Behavior

Only an opening fence (with its optional json info string) at the very start and a closing fence at the very end are markup; interior occurrences must be preserved.

Environment

MemOS main (a7367d0), python 3.14.

Additional Context

Fix + red/green-verified tests ready; PR to follow referencing this issue.

simpleqt avatar Sep 28 '26 07:09 simpleqt

🤖 AutoDev has picked up this issue and started working on it.

Task ID: d7dd51fe8ad9ac03 Working branch: bugfix/autodev-2420-20260928074010713 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.

Memtensor-AI avatar Sep 28 '26 07:09 Memtensor-AI

✅ AutoDev task d7dd51fe8ad9ac03 completed.

Summary: Fix issue #2420 — clean_json_response was globally stripping every occurrence via str.replace, corrupting JSON string values that legitimately contained fenced code blocks (e.g. {"text": "py\ncode```"}) before json.loads. Both chat_handler and suggestion_handler feed LLM output through this helper, so any suggestion or chat metadata carrying a markdown snippet was silently mangled.

The fix rewrites clean_json_response in src/memos/mem_os/utils/format_utils.py to recognise only the outermost fence: an anchored regex ^[^\n`]*\n matches an opening fence line (with optional info string like "json"), paired with a trailing at the very end. When both are present, only those delimiters are sliced off; interior fences are never candidates for removal. The None-input ValueError guard from #1525 is preserved.

Tests: added 4 red-then-green regression cases in tests/mem_os/test_format_utils.py covering wrapped/unwrapped payloads with interior py blocks and literal json snippets (including the exact reproduction from the issue). Full test run: 12/12 in test_format_utils.py green; 40/40 across tests/mem_os/ green; ruff check + format --check both clean. Commit 95dbd64e pushed to bugfix/autodev-2420-20260928074010713.

Base branch: main Branch: bugfix/autodev-2420-20260928074010713 Commit: 95dbd64 PR: https://github.com/MemTensor/MemOS/pull/2422 Assigned to: @syzsunshine219 Reviewers: @WeiminLee

Memtensor-AI avatar Sep 28 '26 07:09 Memtensor-AI

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

github-actions[bot] avatar Oct 31 '26 02:10 github-actions[bot]

This issue has been automatically closed due to inactivity.

github-actions[bot] avatar Nov 08 '26 02:11 github-actions[bot]