#2420·MemOS

clean_json_response corrupts JSON values containing code fences

Author: simpleqtCreated Sep 28, 2026Updated Sep 28, 2026
Labelstypes:bugai:taskarea:coreai:testingstatus:in-progress

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:

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