UserSyncUI / test_list_parse.py
harvesthealth's picture
Upload folder using huggingface_hub
f46c365 verified
import re
import json
import ast
def _coerce_to_list(llm_output:str):
if isinstance(llm_output, list):
return llm_output
# must make sure there's actually a list. Let's start with regex
match = re.search(r'\[.*\]', llm_output, flags=re.DOTALL)
if match:
try:
return json.loads(match.group(0))
except json.JSONDecodeError as e:
print("Failed standard load:", e)
try:
return ast.literal_eval(match.group(0))
except Exception as e2:
print("Failed ast load:", e2)
raise ValueError("Cannot convert the LLM output to a list.")
print(_coerce_to_list("""
```json
[
{"key": "value"}
]
```
"""))