From 14bc97fb027a7f44b5d04a92e0b5ab23973c639a Mon Sep 17 00:00:00 2001 From: Daniel Henry Date: Wed, 28 Jan 2026 13:35:07 -0600 Subject: [PATCH] Add retrying when task has no description Signed-off-by: Daniel Henry --- app/routers/classify_email.py | 72 ++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 23 deletions(-) diff --git a/app/routers/classify_email.py b/app/routers/classify_email.py index 33d7bef..83b045d 100644 --- a/app/routers/classify_email.py +++ b/app/routers/classify_email.py @@ -5,8 +5,7 @@ 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 - -logging = True +import json class ClassifyRequest(BaseModel): email_data: EmailData @@ -14,37 +13,64 @@ class ClassifyRequest(BaseModel): router = APIRouter() @router.post("/classify") async def classify_route(request: ClassifyRequest): - - if logging: - print("Payload Recieved:") - print(request.email_data) - print("\n\n-----") - - 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) - if logging: - print("Cleaned Email Body:") - print(clean_email_html(clean_email.body)) - print("\n\n-----\n\n\n") clean_email.body = remove_disclaimer(clean_email.body) - if logging: - print("Cleaned Payload:") - print(clean_email.subject) - print("\n\n-----") - + max_retries = 3 + attempts = 0 + valid_response = False + response_data = {} response = 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 = False # data.get("task_description") - return response + # 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