Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(agent/json_utils): Make `extract_dict_from_response` more robust

Reinier van der Leer25b9e290a5f8
diff --git a/autogpts/autogpt/autogpt/json_utils/utilities.py b/autogpts/autogpt/autogpt/json_utils/utilities.py
index e5445dcb88..0521372e02 100644
--- a/autogpts/autogpt/autogpt/json_utils/utilities.py
+++ b/autogpts/autogpt/autogpt/json_utils/utilities.py
@@ -9,13 +9,11 @@ logger = logging.getLogger(__name__)
def extract_dict_from_response(response_content: str) -> dict[str, Any]:
# Sometimes the response includes the JSON in a code block with ```
- pattern = r"```([\s\S]*?)```"
+ pattern = r"```(?:json|JSON)*([\s\S]*?)```"
match = re.search(pattern, response_content)
if match:
response_content = match.group(1).strip()
- # Remove language names in code blocks
- response_content = response_content.lstrip("json")
else:
# The string may contain JSON.
json_pattern = r"{[\s\S]*}"