diff --git a/src/memos/mem_os/utils/format_utils.py b/src/memos/mem_os/utils/format_utils.py index 3991d0667..4e1f9d139 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,15 @@ 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. 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("\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 b97178784..6a99889db 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,58 @@ 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```"} + + 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```"}