From 1bac0b90a605a0d9e381745fe11918b6d2d54bb0 Mon Sep 17 00:00:00 2001 From: Jeff Emmett Date: Tue, 10 Feb 2026 01:13:00 +0000 Subject: [PATCH] debug: improve AI analysis JSON parsing error logging Show more context in error messages to diagnose parse failures. Co-Authored-By: Claude Opus 4.6 --- backend/app/services/ai_analysis.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/backend/app/services/ai_analysis.py b/backend/app/services/ai_analysis.py index 00f0210..c81f4c3 100644 --- a/backend/app/services/ai_analysis.py +++ b/backend/app/services/ai_analysis.py @@ -254,7 +254,9 @@ async def _call_openai(system: str, user_prompt: str) -> str: ) response.raise_for_status() result = response.json() - return result["choices"][0]["message"]["content"] + content = result["choices"][0]["message"]["content"] + logger.info(f"OpenAI API response ({len(content)} chars): {content[:300]}") + return content def _parse_clips(content: str, video_duration: float) -> list[dict]: @@ -268,17 +270,21 @@ def _parse_clips(content: str, video_duration: float) -> list[dict]: logger.error(f"No JSON found in LLM response: {content[:200]}") return [] + raw_json = json_match.group() + logger.debug(f"Extracted JSON ({len(raw_json)} chars): {raw_json[:500]}") + try: - data = json.loads(json_match.group()) - except json.JSONDecodeError: + data = json.loads(raw_json) + except json.JSONDecodeError as e: + logger.warning(f"JSON parse attempt 1 failed: {e}") # Try to fix common JSON issues - fixed = json_match.group() + fixed = raw_json fixed = re.sub(r",\s*}", "}", fixed) fixed = re.sub(r",\s*]", "]", fixed) try: data = json.loads(fixed) - except json.JSONDecodeError: - logger.error(f"Failed to parse LLM JSON: {content[:200]}") + except json.JSONDecodeError as e2: + logger.error(f"Failed to parse LLM JSON ({e2}): {content[:500]}") return [] raw_clips = data.get("clips", [])