import re import json import ast def _coerce_to_dict_or_list(llm_output:str): if isinstance(llm_output, (dict, list)): return llm_output try: result = extract_json(llm_output) if result == {} and not (isinstance(llm_output, str) and ('{}' in llm_output or '{' in llm_output and '}' in llm_output)): raise ValueError("Cannot convert the LLM output to a dict or list value.") if not isinstance(result, (dict, list)): raise ValueError("Cannot convert the LLM output to a dict or list value.") return result except Exception: raise ValueError("Cannot convert the LLM output to a dict or list value.") def extract_json(text: str) -> dict: try: if isinstance(text, dict) or isinstance(text, list): return text filtered_text = "" filtered_text = re.sub(r'^.*?({|\[)', r'\1', text, flags=re.DOTALL) filtered_text = re.sub(r'(}|\])(?!.*(\]|\})).*$', r'\1', filtered_text, flags=re.DOTALL) filtered_text = re.sub("\\'", "'", filtered_text) filtered_text = re.sub("\\,", ",", filtered_text) try: parsed = json.loads(filtered_text, strict=False) except json.JSONDecodeError: try: parsed = ast.literal_eval(filtered_text) except Exception: return {} return parsed except Exception: return {} print("Test 1:", _coerce_to_dict_or_list("""```json\n{"key": "value"}\n```""")) print("Test 2:", _coerce_to_dict_or_list("""```json\n{"key": "value"}\n""")) print("Test 3:", _coerce_to_dict_or_list("""{"key": "value"}"""))