fix: Strip markdown fences from Gemini JSON response to fix PDF curriculum extraction

This commit is contained in:
Hamza-Ayed
2026-08-29 00:14:43 +03:00
parent 5901329ef2
commit 03039a9611
+8
View File
@@ -209,9 +209,17 @@ if ($code !== 200 || empty($res)) {
$jsonResponse = json_decode($res, true);
$text = $jsonResponse['candidates'][0]['content']['parts'][0]['text'] ?? '';
// Clean up markdown fences if Gemini returned them
$text = preg_replace('/^```json\s*/i', '', $text);
$text = preg_replace('/```\s*$/i', '', $text);
$text = trim($text);
$parsedStructure = json_decode($text, true);
if (empty($parsedStructure) || empty($parsedStructure['units'])) {
// Log the actual text for debugging
file_put_contents($stateFile . '.debug.txt', $text);
updateState($stateFile, 'error', 0, 'فشل الذكاء الاصطناعي في إرجاع بنية JSON صالحة.');
return;
}