From 95dbd64ec0ac7b495b7f7bbf058cb6858670120d Mon Sep 17 00:00:00 2001 From: MemTensor AutoDev Date: Mon, 28 Sep 2026 15:57:04 +0800 Subject: [PATCH 1/2] fix(format_utils): preserve interior code fences in clean_json_response The old implementation stripped every ``` occurrence globally, corrupting JSON string values that legitimately contained fenced code blocks (e.g. {"text": "```py\ncode```"}) before json.loads. Both chat_handler and suggestion_handler pipe LLM output through this helper, so any payload carrying a markdown snippet came back mangled. Recognise only the outermost fence: anchor a regex at the start of the trimmed response for the opening line (``` + optional info string + \n), pair it with a trailing ``` at the very end, and slice off exactly those delimiters. Interior fences are never candidates for removal. Adds 4 red-then-green regression tests covering wrapped/unwrapped payloads with interior ```py blocks and literal ```json snippets. Refs: #2420 --- src/memos/mem_os/utils/format_utils.py | 22 +++++++++++++-- tests/mem_os/test_format_utils.py | 39 ++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/src/memos/mem_os/utils/format_utils.py b/src/memos/mem_os/utils/format_utils.py index 3991d0667..3803bd293 100644 --- a/src/memos/mem_os/utils/format_utils.py +++ b/src/memos/mem_os/utils/format_utils.py @@ -1,5 +1,6 @@ import math import random +import re from typing import Any @@ -1394,11 +1395,16 @@ def clean_json_response(response: str) -> str: """ Remove markdown JSON code block formatting from LLM response. + Only an opening fence (with optional ``json`` info string) at the very + start of the response and a closing fence at the very end are treated as + markdown wrappers; any ``` occurrences inside JSON string values are + preserved so payloads carrying embedded code fences survive intact. + Args: - response: Raw response string that may contain ```json and ``` + response: Raw response string that may be wrapped in ```json ... ```. Returns: - str: Clean JSON string without markdown formatting + str: Clean JSON string without the outer markdown fence. Raises: ValueError: If ``response`` is None. This is almost always an upstream @@ -1413,4 +1419,14 @@ def clean_json_response(response: str) -> str: "failed silently (check timed_with_status / generate() error " "handling)." ) - return response.replace("```json", "").replace("```", "").strip() + + stripped = response.strip() + + # Opening fence: ``` optionally followed by an info string like "json" + # on the same line, then a newline. We only strip the fence when a + # matching closing fence is present at the end of the string. + open_match = re.match(r"^```[^\n`]*\n", stripped) + if open_match and stripped.endswith("```"): + stripped = stripped[open_match.end() : -len("```")] + + return stripped.strip() diff --git a/tests/mem_os/test_format_utils.py b/tests/mem_os/test_format_utils.py index b97178784..73f4d96d3 100644 --- a/tests/mem_os/test_format_utils.py +++ b/tests/mem_os/test_format_utils.py @@ -2,9 +2,11 @@ Test suite for src/memos/mem_os/utils/format_utils.py Focus: clean_json_response function defensive behavior -Related issue: #1525 +Related issues: #1525, #2420 """ +import json + import pytest from memos.mem_os.utils.format_utils import clean_json_response @@ -63,13 +65,44 @@ def test_clean_json_response_with_complex_json(self): ```""" result = clean_json_response(input_str) assert "```json" not in result - assert "```" not in result assert '"queries"' in result assert result.strip() == result # No leading/trailing whitespace def test_clean_json_response_preserves_internal_backticks(self): - """Test that backticks inside JSON content are preserved.""" + """Test that single backticks inside JSON content are preserved.""" input_str = '```json\n{"code": "`example`"}\n```' result = clean_json_response(input_str) assert "`example`" in result assert result.count("`") == 2 # Only internal backticks remain + + def test_clean_json_response_preserves_interior_code_fence(self): + """Regression for issue #2420. + + A JSON string value that itself contains a fenced ```py ... ``` block + must survive untouched — only the outer ```json / ``` wrapper is markup. + """ + input_str = '```json\n{"text": "```py\\ncode```"}\n```' + result = clean_json_response(input_str) + parsed = json.loads(result) + assert parsed == {"text": "```py\ncode```"} + + def test_clean_json_response_preserves_interior_code_fence_no_wrapper(self): + """Bare JSON containing an interior ``` fence must be returned as-is.""" + input_str = '{"text": "```py\\ncode```"}' + result = clean_json_response(input_str) + parsed = json.loads(result) + assert parsed == {"text": "```py\ncode```"} + + def test_clean_json_response_preserves_interior_json_fence(self): + """A JSON value quoting the literal string ```json must survive.""" + input_str = '```json\n{"snippet": "see ```json blocks```"}\n```' + result = clean_json_response(input_str) + parsed = json.loads(result) + assert parsed == {"snippet": "see ```json blocks```"} + + def test_clean_json_response_leaves_unwrapped_response_untouched(self): + """Response with no wrapping fence at all is only whitespace-stripped.""" + input_str = ' {"answer": "```code```"} ' + result = clean_json_response(input_str) + parsed = json.loads(result) + assert parsed == {"answer": "```code```"} From 46ed407efc3e7d391de86282b645ccac137d35d1 Mon Sep 17 00:00:00 2001 From: MemTensor AutoDev Date: Mon, 28 Sep 2026 16:11:47 +0800 Subject: [PATCH 2/2] fix(format_utils): require newline before closing fence MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Tighten clean_json_response so the closing markdown fence must appear on its own line ('\n```' at end). The prior endswith('```') check was too permissive: any trailing triple-backtick — including one that belongs to a JSON string value — could be interpreted as a closing fence and stripped, corrupting the payload. Adds a regression test covering '```json\n{"a": "x```"}\n```' to lock in that interior code fences survive when the wrapper uses a proper on-its-own-line closing fence. Addresses Open Code Review finding on PR #2422. --- src/memos/mem_os/utils/format_utils.py | 9 +++++---- tests/mem_os/test_format_utils.py | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/memos/mem_os/utils/format_utils.py b/src/memos/mem_os/utils/format_utils.py index 3803bd293..4e1f9d139 100644 --- a/src/memos/mem_os/utils/format_utils.py +++ b/src/memos/mem_os/utils/format_utils.py @@ -1423,10 +1423,11 @@ def clean_json_response(response: str) -> str: stripped = response.strip() # Opening fence: ``` optionally followed by an info string like "json" - # on the same line, then a newline. We only strip the fence when a - # matching closing fence is present at the end of the string. + # on the same line, then a newline. The closing fence must appear on its + # own line (preceded by a newline) — this prevents a trailing ``` that + # belongs to a JSON string value from being mistaken for a fence. open_match = re.match(r"^```[^\n`]*\n", stripped) - if open_match and stripped.endswith("```"): - stripped = stripped[open_match.end() : -len("```")] + if open_match and stripped.endswith("\n```"): + stripped = stripped[open_match.end() : -len("\n```")] return stripped.strip() diff --git a/tests/mem_os/test_format_utils.py b/tests/mem_os/test_format_utils.py index 73f4d96d3..6a99889db 100644 --- a/tests/mem_os/test_format_utils.py +++ b/tests/mem_os/test_format_utils.py @@ -106,3 +106,17 @@ def test_clean_json_response_leaves_unwrapped_response_untouched(self): result = clean_json_response(input_str) parsed = json.loads(result) assert parsed == {"answer": "```code```"} + + def test_clean_json_response_requires_closing_fence_on_own_line(self): + """A trailing ``` must be preceded by a newline to count as a fence. + + Guards against a JSON string value that legitimately ends with + triple-backticks being mistaken for a closing fence — only a fence + on its own line (``\\n``` ``) is recognised. + """ + # Opening fence + JSON whose last value ends in ``` + closing fence + # on its own line: interior fence must survive intact. + input_str = '```json\n{"a": "x```"}\n```' + result = clean_json_response(input_str) + parsed = json.loads(result) + assert parsed == {"a": "x```"}