factorstudios commited on
Commit
d3ef6c9
·
verified ·
1 Parent(s): 02812c3

Update base_agent.py

Browse files
Files changed (1) hide show
  1. base_agent.py +22 -2
base_agent.py CHANGED
@@ -4,6 +4,7 @@ import logging
4
  from datetime import datetime
5
  from pathlib import Path
6
  from typing import Any, Dict, Optional
 
7
  from openai import OpenAI
8
  from config import settings
9
 
@@ -74,10 +75,29 @@ class BaseAgent:
74
 
75
  # Parse output (assuming JSON format)
76
  try:
 
77
  output_data = json.loads(output_text)
78
  except json.JSONDecodeError:
79
- # If not JSON, wrap in a generic output
80
- output_data = {"output": output_text}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
82
  logger.info(f"{self.agent_name}: Processing completed")
83
 
 
4
  from datetime import datetime
5
  from pathlib import Path
6
  from typing import Any, Dict, Optional
7
+ import re
8
  from openai import OpenAI
9
  from config import settings
10
 
 
75
 
76
  # Parse output (assuming JSON format)
77
  try:
78
+ # Attempt direct JSON parsing first
79
  output_data = json.loads(output_text)
80
  except json.JSONDecodeError:
81
+ # If direct parsing fails, try to extract JSON from markdown or embedded text
82
+ json_match = re.search(r'```json\s*(.*?)\s*```', output_text, re.DOTALL)
83
+ if json_match:
84
+ try:
85
+ output_data = json.loads(json_match.group(1))
86
+ except json.JSONDecodeError:
87
+ # Fallback if markdown-wrapped JSON is malformed
88
+ output_data = {"output": output_text}
89
+ else:
90
+ # Try to find any JSON object embedded in the text
91
+ json_obj_match = re.search(r'(\{.*\})', output_text, re.DOTALL)
92
+ if json_obj_match:
93
+ try:
94
+ output_data = json.loads(json_obj_match.group(1))
95
+ except json.JSONDecodeError:
96
+ # Fallback if embedded JSON is malformed
97
+ output_data = {"output": output_text}
98
+ else:
99
+ # If no JSON found, wrap the entire output
100
+ output_data = {"output": output_text}
101
 
102
  logger.info(f"{self.agent_name}: Processing completed")
103