You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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 95dbd64 pushed to bugfix/autodev-2420-20260928074010713.
Base branch:main Branch:bugfix/autodev-2420-20260928074010713 Commit:95dbd64 PR:#2422 Assigned to:@syzsunshine219 Reviewers:@WeiminLee
Activity
Memtensor-AI commentedon Sep 28, 2026
🤖 AutoDev has picked up this issue and started working on it.
Task ID:
d7dd51fe8ad9ac03Working branch:
bugfix/autodev-2420-20260928074010713Target branch:
latest dev* branchWorkflow: 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.
fix(format_utils): preserve interior code fences in clean_json_response
Memtensor-AI commentedon Sep 28, 2026
✅ AutoDev task
d7dd51fe8ad9ac03completed.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 trailingat 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 literaljson 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 95dbd64 pushed to bugfix/autodev-2420-20260928074010713.Base branch:
mainBranch:
bugfix/autodev-2420-20260928074010713Commit:
95dbd64PR: #2422
Assigned to: @syzsunshine219
Reviewers: @WeiminLee
fix(mem_os): strip only anchored markdown fences in clean_json_respon…
github-actions commentedon Oct 31, 2026
This issue has been automatically marked as stale due to inactivity.
fix(mem_os): strip only anchored markdown fences in clean_json_respon…
github-actions commentedon Nov 8, 2026
This issue has been automatically closed due to inactivity.