From 023b08eea2769f257b897d539d4e6241e84a2c7d Mon Sep 17 00:00:00 2001 From: hbrain Date: Sat, 30 May 2026 01:55:56 +0200 Subject: [PATCH] Fix: Handle multiple description candidates from Gemini --- lib/generate_data_sync.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/generate_data_sync.php b/lib/generate_data_sync.php index c0516ff..c6033aa 100644 --- a/lib/generate_data_sync.php +++ b/lib/generate_data_sync.php @@ -74,7 +74,17 @@ $log = implode("\n", $out_lines); $gfile = dirname($jobdir) . '/' . basename($jobdir) . '.json'; if (is_file($gfile)) { $raw = @file_get_contents($gfile); - $json = @json_decode($raw, true); + $json_data = @json_decode($raw, true); + + // Handle case where Gemini returns an array of candidates or a single object. + // We will always take the first valid description object we can find. + $json = null; + if (is_array($json_data) && isset($json_data[0]) && is_array($json_data[0])) { + $json = $json_data[0]; // It's an array of descriptions, take the first. + } elseif (is_array($json_data)) { + $json = $json_data; // It's already a single description object. + } + $description = ''; $teaser = ''; $tags = []; @@ -83,12 +93,13 @@ if (is_file($gfile)) { $teaser = isset($json['teaser']) ? (string)$json['teaser'] : ''; $tags = isset($json['tags']) && is_array($json['tags']) ? $json['tags'] : []; } + echo json_encode([ 'status' => 'ok', 'description' => $description, 'teaser' => $teaser, 'tags' => $tags, - 'raw_json' => $json, + 'raw_json' => $json_data, // Send original raw data back for reference 'log' => $log, 'rc' => $rc, ], JSON_UNESCAPED_UNICODE);