Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

def extract_json_from_str(content: str) -> List[Dict[str, Any]]:
"""Extract JSON objects from a string. Supports backtick enclosed JSON objects"""
pattern = re.compile(r"```(?:\s*([\w\+\-]+))?\n([\s\S]*?)```")
pattern = re.compile(r"```(?:\s*([\w\+\-]+))?\r?\n([\s\S]*?)```")
matches = pattern.findall(content)
ret: List[Dict[str, Any]] = []
# If no matches found, assume the entire content is a JSON object
Expand Down
5 changes: 5 additions & 0 deletions python/packages/autogen-core/tests/test_json_extraction.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest

from autogen_core.utils import extract_json_from_str


Expand Down Expand Up @@ -72,6 +73,10 @@ def test_extract_json_from_str_codeblock() -> None:
multi_resp = extract_json_from_str(multi_json_str)
assert multi_resp == multi_json_resp

crlf_code_block_str = '```json\r\n{"name": "Alice", "age": 28, "city": "Seattle"}\r\n```'
crlf_resp = extract_json_from_str(crlf_code_block_str)
assert crlf_resp == code_block_resp

invalid_lang_code_block_str = """
```notjson
{
Expand Down