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 <noreply@anthropic.com>
This commit is contained in:
Jeff Emmett 2026-02-10 01:13:00 +00:00
parent 72efed481f
commit 1bac0b90a6
1 changed files with 12 additions and 6 deletions

View File

@ -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", [])