fix: Strip markdown fences from Gemini JSON response to fix PDF curriculum extraction
This commit is contained in:
@@ -209,9 +209,17 @@ if ($code !== 200 || empty($res)) {
|
|||||||
|
|
||||||
$jsonResponse = json_decode($res, true);
|
$jsonResponse = json_decode($res, true);
|
||||||
$text = $jsonResponse['candidates'][0]['content']['parts'][0]['text'] ?? '';
|
$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);
|
$parsedStructure = json_decode($text, true);
|
||||||
|
|
||||||
if (empty($parsedStructure) || empty($parsedStructure['units'])) {
|
if (empty($parsedStructure) || empty($parsedStructure['units'])) {
|
||||||
|
// Log the actual text for debugging
|
||||||
|
file_put_contents($stateFile . '.debug.txt', $text);
|
||||||
updateState($stateFile, 'error', 0, 'فشل الذكاء الاصطناعي في إرجاع بنية JSON صالحة.');
|
updateState($stateFile, 'error', 0, 'فشل الذكاء الاصطناعي في إرجاع بنية JSON صالحة.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user