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: Improve parsing of JSON in LLM responses

Reinier van der Leer0b3aea92d197
diff --git a/autogpts/autogpt/autogpt/json_utils/utilities.py b/autogpts/autogpt/autogpt/json_utils/utilities.py
index 876106adeb..ff98a0bf5c 100644
--- a/autogpts/autogpt/autogpt/json_utils/utilities.py
+++ b/autogpts/autogpt/autogpt/json_utils/utilities.py
@@ -10,7 +10,10 @@ def extract_dict_from_response(response_content: str) -> dict[str, Any]:
# Sometimes the response includes the JSON in a code block with ```
if response_content.startswith("```") and response_content.endswith("```"):
# Discard the first and last ```, then re-join in case the response naturally included ```
- response_content = "```".join(response_content.split("```")[1:-1])
+ response_content = "```".join(response_content.split("```")[1:-1]).strip()
+
+ if (ob_pos := response_content.index("{")) > 0:
+ response_content = response_content[ob_pos:]
# response content comes from OpenAI as a Python `str(content_dict)`, literal_eval reverses this
try: