fix: add diagnostic logging and increase scaffold max_tokens to 2048

The "Unterminated string" JSON parse error is likely caused by Gemini
output truncation at 1024 tokens. Increases scaffold max_tokens to 2048
and adds logging for: raw response text, finish_reason (truncation
detection), and JSON parse failures.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit was merged in pull request #93.
This commit is contained in:
chihlasm
2026-02-27 00:15:01 -05:00
parent 957f13b993
commit 6fc76187c0
2 changed files with 26 additions and 1 deletions

View File

@@ -5,10 +5,13 @@ Supports Gemini (google-genai) and Anthropic (anthropic) as interchangeable
backends for JSON generation used by the AI Flow Builder.
"""
import logging
from abc import ABC, abstractmethod
from app.core.config import settings
logger = logging.getLogger(__name__)
class AIProvider(ABC):
"""Abstract base class for AI providers."""
@@ -74,6 +77,16 @@ class GeminiProvider(AIProvider):
config=config,
)
# Log finish reason to detect truncation
if response.candidates:
finish_reason = getattr(response.candidates[0], "finish_reason", None)
logger.info("Gemini finish_reason=%s model=%s", finish_reason, self._model)
if str(finish_reason) == "MAX_TOKENS":
logger.warning(
"Gemini output truncated (MAX_TOKENS). max_output_tokens=%d",
max_tokens,
)
text = response.text or ""
input_tokens = getattr(response.usage_metadata, "prompt_token_count", 0) or 0
output_tokens = (