malformed deepseek response handling for formatted json

This commit is contained in:
2026-01-02 17:54:06 -07:00
parent d29ba781ba
commit 2a43b29ca9
4 changed files with 62 additions and 3 deletions

View File

@@ -310,10 +310,15 @@ class JournalPromptGenerator:
"""
Parse the AI response to extract new prompts.
Expected format: JSON array with keys "newprompt0" to "newpromptN" where N = num_prompts-1
Handles DeepSeek API responses that may include backticks and leading "json" string.
"""
# First, try to clean up the response content
cleaned_content = self._clean_ai_response(response_content)
try:
# Try to parse as JSON
data = json.loads(response_content)
data = json.loads(cleaned_content)
# Convert to list of prompt dictionaries
new_prompts = []
@@ -333,6 +338,7 @@ class JournalPromptGenerator:
self.console.print("[yellow]Warning: AI response is not valid JSON, attempting to extract prompts...[/yellow]")
self.console.print(f"[yellow]Full response content for debugging:[/yellow]")
self.console.print(f"[yellow]{response_content}[/yellow]")
self.console.print(f"[yellow]Cleaned content: {cleaned_content}[/yellow]")
# Look for patterns in the text
lines = response_content.strip().split('\n')
@@ -346,8 +352,55 @@ class JournalPromptGenerator:
}
new_prompts.append(prompt_obj)
# If still no prompts could be parsed, dump the full payload for debugging
if not new_prompts:
self.console.print("[red]Error: Could not extract any prompts from AI response[/red]")
self.console.print("[red]Full payload dump for debugging:[/red]")
self.console.print(f"[red]{response_content}[/red]")
return new_prompts
def _clean_ai_response(self, response_content: str) -> str:
"""
Clean up AI response content to handle common formatting issues from DeepSeek API.
Handles:
1. Leading/trailing backticks (```json ... ```)
2. Leading "json" string on its own line
3. Extra whitespace and newlines
"""
content = response_content.strip()
# Remove leading/trailing backticks (```json ... ```)
if content.startswith('```'):
# Find the first newline after the opening backticks
lines = content.split('\n')
if len(lines) > 1:
# Check if first line contains "json" or other language specifier
first_line = lines[0].strip()
if 'json' in first_line.lower() or first_line == '```':
# Remove the first line (```json or ```)
content = '\n'.join(lines[1:])
# Remove trailing backticks if present
if content.endswith('```'):
content = content[:-3].rstrip()
# Remove leading "json" string on its own line (case-insensitive)
lines = content.split('\n')
if len(lines) > 0:
first_line = lines[0].strip().lower()
if first_line == 'json':
content = '\n'.join(lines[1:])
# Also handle the case where "json" might be at the beginning of the first line
# but not the entire line (e.g., "json\n{...}")
content = content.strip()
if content.lower().startswith('json\n'):
content = content[4:].strip()
return content.strip()
def generate_prompts(self) -> List[Dict[str, str]]:
"""Generate new journal prompts using AI."""
self.console.print("\n[cyan]Generating new journal prompts...[/cyan]")