From 05edc4f226b376ab373b35c7468fa2e2b155bf28 Mon Sep 17 00:00:00 2001 From: Lucas Gomide Date: Wed, 30 Apr 2025 14:02:08 -0300 Subject: [PATCH] fix: renaming TaskGuardrail to LLMGuardrail --- docs/concepts/tasks.mdx | 6 +++--- src/crewai/task.py | 12 ++++++------ .../tasks/{task_guardrail.py => llm_guardrail.py} | 8 ++++---- src/crewai/utilities/events/__init__.py | 6 +++--- src/crewai/utilities/events/event_types.py | 12 ++++++------ ...guardrail_events.py => llm_guardrail_events.py} | 14 +++++++------- tests/test_task_guardrails.py | 14 +++++++------- 7 files changed, 36 insertions(+), 36 deletions(-) rename src/crewai/tasks/{task_guardrail.py => llm_guardrail.py} (93%) rename src/crewai/utilities/events/{task_guardrail_events.py => llm_guardrail_events.py} (67%) diff --git a/docs/concepts/tasks.mdx b/docs/concepts/tasks.mdx index 5b2916d94..b88d52a92 100644 --- a/docs/concepts/tasks.mdx +++ b/docs/concepts/tasks.mdx @@ -322,9 +322,9 @@ blog_task = Task( - On success: it returns a tuple of `(bool, Any)`. For example: `(True, validated_result)` - On Failure: it returns a tuple of `(bool, str)`. For example: `(False, "Error message explain the failure")` -### TaskGuardrail +### LLMGuardrail -The `TaskGuardrail` class offers a robust mechanism for validating task outputs +The `LLMGuardrail` class offers a robust mechanism for validating task outputs. ### Error Handling Best Practices @@ -819,7 +819,7 @@ from crewai.llm import LLM task = Task( description="Generate JSON data", expected_output="Valid JSON object", - guardrail=TaskGuardrail( + guardrail=LLMGuardrail( description="Ensure the response is a valid JSON object", llm=LLM(model="gpt-4o-mini"), ) diff --git a/src/crewai/task.py b/src/crewai/task.py index 8a1091935..e4a25f438 100644 --- a/src/crewai/task.py +++ b/src/crewai/task.py @@ -241,10 +241,10 @@ class Task(BaseModel): if callable(self.guardrail): self._guardrail = self.guardrail elif isinstance(self.guardrail, str): - from crewai.tasks.task_guardrail import TaskGuardrail + from crewai.tasks.llm_guardrail import LLMGuardrail assert self.agent is not None - self._guardrail = TaskGuardrail( + self._guardrail = LLMGuardrail( description=self.guardrail, llm=self.agent.llm ) @@ -494,8 +494,8 @@ class Task(BaseModel): assert self._guardrail is not None from crewai.utilities.events import ( - TaskGuardrailCompletedEvent, - TaskGuardrailStartedEvent, + LLMGuardrailCompletedEvent, + LLMGuardrailStartedEvent, ) from crewai.utilities.events.crewai_event_bus import crewai_event_bus @@ -503,7 +503,7 @@ class Task(BaseModel): crewai_event_bus.emit( self, - TaskGuardrailStartedEvent( + LLMGuardrailStartedEvent( guardrail=self._guardrail, retry_count=self.retry_count ), ) @@ -512,7 +512,7 @@ class Task(BaseModel): crewai_event_bus.emit( self, - TaskGuardrailCompletedEvent( + LLMGuardrailCompletedEvent( success=guardrail_result.success, result=guardrail_result.result, error=guardrail_result.error, diff --git a/src/crewai/tasks/task_guardrail.py b/src/crewai/tasks/llm_guardrail.py similarity index 93% rename from src/crewai/tasks/task_guardrail.py rename to src/crewai/tasks/llm_guardrail.py index 9c1ae916b..2bb948075 100644 --- a/src/crewai/tasks/task_guardrail.py +++ b/src/crewai/tasks/llm_guardrail.py @@ -8,7 +8,7 @@ from crewai.task import Task from crewai.tasks.task_output import TaskOutput -class TaskGuardrailResult(BaseModel): +class LLMGuardrailResult(BaseModel): valid: bool = Field( description="Whether the task output complies with the guardrail" ) @@ -18,7 +18,7 @@ class TaskGuardrailResult(BaseModel): ) -class TaskGuardrail: +class LLMGuardrail: """It validates the output of another task using an LLM. This class is used to validate the output from a Task based on specified criteria. @@ -62,7 +62,7 @@ class TaskGuardrail: - If the Task result complies with the guardrail, saying that is valid """ - result = agent.kickoff(query, response_format=TaskGuardrailResult) + result = agent.kickoff(query, response_format=LLMGuardrailResult) return result @@ -81,7 +81,7 @@ class TaskGuardrail: try: result = self._validate_output(task_output) assert isinstance( - result.pydantic, TaskGuardrailResult + result.pydantic, LLMGuardrailResult ), "The guardrail result is not a valid pydantic model" if result.pydantic.valid: diff --git a/src/crewai/utilities/events/__init__.py b/src/crewai/utilities/events/__init__.py index b87f3d9fa..d26d167bb 100644 --- a/src/crewai/utilities/events/__init__.py +++ b/src/crewai/utilities/events/__init__.py @@ -9,9 +9,9 @@ from .crew_events import ( CrewTestCompletedEvent, CrewTestFailedEvent, ) -from .task_guardrail_events import ( - TaskGuardrailCompletedEvent, - TaskGuardrailStartedEvent, +from .llm_guardrail_events import ( + LLMGuardrailCompletedEvent, + LLMGuardrailStartedEvent, ) from .agent_events import ( AgentExecutionStartedEvent, diff --git a/src/crewai/utilities/events/event_types.py b/src/crewai/utilities/events/event_types.py index 9f5a5da58..4e0673758 100644 --- a/src/crewai/utilities/events/event_types.py +++ b/src/crewai/utilities/events/event_types.py @@ -29,15 +29,15 @@ from .llm_events import ( LLMCallStartedEvent, LLMStreamChunkEvent, ) +from .llm_guardrail_events import ( + LLMGuardrailCompletedEvent, + LLMGuardrailStartedEvent, +) from .task_events import ( TaskCompletedEvent, TaskFailedEvent, TaskStartedEvent, ) -from .task_guardrail_events import ( - TaskGuardrailCompletedEvent, - TaskGuardrailStartedEvent, -) from .tool_usage_events import ( ToolUsageErrorEvent, ToolUsageFinishedEvent, @@ -72,6 +72,6 @@ EventTypes = Union[ LLMCallCompletedEvent, LLMCallFailedEvent, LLMStreamChunkEvent, - TaskGuardrailStartedEvent, - TaskGuardrailCompletedEvent, + LLMGuardrailStartedEvent, + LLMGuardrailCompletedEvent, ] diff --git a/src/crewai/utilities/events/task_guardrail_events.py b/src/crewai/utilities/events/llm_guardrail_events.py similarity index 67% rename from src/crewai/utilities/events/task_guardrail_events.py rename to src/crewai/utilities/events/llm_guardrail_events.py index 1a036d29c..a484c187a 100644 --- a/src/crewai/utilities/events/task_guardrail_events.py +++ b/src/crewai/utilities/events/llm_guardrail_events.py @@ -3,35 +3,35 @@ from typing import Any, Callable, Optional, Union from crewai.utilities.events.base_events import BaseEvent -class TaskGuardrailStartedEvent(BaseEvent): +class LLMGuardrailStartedEvent(BaseEvent): """Event emitted when a guardrail task starts Attributes: - guardrail: The guardrail callable or TaskGuardrail instance + guardrail: The guardrail callable or LLMGuardrail instance retry_count: The number of times the guardrail has been retried """ - type: str = "task_guardrail_started" + type: str = "llm_guardrail_started" guardrail: Union[str, Callable] retry_count: int def __init__(self, **data): from inspect import getsource - from crewai.tasks.task_guardrail import TaskGuardrail + from crewai.tasks.llm_guardrail import LLMGuardrail super().__init__(**data) - if isinstance(self.guardrail, TaskGuardrail): + if isinstance(self.guardrail, LLMGuardrail): self.guardrail = self.guardrail.description.strip() elif isinstance(self.guardrail, Callable): self.guardrail = getsource(self.guardrail).strip() -class TaskGuardrailCompletedEvent(BaseEvent): +class LLMGuardrailCompletedEvent(BaseEvent): """Event emitted when a guardrail task completes""" - type: str = "task_guardrail_completed" + type: str = "llm_guardrail_completed" success: bool result: Any error: Optional[str] = None diff --git a/tests/test_task_guardrails.py b/tests/test_task_guardrails.py index c4224f9cc..aaac05bb7 100644 --- a/tests/test_task_guardrails.py +++ b/tests/test_task_guardrails.py @@ -4,11 +4,11 @@ import pytest from crewai import Agent, Task from crewai.llm import LLM -from crewai.tasks.task_guardrail import TaskGuardrail +from crewai.tasks.llm_guardrail import LLMGuardrail from crewai.tasks.task_output import TaskOutput from crewai.utilities.events import ( - TaskGuardrailCompletedEvent, - TaskGuardrailStartedEvent, + LLMGuardrailCompletedEvent, + LLMGuardrailStartedEvent, ) from crewai.utilities.events.crewai_event_bus import crewai_event_bus @@ -153,7 +153,7 @@ def task_output(): @pytest.mark.vcr(filter_headers=["authorization"]) def test_task_guardrail_process_output(task_output): - guardrail = TaskGuardrail( + guardrail = LLMGuardrail( description="Ensure the result has less than 10 words", llm=LLM(model="gpt-4o") ) @@ -162,7 +162,7 @@ def test_task_guardrail_process_output(task_output): assert "exceeding the guardrail limit of fewer than" in result[1].lower() - guardrail = TaskGuardrail( + guardrail = LLMGuardrail( description="Ensure the result has less than 500 words", llm=LLM(model="gpt-4o") ) @@ -178,13 +178,13 @@ def test_guardrail_emits_events(sample_agent): with crewai_event_bus.scoped_handlers(): - @crewai_event_bus.on(TaskGuardrailStartedEvent) + @crewai_event_bus.on(LLMGuardrailStartedEvent) def handle_guardrail_started(source, event): started_guardrail.append( {"guardrail": event.guardrail, "retry_count": event.retry_count} ) - @crewai_event_bus.on(TaskGuardrailCompletedEvent) + @crewai_event_bus.on(LLMGuardrailCompletedEvent) def handle_guardrail_completed(source, event): completed_guardrail.append( {