Skip to content

Commit 13051c5

Browse files
fix: Validating the Workforce Task Content (#2611)
Co-authored-by: Wendong <w3ndong.fan@gmail.com>
1 parent 5a1e0eb commit 13051c5

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

camel/societies/workforce/role_playing_worker.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
ROLEPLAY_PROCESS_TASK_PROMPT,
2626
ROLEPLAY_SUMMARIZE_PROMPT,
2727
)
28-
from camel.societies.workforce.utils import TaskResult
28+
from camel.societies.workforce.utils import TaskResult, validate_task_content
2929
from camel.societies.workforce.worker import Worker
3030
from camel.tasks.task import Task, TaskState
3131
from camel.utils import print_text_animated
@@ -182,6 +182,14 @@ async def _process_task(
182182
)
183183
result_dict = json.loads(response.msg.content)
184184
task_result = TaskResult(**result_dict)
185+
186+
if not validate_task_content(task_result.content, task.id):
187+
print(
188+
f"{Fore.RED}Task {task.id}: Content validation failed - "
189+
f"task marked as failed{Fore.RESET}"
190+
)
191+
return TaskState.FAILED
192+
185193
task.result = task_result.content
186194

187195
print(f"Task result: {task.result}\n")

camel/societies/workforce/single_agent_worker.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
from camel.agents import ChatAgent
2323
from camel.societies.workforce.prompts import PROCESS_TASK_PROMPT
24-
from camel.societies.workforce.utils import TaskResult
24+
from camel.societies.workforce.utils import TaskResult, validate_task_content
2525
from camel.societies.workforce.worker import Worker
2626
from camel.tasks.task import Task, TaskState
2727
from camel.utils import print_text_animated
@@ -120,5 +120,12 @@ async def _process_task(
120120
if task_result.failed:
121121
return TaskState.FAILED
122122

123+
if not validate_task_content(task_result.content, task.id):
124+
print(
125+
f"{Fore.RED}Task {task.id}: Content validation failed - "
126+
f"task marked as failed{Fore.RESET}"
127+
)
128+
return TaskState.FAILED
129+
123130
task.result = task_result.content
124131
return TaskState.DONE

camel/societies/workforce/utils.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
from pydantic import BaseModel, Field
1818

19+
from camel.logger import get_logger
20+
21+
logger = get_logger(__name__)
22+
1923

2024
class WorkerConf(BaseModel):
2125
r"""The configuration of a worker."""
@@ -71,3 +75,50 @@ def wrapper(self, *args, **kwargs):
7175
return wrapper
7276

7377
return decorator
78+
79+
80+
def validate_task_content(
81+
content: str, task_id: str = "unknown", min_length: int = 10
82+
) -> bool:
83+
r"""Validates task result content to avoid silent failures.
84+
It performs basic checks to ensure the content meets minimum
85+
quality standards.
86+
87+
Args:
88+
content (str): The task result content to validate.
89+
task_id (str): Task ID for logging purposes.
90+
(default: :obj:`"unknown"`)
91+
min_length (int): Minimum content length after stripping whitespace.
92+
(default: :obj:`10`)
93+
94+
Returns:
95+
bool: True if content passes validation, False otherwise.
96+
"""
97+
# 1: Content must not be None
98+
if content is None:
99+
logger.warning(f"Task {task_id}: None content rejected")
100+
return False
101+
102+
# 2: Content must not be empty after stripping whitespace
103+
stripped_content = content.strip()
104+
if not stripped_content:
105+
logger.warning(
106+
f"Task {task_id}: Empty or whitespace-only content rejected."
107+
)
108+
return False
109+
110+
# 3: Content must meet minimum meaningful length
111+
if len(stripped_content) < min_length:
112+
logger.warning(
113+
f"Task {task_id}: Content too short ({len(stripped_content)} "
114+
f"chars < {min_length} minimum). Content preview: "
115+
f"'{stripped_content[:50]}...'"
116+
)
117+
return False
118+
119+
# All validation checks passed
120+
logger.debug(
121+
f"Task {task_id}: Content validation passed "
122+
f"({len(stripped_content)} chars)"
123+
)
124+
return True

0 commit comments

Comments
 (0)