debug: show raw Gemini vision response in Telegram when JSON parse fails

This commit is contained in:
Lizandro
2026-07-15 17:47:07 +00:00
parent 2acb80a7d2
commit bd93ef2029
2 changed files with 20 additions and 4 deletions
+14 -3
View File
@@ -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;
}
+6 -1
View File
@@ -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;
}