from fastapi import APIRouter from pydantic import BaseModel from app.helpers.extract_latest_message import extract_latest_message from app.helpers.clean_email_html import clean_email_html from app.helpers.remove_disclaimer import remove_disclaimer from app.helpers.send_classify_request import send_classify_request from app.models import EmailData import json class ClassifyRequest(BaseModel): email_data: EmailData router = APIRouter() @router.post("/classify") async def classify_route(request: ClassifyRequest): email = request.email_data clean_email = email.copy() clean_email.subject = email.subject clean_email.body = extract_latest_message(clean_email.body) clean_email.body = clean_email_html(clean_email.body) clean_email.body = remove_disclaimer(clean_email.body) max_retries = 3 attempts = 0 valid_response = False response_data = {} # return await send_classify_request(clean_email) while attempts < max_retries: # 1. Get the raw string response raw_response = await send_classify_request(clean_email) try: if raw_response is None: print("Error: Received no response from classifier.") continue # or handle the error # 2. Parse the string into a Python dict data = json.loads(raw_response) needs_action = data.get("needs_action") task_description = data.get("task_description") # 3. Check your "re-do" condition # Logic: If it needs action but the description is missing/empty, we retry. if needs_action is True and not task_description: print(f"Attempt {attempts + 1}: Needs action but description is empty. Retrying...") attempts += 1 continue # If we reach here, the response is either (needs_action=False) # OR (needs_action=True AND has a description). response_data = data valid_response = True break except json.JSONDecodeError: print("Response was not valid JSON. Retrying...") attempts += 1 if not valid_response: print("Failed to get a valid classification after maximum retries. Sending fallback.") # Create a safe, default response response_data = { "needs_action": False, "category": "uncategorized", "priority": "low", "task_description": "", "reasoning": "System failed to classify after multiple attempts.", "confidence": 0.0 } return response_data