Skip to content

A programming framework for agentic AI

Parse backtick-enclosed json (#6607)

peterychang1858799fa63e
diff --git a/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_magentic_one/_magentic_one_orchestrator.py b/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_magentic_one/_magentic_one_orchestrator.py
index e46ff46ff..2f1d2fd67 100644
--- a/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_magentic_one/_magentic_one_orchestrator.py
+++ b/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_magentic_one/_magentic_one_orchestrator.py
@@ -11,6 +11,7 @@ from autogen_core.models import (
LLMMessage,
UserMessage,
)
+from autogen_core.utils import extract_json_from_str
from .... import TRACE_LOGGER_NAME
from ....base import Response, TerminationCondition
@@ -325,7 +326,12 @@ class MagenticOneOrchestrator(BaseGroupChatManager):
ledger_str = response.content
try:
assert isinstance(ledger_str, str)
- progress_ledger = json.loads(ledger_str)
+ output_json = extract_json_from_str(ledger_str)
+ if len(output_json) != 1:
+ raise ValueError(
+ f"Progress ledger should contain a single JSON object, but found: {len(progress_ledger)}"
+ )
+ progress_ledger = output_json[0]
# If the team consists of a single agent, deterministically set the next speaker
if len(self._participant_names) == 1:
diff --git a/python/packages/autogen-core/src/autogen_core/utils/__init__.py b/python/packages/autogen-core/src/autogen_core/utils/__init__.py
index c5c0cfde5..e46b75697 100644
--- a/python/packages/autogen-core/src/autogen_core/utils/__init__.py
+++ b/python/packages/autogen-core/src/autogen_core/utils/__init__.py
@@ -1,3 +1,4 @@
from ._json_to_pydantic import schema_to_pydantic_model
+from ._load_json import extract_json_from_str
-__all__ = ["schema_to_pydantic_model"]
+__all__ = ["schema_to_pydantic_model", "extract_json_from_str"]
diff --git a/python/packages/autogen-core/src/autogen_core/utils/_load_json.py b/python/packages/autogen-core/src/autogen_core/utils/_load_json.py
new file mode 100644
index 000000000..95ccb0e7c
--- /dev/null
+++ b/python/packages/autogen-core/src/autogen_core/utils/_load_json.py
@@ -0,0 +1,20 @@
+import json
+import re
+from typing import Any, Dict, List
+
+
+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]*?)```")
+ matches = pattern.findall(content)
+ ret: List[Dict[str, Any]] = []
+ # If no matches found, assume the entire content is a JSON object
+ if not matches:
+ ret.append(json.loads(content))
+ for match in matches:
+ language = match[0].strip() if match[0] else None
+ if language and language.lower() != "json":
+ raise ValueError(f"Expected JSON object, but found language: {language}")
+ content = match[1]
+ ret.append(json.loads(content))
+ return ret
diff --git a/python/packages/autogen-core/tests/test_json_extraction.py b/python/packages/autogen-core/tests/test_json_extraction.py
new file mode 100644
index 000000000..f511d4097
--- /dev/null
+++ b/python/packages/autogen-core/tests/test_json_extraction.py
@@ -0,0 +1,85 @@
+import pytest
+from autogen_core.utils import extract_json_from_str
+
+
+def test_extract_json_from_str() -> None:
+ json_str = """
+ {
+ "name": "John",
+ "age": 30,
+ "city": "New York"
+ }
+ """
+ json_resp = [{"name": "John", "age": 30, "city": "New York"}]
+ resp = extract_json_from_str(json_str)
+ assert resp == json_resp
+
+ invalid_json_str = """
+ {
+ "name": "John",
+ "age": 30,
+ "city": "New York"
+ """
+ with pytest.raises(ValueError):
+ extract_json_from_str(invalid_json_str)
+
+
+def test_extract_json_from_str_codeblock() -> None:
+ code_block_lang_str = """
+ ```json
+ {
+ "name": "Alice",
+ "age": 28,
+ "city": "Seattle"
+ }
+ ```
+ """
+ code_block_no_lang_str = """
+ ```
+ {
+ "name": "Alice",
+ "age": 28,
+ "city": "Seattle"
+ }
+ ```
+ """
+ code_block_resp = [{"name": "Alice", "age": 28, "city": "Seattle"}]
+ multi_json_str = """
+ ```json
+ {
+ "name": "John",
+ "age": 30,
+ "city": "New York"
+ }
+ ```
+ ```json
+ {
+ "name": "Jane",
+ "age": 25,
+ "city": "Los Angeles"
+ }
+ ```
+ """
+ multi_json_resp = [
+ {"name": "John", "age": 30, "city": "New York"},
+ {"name": "Jane", "age": 25, "city": "Los Angeles"},
+ ]
+
+ lang_resp = extract_json_from_str(code_block_lang_str)
+ assert lang_resp == code_block_resp
+ no_lang_resp = extract_json_from_str(code_block_no_lang_str)
+ assert no_lang_resp == code_block_resp
+ multi_resp = extract_json_from_str(multi_json_str)
+ assert multi_resp == multi_json_resp
+
+ invalid_lang_code_block_str = """
+ ```notjson
+ {
+ "name": "Jane",
+ "age": 25,
+ "city": "Los Angeles"
+ }
+ ```
+ """
+ with pytest.raises(ValueError):
+ extract_json_from_str(invalid_lang_code_block_str)
diff --git a/python/packages/autogen-ext/src/autogen_ext/models/anthropic/_anthropic_client.py b/python/packages/autogen-ext/src/autogen_ext/models/anthropic/_anthropic_client.py
index d5f9d62b7..f583c1f4e 100644
--- a/python/packages/autogen-ext/src/autogen_ext/models/anthropic/_anthropic_client.py
+++ b/python/packages/autogen-ext/src/autogen_ext/models/anthropic/_anthropic_client.py
@@ -61,6 +61,7 @@ from autogen_core.models import (
validate_model_info,
)
from autogen_core.tools import Tool, ToolSchema
+from autogen_core.utils import extract_json_from_str
from pydantic import BaseModel, SecretStr
from typing_extensions import Self, Unpack
@@ -220,7 +221,10 @@ def assistant_message_to_anthropic(message: AssistantMessage) -> MessageParam:
args = __empty_content_to_whitespace(args)
if isinstance(args, str):
try:
- args_dict = json.loads(args)
+ json_objs = extract_json_from_str(args)
+ if len(json_objs) != 1:
+ raise ValueError(f"Expected a single JSON object, but found {len(json_objs)}")
+ args_dict = json_objs[0]
except json.JSONDecodeError:
args_dict = {"text": args}
else: