Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 9 additions & 29 deletions lib/crewai/src/crewai/agents/crew_agent_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
)
from crewai.hooks.tool_hooks import (
ToolCallHookContext,
get_after_tool_call_hooks,
get_before_tool_call_hooks,
run_after_tool_call_hooks,
run_before_tool_call_hooks,
)
from crewai.types.callback import SerializableCallable
from crewai.utilities.agent_utils import (
Expand Down Expand Up @@ -951,7 +951,6 @@ def _execute_single_native_tool_call(

track_delegation_if_needed(func_name, args_dict or {}, self.task)

hook_blocked = False
before_hook_context = ToolCallHookContext(
tool_name=func_name,
tool_input=args_dict or {},
Expand All @@ -960,19 +959,7 @@ def _execute_single_native_tool_call(
task=self.task,
crew=self.crew,
)
before_hooks = get_before_tool_call_hooks()
try:
for hook in before_hooks:
hook_result = hook(before_hook_context)
if hook_result is False:
hook_blocked = True
break
except Exception as hook_error:
if self.agent.verbose:
PRINTER.print(
content=f"Error in before_tool_call hook: {hook_error}",
color="red",
)
hook_blocked = run_before_tool_call_hooks(before_hook_context)

if hook_blocked:
result = f"Tool execution blocked by hook. Tool: {func_name}"
Expand Down Expand Up @@ -1033,19 +1020,9 @@ def _execute_single_native_tool_call(
tool_result=result,
raw_tool_result=raw_tool_result,
)
after_hooks = get_after_tool_call_hooks()
try:
for after_hook in after_hooks:
after_hook_result = after_hook(after_hook_context)
if after_hook_result is not None:
result = after_hook_result
after_hook_context.tool_result = result
except Exception as hook_error:
if self.agent.verbose:
PRINTER.print(
content=f"Error in after_tool_call hook: {hook_error}",
color="red",
)
modified_result = run_after_tool_call_hooks(after_hook_context)
if modified_result is not None:
result = modified_result

if not error_event_emitted:
crewai_event_bus.emit(
Expand All @@ -1068,6 +1045,7 @@ def _execute_single_native_tool_call(
"result": result,
"from_cache": from_cache,
"original_tool": original_tool,
"is_error": error_event_emitted,
}

def _append_tool_result_and_check_finality(
Expand All @@ -1078,6 +1056,7 @@ def _append_tool_result_and_check_finality(
result = cast(str, execution_result["result"])
from_cache = cast(bool, execution_result["from_cache"])
original_tool = execution_result["original_tool"]
is_error = cast(bool, execution_result.get("is_error", False))

tool_message: LLMMessage = {
"role": "tool",
Expand All @@ -1098,6 +1077,7 @@ def _append_tool_result_and_check_finality(
original_tool
and hasattr(original_tool, "result_as_answer")
and original_tool.result_as_answer
and not is_error
):
return AgentFinish(
thought="Tool result is the final answer",
Expand Down