Spaces:
Sleeping
Sleeping
Fix: Use greedy regex for JSON extraction from markdown
Browse files- student/code_generator.py +10 -3
student/code_generator.py
CHANGED
|
@@ -206,10 +206,17 @@ Generate ONLY the JSON output, no other text. Ensure all code is complete and fu
|
|
| 206 |
Returns:
|
| 207 |
Parsed JSON dictionary
|
| 208 |
"""
|
| 209 |
-
# Try to find JSON in markdown code block
|
| 210 |
import re
|
| 211 |
|
| 212 |
-
json_match = re.search(r"```json\s*(\{.*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
if json_match:
|
| 214 |
result = json.loads(json_match.group(1))
|
| 215 |
# Validate that all values are strings
|
|
@@ -221,7 +228,7 @@ Generate ONLY the JSON output, no other text. Ensure all code is complete and fu
|
|
| 221 |
# Validate that all values are strings
|
| 222 |
return {k: v for k, v in result.items() if v is not None}
|
| 223 |
except json.JSONDecodeError:
|
| 224 |
-
# Try to find any JSON object
|
| 225 |
json_match = re.search(r"\{.*\}", content, re.DOTALL)
|
| 226 |
if json_match:
|
| 227 |
result = json.loads(json_match.group(0))
|
|
|
|
| 206 |
Returns:
|
| 207 |
Parsed JSON dictionary
|
| 208 |
"""
|
| 209 |
+
# Try to find JSON in markdown code block (greedy match for nested braces)
|
| 210 |
import re
|
| 211 |
|
| 212 |
+
json_match = re.search(r"```json\s*(\{.*\})\s*```", content, re.DOTALL)
|
| 213 |
+
if json_match:
|
| 214 |
+
result = json.loads(json_match.group(1))
|
| 215 |
+
# Validate that all values are strings
|
| 216 |
+
return {k: v for k, v in result.items() if v is not None}
|
| 217 |
+
|
| 218 |
+
# Try to find JSON in plain code block
|
| 219 |
+
json_match = re.search(r"```\s*(\{.*\})\s*```", content, re.DOTALL)
|
| 220 |
if json_match:
|
| 221 |
result = json.loads(json_match.group(1))
|
| 222 |
# Validate that all values are strings
|
|
|
|
| 228 |
# Validate that all values are strings
|
| 229 |
return {k: v for k, v in result.items() if v is not None}
|
| 230 |
except json.JSONDecodeError:
|
| 231 |
+
# Try to find any JSON object (greedy match)
|
| 232 |
json_match = re.search(r"\{.*\}", content, re.DOTALL)
|
| 233 |
if json_match:
|
| 234 |
result = json.loads(json_match.group(0))
|