From 03039a961148cecbcd919b25b04774f1f31996a8 Mon Sep 17 00:00:00 2001 From: Hamza-Ayed Date: Sat, 29 Aug 2026 00:14:43 +0300 Subject: [PATCH] fix: Strip markdown fences from Gemini JSON response to fix PDF curriculum extraction --- backend/scripts/curriculum_worker.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/backend/scripts/curriculum_worker.php b/backend/scripts/curriculum_worker.php index d6dd403..b3c998c 100644 --- a/backend/scripts/curriculum_worker.php +++ b/backend/scripts/curriculum_worker.php @@ -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; }