diff --git a/core/llm/parser.py b/core/llm/parser.py index c058c42f2..4d49b3d73 100644 --- a/core/llm/parser.py +++ b/core/llm/parser.py @@ -35,8 +35,7 @@ class MultiCodeBlockParser: """ def __init__(self): - # FIXME: ``` should be the only content on the line` - self.pattern = re.compile(r"```([a-z0-9]+\n)?(.*?)```\s*", re.DOTALL) + self.pattern = re.compile(r"^```([a-z0-9]+\n)?(.*?)^```\s*", re.DOTALL | re.MULTILINE) def __call__(self, text: str) -> list[str]: blocks = [] diff --git a/tests/llm/test_parser.py b/tests/llm/test_parser.py index 3ed3dbebf..fcc6ce9ab 100644 --- a/tests/llm/test_parser.py +++ b/tests/llm/test_parser.py @@ -15,6 +15,7 @@ ("```\n```", [""]), ("```py\n```", [""]), ("```py\nsome code\n```", ["some code"]), + ("```py\nsome ``` code\n```", ["some ``` code"]), ( "some text preamble\n" "```\nsome code\n```\n" "```py\nmore\ncode\n```\n" "some text conclusion", ["some code", "more\ncode"], @@ -49,7 +50,7 @@ def test_code_block_parser(input, expected): [ ("{}", True, {}), ('{"a": 1}', True, {"a": 1}), - ('```json\n{"a": 1, "b": "c"}```', True, {"a": 1, "b": "c"}), + ('```json\n{"a": 1, "b": "c"}\n```', True, {"a": 1, "b": "c"}), ("", True, ValueError), ("", False, None), ("{bad json}", True, ValueError), @@ -85,7 +86,7 @@ def test_parse_json_no_spec(input, strict, expected): }, ), ( - '```{"name": "John", "children": [{"age": 1, "name": "Jane", "geo": [1.0, 2.0]}]}```', + '```\n{"name": "John", "children": [{"age": 1, "name": "Jane", "geo": [1.0, 2.0]}]}\n```', { "name": "John", "children": [