From 956cdc77fa8eb00a98776bacd75f07fad63f3e01 Mon Sep 17 00:00:00 2001 From: Reinier van der Leer Date: Wed, 31 Jan 2024 14:15:02 +0100 Subject: [PATCH] fix(agent/json_utils): Decode as JSON rather than Python objects * Replace `ast.literal_eval` with `json.loads` in `extract_dict_from_response` This fixes a bug where boolean values could not be decoded because of their required capitalization in Python. --- autogpts/autogpt/autogpt/json_utils/utilities.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/autogpts/autogpt/autogpt/json_utils/utilities.py b/autogpts/autogpt/autogpt/json_utils/utilities.py index 0521372e02..2bc0eb2c87 100644 --- a/autogpts/autogpt/autogpt/json_utils/utilities.py +++ b/autogpts/autogpt/autogpt/json_utils/utilities.py @@ -1,5 +1,5 @@ """Utilities for the json_fixes package.""" -import ast +import json import logging import re from typing import Any @@ -22,9 +22,7 @@ def extract_dict_from_response(response_content: str) -> dict[str, Any]: if match: response_content = match.group() - # Response content comes from OpenAI as a Python `str(content_dict)`. - # `literal_eval` does the reverse of `str(dict)`. - result = ast.literal_eval(response_content) + result = json.loads(response_content) if not isinstance(result, dict): raise ValueError( f"Response '''{response_content}''' evaluated to "