From c2cc4c71c963ead5f6ab5a020b3c48d1794b7484 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Sun, 22 Dec 2024 03:48:54 +0000 Subject: [PATCH] docs: Enhance guardrail validator docstring with runtime validation rationale Co-Authored-By: Joe Moura --- src/crewai/task.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/crewai/task.py b/src/crewai/task.py index c4278ce11..ed15fda2c 100644 --- a/src/crewai/task.py +++ b/src/crewai/task.py @@ -128,7 +128,28 @@ class Task(BaseModel): @field_validator("guardrail") @classmethod def validate_guardrail_function(cls, v: Optional[Callable]) -> Optional[Callable]: - """Validate that the guardrail function has the correct signature.""" + """Validate that the guardrail function has the correct signature and behavior. + + While type hints provide static checking, this validator ensures runtime safety by: + 1. Verifying the function accepts exactly one parameter (the TaskOutput) + 2. Checking return type annotations match Tuple[bool, Any] if present + 3. Providing clear, immediate error messages for debugging + + This runtime validation is crucial because: + - Type hints are optional and can be ignored at runtime + - Function signatures need immediate validation before task execution + - Clear error messages help users debug guardrail implementation issues + + Args: + v: The guardrail function to validate + + Returns: + The validated guardrail function + + Raises: + ValueError: If the function signature is invalid or return annotation + doesn't match Tuple[bool, Any] + """ if v is not None: sig = inspect.signature(v) if len(sig.parameters) != 1: