Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

fix: Validating the Workforce Task Content (#2611)

Hesam Sheikh13051c55eaa3
diff --git a/camel/societies/workforce/role_playing_worker.py b/camel/societies/workforce/role_playing_worker.py
index 0990aef46..52ef989b5 100644
--- a/camel/societies/workforce/role_playing_worker.py
+++ b/camel/societies/workforce/role_playing_worker.py
@@ -25,7 +25,7 @@ from camel.societies.workforce.prompts import (
ROLEPLAY_PROCESS_TASK_PROMPT,
ROLEPLAY_SUMMARIZE_PROMPT,
)
-from camel.societies.workforce.utils import TaskResult
+from camel.societies.workforce.utils import TaskResult, validate_task_content
from camel.societies.workforce.worker import Worker
from camel.tasks.task import Task, TaskState
from camel.utils import print_text_animated
@@ -182,6 +182,14 @@ class RolePlayingWorker(Worker):
)
result_dict = json.loads(response.msg.content)
task_result = TaskResult(**result_dict)
+
+ if not validate_task_content(task_result.content, task.id):
+ print(
+ f"{Fore.RED}Task {task.id}: Content validation failed - "
+ f"task marked as failed{Fore.RESET}"
+ )
+ return TaskState.FAILED
+
task.result = task_result.content
print(f"Task result: {task.result}\n")
diff --git a/camel/societies/workforce/single_agent_worker.py b/camel/societies/workforce/single_agent_worker.py
index 70f432b45..a3cb6455f 100644
--- a/camel/societies/workforce/single_agent_worker.py
+++ b/camel/societies/workforce/single_agent_worker.py
@@ -21,7 +21,7 @@ from colorama import Fore
from camel.agents import ChatAgent
from camel.societies.workforce.prompts import PROCESS_TASK_PROMPT
-from camel.societies.workforce.utils import TaskResult
+from camel.societies.workforce.utils import TaskResult, validate_task_content
from camel.societies.workforce.worker import Worker
from camel.tasks.task import Task, TaskState
from camel.utils import print_text_animated
@@ -120,5 +120,12 @@ class SingleAgentWorker(Worker):
if task_result.failed:
return TaskState.FAILED
+ if not validate_task_content(task_result.content, task.id):
+ print(
+ f"{Fore.RED}Task {task.id}: Content validation failed - "
+ f"task marked as failed{Fore.RESET}"
+ )
+ return TaskState.FAILED
+
task.result = task_result.content
return TaskState.DONE
diff --git a/camel/societies/workforce/utils.py b/camel/societies/workforce/utils.py
index bdf0aafa8..c3daed50d 100644
--- a/camel/societies/workforce/utils.py
+++ b/camel/societies/workforce/utils.py
@@ -16,6 +16,10 @@ from typing import Callable
from pydantic import BaseModel, Field
+from camel.logger import get_logger
+
+logger = get_logger(__name__)
+
class WorkerConf(BaseModel):
r"""The configuration of a worker."""
@@ -71,3 +75,50 @@ def check_if_running(running: bool) -> Callable:
return wrapper
return decorator
+
+
+def validate_task_content(
+ content: str, task_id: str = "unknown", min_length: int = 10
+) -> bool:
+ r"""Validates task result content to avoid silent failures.
+ It performs basic checks to ensure the content meets minimum
+ quality standards.
+
+ Args:
+ content (str): The task result content to validate.
+ task_id (str): Task ID for logging purposes.
+ (default: :obj:`"unknown"`)
+ min_length (int): Minimum content length after stripping whitespace.
+ (default: :obj:`10`)
+
+ Returns:
+ bool: True if content passes validation, False otherwise.
+ """
+ # 1: Content must not be None
+ if content is None:
+ logger.warning(f"Task {task_id}: None content rejected")
+ return False
+
+ # 2: Content must not be empty after stripping whitespace
+ stripped_content = content.strip()
+ if not stripped_content:
+ logger.warning(
+ f"Task {task_id}: Empty or whitespace-only content rejected."
+ )
+ return False
+
+ # 3: Content must meet minimum meaningful length
+ if len(stripped_content) < min_length:
+ logger.warning(
+ f"Task {task_id}: Content too short ({len(stripped_content)} "
+ f"chars < {min_length} minimum). Content preview: "
+ f"'{stripped_content[:50]}...'"
+ )
+ return False
+
+ # All validation checks passed
+ logger.debug(
+ f"Task {task_id}: Content validation passed "
+ f"({len(stripped_content)} chars)"
+ )
+ return True