| 413 | |
| 414 | |
| 415 | def parse_json_from_llm(text: str) -> dict: |
| 416 | match = re.search(r"```json\s*(.*?)\s*```", text, re.DOTALL | re.IGNORECASE) |
| 417 | if match: |
| 418 | text = match.group(1) |
| 419 | else: |
| 420 | match = re.search(r"```\s*(.*?)\s*```", text, re.DOTALL) |
| 421 | if match: |
| 422 | text = match.group(1) |
| 423 | text = text.strip() |
| 424 | try: |
| 425 | return json.loads(text) |
| 426 | except json.JSONDecodeError: |
| 427 | match = re.search(r"(\{.*\})", text, re.DOTALL) |
| 428 | if match: |
| 429 | try: |
| 430 | return json.loads(match.group(1)) |
| 431 | except json.JSONDecodeError: |
| 432 | pass |
| 433 | raise ValueError(f"Could not parse valid JSON from LLM response:\n{text}") |
| 434 | |
| 435 | |
| 436 | def parse_node(node, nodes_dict): |