Skip to content

Commit 072c3c7

Browse files
fix: Improve guardrail validation and add missing type hints
- Fixed critical bug: validation now properly checks for GuardrailResult type instead of accepting any class with the same name - Added missing TaskOutput type hints in example file - Refactored conditional logic for better readability - Maintains full backward compatibility Co-authored-by: Mervin Praison <MervinPraison@users.noreply.github.com>
1 parent db2b52b commit 072c3c7

2 files changed

Lines changed: 23 additions & 26 deletions

File tree

examples/guardrail_example_fixed.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
This addresses the issues reported in issue #875.
55
"""
66

7-
from praisonaiagents import Agent, Task, GuardrailResult, PraisonAIAgents
7+
from praisonaiagents import Agent, Task, GuardrailResult, PraisonAIAgents, TaskOutput
88
from typing import Tuple, Any
99
import trafilatura
1010

1111
# Example 1: Using GuardrailResult return type (now supported!)
12-
def validate_length_guardrailresult(output) -> GuardrailResult:
12+
def validate_length_guardrailresult(output: TaskOutput) -> GuardrailResult:
1313
"""Ensure output is between 100-500 characters using GuardrailResult"""
1414
# Extract the raw text from the TaskOutput object
1515
text = output.raw if hasattr(output, 'raw') else str(output)
@@ -29,7 +29,7 @@ def validate_length_guardrailresult(output) -> GuardrailResult:
2929
)
3030

3131
# Example 2: Using Tuple[bool, Any] return type (original method)
32-
def validate_length_tuple(output) -> Tuple[bool, Any]:
32+
def validate_length_tuple(output: TaskOutput) -> Tuple[bool, Any]:
3333
"""Ensure output is between 100-500 characters using tuple"""
3434
text = output.raw if hasattr(output, 'raw') else str(output)
3535
length = len(text)

src/praisonai-agents/praisonaiagents/task/task.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -176,29 +176,26 @@ def _setup_guardrail(self):
176176
from ..guardrails import GuardrailResult
177177

178178
# Check if it's a GuardrailResult type
179-
if return_annotation is GuardrailResult or (
180-
hasattr(return_annotation, '__name__') and
181-
return_annotation.__name__ == 'GuardrailResult'
182-
):
183-
# Valid GuardrailResult return type
184-
pass
185-
else:
186-
# Check for tuple return type
187-
return_annotation_args = get_args(return_annotation)
188-
if not (
189-
get_origin(return_annotation) is tuple
190-
and len(return_annotation_args) == 2
191-
and return_annotation_args[0] is bool
192-
and (
193-
return_annotation_args[1] is Any
194-
or return_annotation_args[1] is str
195-
or return_annotation_args[1] is TaskOutput
196-
or return_annotation_args[1] == Union[str, TaskOutput]
197-
)
198-
):
199-
raise ValueError(
200-
"If return type is annotated, it must be GuardrailResult or Tuple[bool, Any]"
201-
)
179+
is_guardrail_result = return_annotation is GuardrailResult
180+
181+
# Check for tuple return type
182+
return_annotation_args = get_args(return_annotation)
183+
is_tuple = (
184+
get_origin(return_annotation) is tuple
185+
and len(return_annotation_args) == 2
186+
and return_annotation_args[0] is bool
187+
and (
188+
return_annotation_args[1] is Any
189+
or return_annotation_args[1] is str
190+
or return_annotation_args[1] is TaskOutput
191+
or return_annotation_args[1] == Union[str, TaskOutput]
192+
)
193+
)
194+
195+
if not (is_guardrail_result or is_tuple):
196+
raise ValueError(
197+
"If return type is annotated, it must be GuardrailResult or Tuple[bool, Any]"
198+
)
202199

203200
self._guardrail_fn = self.guardrail
204201
elif isinstance(self.guardrail, str):

0 commit comments

Comments
 (0)