Skip to content

Latest commit

 

History

History
106 lines (90 loc) · 4.22 KB

File metadata and controls

106 lines (90 loc) · 4.22 KB

Exact Changes Made to Fix AI Agent JSON Output

File: backend/app/services/agents/resume_agent_service.py

Change 1: Enhanced Prompt Template

Method: ResumeIntelligenceAgent._get_prompt_template() Lines: 64-130

What Changed:

  • Replaced vague "Final Answer: " with explicit "Final Answer: [VALID JSON OBJECT ONLY - NO OTHER TEXT]"
  • Added 7-point "CRITICAL INSTRUCTIONS FOR FINAL ANSWER" section
  • Added 7-step "WORKFLOW" section
  • Key Addition: "ALWAYS use the analysis_formatter tool as your LAST action before Final Answer"
  • Key Addition: "Copy the exact JSON output from analysis_formatter tool into your Final Answer"
  • Added markdown code block handling instructions
  • Provided concrete JSON structure example

Change 2: Improved JSON Extraction

Method: ResumeAgentService._validate_and_parse_output() Lines: 396-450

What Changed:

  • Added logging: logger.info(f"Agent raw output (first 500 chars): {raw_text[:500]}")
  • Added markdown code block removal:
    if raw_text.startswith("```json"):
        raw_text = raw_text[7:].strip()
    if raw_text.startswith("```"):
        raw_text = raw_text[3:].strip()
    if raw_text.endswith("```"):
        raw_text = raw_text[:-3].strip()
  • Added logging in _extract_json(): logger.info(f"Extracted JSON (first 300 chars): {extracted[:300]}")
  • Added logging for each validation step: logger.info(f"Section '{section}' validated successfully")
  • Added final success logging: logger.info("Agent output successfully validated and parsed")

Change 3: Better Error Handling

Method: ResumeAgentService._execute_agent_analysis() Lines: 385-407

What Changed:

  • Wrapped validation in try-catch:
    if result['status'] == 'success':
        try:
            validated_output = self._validate_and_parse_output(...)
            result['output'] = validated_output
            logger.info("Agent output successfully validated and parsed")
        except ValueError as e:
            logger.error(f"Agent output validation failed: {e}")
            logger.info("Falling back to traditional NLP analysis")
            result['output'] = self._fallback_analysis(resume, target_role)
            result['status'] = 'fallback'
            result['validation_error'] = str(e)
  • This prevents validation errors from crashing the system
  • Gracefully falls back if JSON is invalid

Change 4: Clearer Input Instructions

Method: ResumeAgentService._execute_agent_analysis() Lines: 375-385

What Changed:

  • Replaced vague instructions with numbered workflow:
    REQUIRED WORKFLOW (follow these steps in order):
    1. Use resume_parser tool to extract resume data
    2. Use skill_extractor tool to analyze skills from the resume
    3. Use experience_analyzer tool to analyze career progression
    4. Use skill_gap_analyzer tool to identify gaps for the target role
    5. Use roadmap_generator tool to create a learning plan
    6. FINAL STEP: Use analysis_formatter tool with all the collected data
    7. Copy the exact JSON output from analysis_formatter into your Final Answer
    
    IMPORTANT: Your Final Answer MUST be ONLY the JSON output from analysis_formatter. Do not add any text before or after it.
    

Summary of Changes

Aspect Before After
Prompt clarity Ambiguous Explicit with 7-point instructions
analysis_formatter usage Optional REQUIRED as final step
JSON extraction Basic Handles markdown code blocks
Error handling Crashes on validation error Graceful fallback
Logging Minimal Detailed at each step
Input instructions Vague Numbered workflow with emphasis

Expected Impact

  • Agent now knows it MUST use analysis_formatter tool
  • Agent knows it MUST copy JSON output directly
  • JSON extraction handles more edge cases
  • Validation errors don't crash the system
  • Better debugging with detailed logs

Testing the Changes

  1. Restart backend with new code
  2. Upload resume and trigger analysis
  3. Check logs for:
    • "Agent raw output (first 500 chars):"
    • "Successfully parsed JSON with keys:"
    • "Agent output successfully validated and parsed"
  4. Check database: status should be success
  5. Frontend should display AI-generated analysis