11import threading
2- from typing import Any , Optional
2+ from collections .abc import Sequence
3+ from typing import Any
34
4- from crewai .experimental .evaluation .base_evaluator import (
5- AgentEvaluationResult ,
6- AggregationStrategy ,
7- )
85from crewai .agent import Agent
9- from crewai .task import Task
10- from crewai .experimental . evaluation . evaluation_display import EvaluationDisplayFormatter
6+ from crewai .agents . agent_builder . base_agent import BaseAgent
7+ from crewai .events . event_bus import crewai_event_bus
118from crewai .events .types .agent_events import (
12- AgentEvaluationStartedEvent ,
139 AgentEvaluationCompletedEvent ,
1410 AgentEvaluationFailedEvent ,
11+ AgentEvaluationStartedEvent ,
12+ LiteAgentExecutionCompletedEvent ,
1513)
16- from crewai .experimental .evaluation import BaseEvaluator , create_evaluation_callbacks
17- from collections .abc import Sequence
18- from crewai .events .event_bus import crewai_event_bus
19- from crewai .events .utils .console_formatter import ConsoleFormatter
2014from crewai .events .types .task_events import TaskCompletedEvent
21- from crewai .events .types .agent_events import LiteAgentExecutionCompletedEvent
15+ from crewai .events .utils .console_formatter import ConsoleFormatter
16+ from crewai .experimental .evaluation import BaseEvaluator , create_evaluation_callbacks
2217from crewai .experimental .evaluation .base_evaluator import (
2318 AgentAggregatedEvaluationResult ,
19+ AgentEvaluationResult ,
20+ AggregationStrategy ,
2421 EvaluationScore ,
2522 MetricCategory ,
2623)
24+ from crewai .experimental .evaluation .evaluation_display import EvaluationDisplayFormatter
25+ from crewai .task import Task
2726
2827
2928class ExecutionState :
30- current_agent_id : Optional [ str ] = None
31- current_task_id : Optional [ str ] = None
29+ current_agent_id : str | None = None
30+ current_task_id : str | None = None
3231
3332 def __init__ (self ):
3433 self .traces = {}
@@ -40,10 +39,10 @@ def __init__(self):
4039class AgentEvaluator :
4140 def __init__ (
4241 self ,
43- agents : list [Agent ],
42+ agents : list [Agent ] | list [ BaseAgent ] ,
4443 evaluators : Sequence [BaseEvaluator ] | None = None ,
4544 ):
46- self .agents : list [Agent ] = agents
45+ self .agents : list [Agent ] | list [ BaseAgent ] = agents
4746 self .evaluators : Sequence [BaseEvaluator ] | None = evaluators
4847
4948 self .callback = create_evaluation_callbacks ()
@@ -75,7 +74,8 @@ def _subscribe_to_events(self) -> None:
7574 )
7675
7776 def _handle_task_completed (self , source : Any , event : TaskCompletedEvent ) -> None :
78- assert event .task is not None
77+ if event .task is None :
78+ raise ValueError ("TaskCompletedEvent must have a task" )
7979 agent = event .task .agent
8080 if (
8181 agent
@@ -92,9 +92,8 @@ def _handle_task_completed(self, source: Any, event: TaskCompletedEvent) -> None
9292 state .current_agent_id = str (agent .id )
9393 state .current_task_id = str (event .task .id )
9494
95- assert (
96- state .current_agent_id is not None and state .current_task_id is not None
97- )
95+ if state .current_agent_id is None or state .current_task_id is None :
96+ raise ValueError ("Agent ID and Task ID must not be None" )
9897 trace = self .callback .get_trace (
9998 state .current_agent_id , state .current_task_id
10099 )
@@ -146,9 +145,8 @@ def _handle_lite_agent_completed(
146145 if not target_agent :
147146 return
148147
149- assert (
150- state .current_agent_id is not None and state .current_task_id is not None
151- )
148+ if state .current_agent_id is None or state .current_task_id is None :
149+ raise ValueError ("Agent ID and Task ID must not be None" )
152150 trace = self .callback .get_trace (
153151 state .current_agent_id , state .current_task_id
154152 )
@@ -244,7 +242,7 @@ def display_evaluation_with_feedback(self) -> None:
244242
245243 def evaluate (
246244 self ,
247- agent : Agent ,
245+ agent : Agent | BaseAgent ,
248246 execution_trace : dict [str , Any ],
249247 final_output : Any ,
250248 state : ExecutionState ,
@@ -255,7 +253,8 @@ def evaluate(
255253 task_id = state .current_task_id or (str (task .id ) if task else "unknown_task" ),
256254 )
257255
258- assert self .evaluators is not None
256+ if self .evaluators is None :
257+ raise ValueError ("Evaluators must be initialized" )
259258 task_id = str (task .id ) if task else None
260259 for evaluator in self .evaluators :
261260 try :
@@ -276,15 +275,15 @@ def evaluate(
276275 metric_category = evaluator .metric_category ,
277276 score = score ,
278277 )
279- except Exception as e :
278+ except Exception as e : # noqa: PERF203
280279 self .emit_evaluation_failed_event (
281280 agent_role = agent .role ,
282281 agent_id = str (agent .id ),
283282 task_id = task_id ,
284283 error = str (e ),
285284 )
286285 self .console_formatter .print (
287- f"Error in { evaluator .metric_category .value } evaluator: { str ( e ) } "
286+ f"Error in { evaluator .metric_category .value } evaluator: { e !s } "
288287 )
289288
290289 return result
@@ -337,14 +336,14 @@ def emit_evaluation_failed_event(
337336 )
338337
339338
340- def create_default_evaluator (agents : list [Agent ], llm : None = None ):
339+ def create_default_evaluator (agents : list [Agent ] | list [ BaseAgent ] , llm : None = None ):
341340 from crewai .experimental .evaluation import (
342341 GoalAlignmentEvaluator ,
343- SemanticQualityEvaluator ,
344- ToolSelectionEvaluator ,
345342 ParameterExtractionEvaluator ,
346- ToolInvocationEvaluator ,
347343 ReasoningEfficiencyEvaluator ,
344+ SemanticQualityEvaluator ,
345+ ToolInvocationEvaluator ,
346+ ToolSelectionEvaluator ,
348347 )
349348
350349 evaluators = [
0 commit comments