From bd93ef20293ca5139a21d3683644393cd88f3893 Mon Sep 17 00:00:00 2001 From: Lizandro Date: Wed, 15 Jul 2026 17:47:07 +0000 Subject: [PATCH] debug: show raw Gemini vision response in Telegram when JSON parse fails --- app/Services/GeminiVisionService.php | 17 ++++++++++++++--- app/Services/TelegramBotService.php | 7 ++++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/app/Services/GeminiVisionService.php b/app/Services/GeminiVisionService.php index 50b2754..09e3835 100644 --- a/app/Services/GeminiVisionService.php +++ b/app/Services/GeminiVisionService.php @@ -76,8 +76,14 @@ class GeminiVisionService $inputTokens = $response->json('usageMetadata.promptTokenCount', 0); $outputTokens = $response->json('usageMetadata.candidatesTokenCount', 0); - $text = $response->json('candidates.0.content.parts.0.text', ''); - $resultado = $this->parseRespuesta($text); + + // Collect text from ALL parts (newer models may put JSON in parts[1] if thinking is in parts[0]) + $parts = $response->json('candidates.0.content.parts', []); + $text = implode("\n", array_column(array_filter($parts, fn($p) => isset($p['text'])), 'text')); + + Log::info('[Gemini Vision] raw response: ' . substr($text, 0, 500)); + + $resultado = $this->parseRespuesta($text); AiUsageLog::registrar([ 'servicio' => 'gemini_vision', @@ -89,9 +95,14 @@ class GeminiVisionService 'resultado' => $resultado ? 'ok' : 'error', 'detalle' => $resultado ? ['banco' => $resultado['banco'], 'valor' => $resultado['valor']] - : ['raw' => substr($text, 0, 200), 'modelo' => $this->model], + : ['raw' => substr($text, 0, 400), 'modelo' => $this->model], ]); + // If parse failed, return debug info so caller can show it + if (! $resultado) { + return ['__parse_error' => substr($text, 0, 300)]; + } + return $resultado; } diff --git a/app/Services/TelegramBotService.php b/app/Services/TelegramBotService.php index 2b6b1ad..9692887 100644 --- a/app/Services/TelegramBotService.php +++ b/app/Services/TelegramBotService.php @@ -188,7 +188,12 @@ class TelegramBotService } if (isset($datosPago['__error'])) { - $this->send($chatId, "⚠️ Error al analizar el comprobante:\n`" . $datosPago['__error'] . "`"); + $this->send($chatId, "⚠️ Error de API:\n`" . $datosPago['__error'] . "`"); + return; + } + + if (isset($datosPago['__parse_error'])) { + $this->send($chatId, "⚠️ Gemini respondió pero no es JSON válido:\n`" . $datosPago['__parse_error'] . "`"); return; }