pavanmutha commited on
Commit
c874a5d
·
verified ·
1 Parent(s): f32617a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -7
app.py CHANGED
@@ -100,18 +100,24 @@ import ast
100
 
101
  def extract_json_from_codeagent_output(raw_output):
102
  try:
103
- # Case 1: Already a dictionary
104
  if isinstance(raw_output, dict):
 
 
 
 
 
 
105
  return raw_output
106
 
107
- # Case 2: Attempt direct JSON parsing
108
  if isinstance(raw_output, str):
109
  try:
110
  return json.loads(raw_output)
111
  except json.JSONDecodeError:
112
- pass # fallback to code block extraction
113
 
114
- # Case 3: Extract code blocks and search for JSON-like structures
115
  code_blocks = re.findall(r"```(?:json|py|python)?\n([\s\S]*?)```", raw_output, re.DOTALL)
116
 
117
  for block in code_blocks:
@@ -119,7 +125,8 @@ def extract_json_from_codeagent_output(raw_output):
119
  r"print\(\s*json\.dumps\(\s*(\{[\s\S]*?\})\s*\)\s*\)",
120
  r"json\.dumps\(\s*(\{[\s\S]*?\})\s*\)",
121
  r"result\s*=\s*(\{[\s\S]*?\})",
122
- r"final_answer\s*\(\s*(\{[\s\S]*?\})\s*\)"
 
123
  ]:
124
  match = re.search(pattern, block, re.DOTALL)
125
  if match:
@@ -128,7 +135,7 @@ def extract_json_from_codeagent_output(raw_output):
128
  except json.JSONDecodeError:
129
  return ast.literal_eval(match.group(1))
130
 
131
- # Case 4: Fallback match the first top-level dict-like structure in raw_output
132
  fallback = re.search(r"\{[\s\S]+?\}", raw_output)
133
  if fallback:
134
  try:
@@ -139,7 +146,7 @@ def extract_json_from_codeagent_output(raw_output):
139
  except Exception as e:
140
  print(f"[extract_json] Error: {e}")
141
 
142
- # Final fallback structured error response
143
  return {"error": "Failed to extract structured JSON"}
144
 
145
 
 
100
 
101
  def extract_json_from_codeagent_output(raw_output):
102
  try:
103
+ # Case 1: If it's already a dict
104
  if isinstance(raw_output, dict):
105
+ # If there's a stringified JSON inside a dict key like 'output'
106
+ if "output" in raw_output and isinstance(raw_output["output"], str):
107
+ try:
108
+ return json.loads(raw_output["output"])
109
+ except json.JSONDecodeError:
110
+ pass # Not JSON inside
111
  return raw_output
112
 
113
+ # Case 2: Try parsing the whole string as JSON
114
  if isinstance(raw_output, str):
115
  try:
116
  return json.loads(raw_output)
117
  except json.JSONDecodeError:
118
+ pass # fallback to deeper extraction
119
 
120
+ # Case 3: Extract code blocks (supports json/py/python/empty labels)
121
  code_blocks = re.findall(r"```(?:json|py|python)?\n([\s\S]*?)```", raw_output, re.DOTALL)
122
 
123
  for block in code_blocks:
 
125
  r"print\(\s*json\.dumps\(\s*(\{[\s\S]*?\})\s*\)\s*\)",
126
  r"json\.dumps\(\s*(\{[\s\S]*?\})\s*\)",
127
  r"result\s*=\s*(\{[\s\S]*?\})",
128
+ r"final_answer\s*\(\s*(\{[\s\S]*?\})\s*\)",
129
+ r"^(\{[\s\S]*\})$" # Direct raw JSON block
130
  ]:
131
  match = re.search(pattern, block, re.DOTALL)
132
  if match:
 
135
  except json.JSONDecodeError:
136
  return ast.literal_eval(match.group(1))
137
 
138
+ # Case 4: Final fallback - any dict-like structure anywhere in output
139
  fallback = re.search(r"\{[\s\S]+?\}", raw_output)
140
  if fallback:
141
  try:
 
146
  except Exception as e:
147
  print(f"[extract_json] Error: {e}")
148
 
149
+ # Case 5: If everything fails
150
  return {"error": "Failed to extract structured JSON"}
151
 
152