Spaces:
Sleeping
Sleeping
Fix: Handle None values in LLM-generated JSON
Browse files- student/code_generator.py +20 -3
student/code_generator.py
CHANGED
|
@@ -211,17 +211,24 @@ Generate ONLY the JSON output, no other text. Ensure all code is complete and fu
|
|
| 211 |
|
| 212 |
json_match = re.search(r"```json\s*(\{.*?\})\s*```", content, re.DOTALL)
|
| 213 |
if json_match:
|
| 214 |
-
|
|
|
|
|
|
|
| 215 |
|
| 216 |
# Try to parse the whole content as JSON
|
| 217 |
try:
|
| 218 |
-
|
|
|
|
|
|
|
| 219 |
except json.JSONDecodeError:
|
| 220 |
# Try to find any JSON object
|
| 221 |
json_match = re.search(r"\{.*\}", content, re.DOTALL)
|
| 222 |
if json_match:
|
| 223 |
-
|
|
|
|
|
|
|
| 224 |
|
|
|
|
| 225 |
raise ValueError("Could not extract JSON from LLM response")
|
| 226 |
|
| 227 |
def _get_fallback_template(self) -> dict[str, str]:
|
|
@@ -271,6 +278,16 @@ MIT License
|
|
| 271 |
output_dir.mkdir(parents=True, exist_ok=True)
|
| 272 |
|
| 273 |
for filename, content in files.items():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
file_path = output_dir / filename
|
| 275 |
file_path.write_text(content, encoding="utf-8")
|
| 276 |
logger.debug(f"Saved {filename} ({len(content)} bytes)")
|
|
|
|
| 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 parse the whole content as JSON
|
| 219 |
try:
|
| 220 |
+
result = json.loads(content)
|
| 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))
|
| 228 |
+
# Validate that all values are strings
|
| 229 |
+
return {k: v for k, v in result.items() if v is not None}
|
| 230 |
|
| 231 |
+
logger.error(f"Could not extract JSON from LLM response: {content[:200]}")
|
| 232 |
raise ValueError("Could not extract JSON from LLM response")
|
| 233 |
|
| 234 |
def _get_fallback_template(self) -> dict[str, str]:
|
|
|
|
| 278 |
output_dir.mkdir(parents=True, exist_ok=True)
|
| 279 |
|
| 280 |
for filename, content in files.items():
|
| 281 |
+
# Skip None or empty content
|
| 282 |
+
if content is None:
|
| 283 |
+
logger.warning(f"Skipping {filename} - content is None")
|
| 284 |
+
continue
|
| 285 |
+
|
| 286 |
+
# Ensure content is a string
|
| 287 |
+
if not isinstance(content, str):
|
| 288 |
+
logger.warning(f"Converting {filename} content to string")
|
| 289 |
+
content = str(content)
|
| 290 |
+
|
| 291 |
file_path = output_dir / filename
|
| 292 |
file_path.write_text(content, encoding="utf-8")
|
| 293 |
logger.debug(f"Saved {filename} ({len(content)} bytes)")
|