From 96670e355ee04be1d5dcb75914ee3666d0a393af Mon Sep 17 00:00:00 2001 From: Dillon Laird Date: Wed, 2 Oct 2024 12:33:56 -0700 Subject: [PATCH] better json parsing --- vision_agent/agent/agent_utils.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/vision_agent/agent/agent_utils.py b/vision_agent/agent/agent_utils.py index adb4a860..dc1127b1 100644 --- a/vision_agent/agent/agent_utils.py +++ b/vision_agent/agent/agent_utils.py @@ -48,11 +48,16 @@ def _strip_markdown_code(inp_str: str) -> str: def extract_json(json_str: str) -> Dict[str, Any]: json_str_mod = json_str.replace("\n", " ").strip() - json_str_mod = json_str_mod.replace("'", '"') json_str_mod = json_str_mod.replace(": True", ": true").replace( ": False", ": false" ) + # sometimes the json is in single quotes + try: + return json.loads(json_str_mod.replace("'", '"')) # type: ignore + except json.JSONDecodeError: + pass + try: return json.loads(json_str_mod) # type: ignore except json.JSONDecodeError: