[BUG] Filtered @before_*/@after_* hook decorators cannot be unregistered via unregister_*_hook
Description
When registering a global hook using any of the filtered hook decorators (@before_tool_call(tools=...), @after_tool_call(tools=...), @before_llm_call(agents=...), or @after_llm_call(agents=...)), _create_hook_decorator (lib/crewai/src/crewai/hooks/decorators.py:54-75) registers the internal filtered_hook wrapper while returning the original function f.
Unlike @on(...) in lib/crewai/src/crewai/hooks/dispatch.py:406 (which stores func._registered_hook = hook and resolves it in dispatch.unregister), _create_hook_decorator does not attach _registered_hook, and all four unregister_*_hook helpers in tool_hooks.py and llm_hooks.py call .remove(hook) directly. As a result, calling unregister_before_tool_call_hook(fn) (or the other three unregister functions) always returns False and leaks the hook in the global registry.
Steps to Reproduce
- Decorate a top-level function with
@before_tool_call(tools=["delete_file"])(or@after_tool_call,@before_llm_call(agents=...),@after_llm_call(agents=...)). - Call
unregister_before_tool_call_hook(my_hook)(or the matchingunregister_*_hookhelper). - Observe that it returns
Falseandget_before_tool_call_hooks()still contains the wrapper hook.
Expected behavior
Calling unregister_before_tool_call_hook, unregister_after_tool_call_hook, unregister_before_llm_call_hook, or unregister_after_llm_call_hook with a function decorated by the corresponding filtered @before_*/@after_* decorator should remove the registered wrapper and return True, matching dispatch.unregister behavior for @on(...).
Screenshots/Code snippets
from crewai.hooks.decorators import before_tool_call from crewai.hooks.tool_hooks import ( clear_all_tool_call_hooks, get_before_tool_call_hooks, unregister_before_tool_call_hook, )
clear_all_tool_call_hooks()
@before_tool_call(tools=["delete_file"]) def guard_delete(context): return False
removed = unregister_before_tool_call_hook(guard_delete) print("removed:", removed, "remaining:", len(get_before_tool_call_hooks()))
Actual: removed: False remaining: 1
Expected: removed: True remaining: 0
Operating System
macOS Sonoma
Python Version
3.12
crewAI Version
1.15.44 (main 6ecb87390)
crewAI Tools Version
N/A
Virtual Environment
Venv
Evidence
lib/crewai/src/crewai/hooks/decorators.py:54-75: registersfiltered_hookand returnsfwithout storingf._registered_hook = filtered_hook.lib/crewai/src/crewai/hooks/tool_hooks.py:298-347andlib/crewai/src/crewai/hooks/llm_hooks.py:313-362:unregister_*_hookcalls.remove(hook)directly instead of delegating tocrewai.hooks.dispatch.unregister.- Running the snippet above prints
removed: False remaining: 1.
Possible Solution
Store f._registered_hook = filtered_hook in _create_hook_decorator when registering a filtered global hook (mirroring dispatch.on), and delegate unregister_before_tool_call_hook, unregister_after_tool_call_hook, unregister_before_llm_call_hook, and unregister_after_llm_call_hook to crewai.hooks.dispatch.unregister.
Additional context
I have a minimal fix with regression tests ready and will open a linked PR. AI assistance was used in preparing this report (llm-generated).
Confirmed on 1.15.44 (Linux, CPython 3.12) — all four unregister helpers return False and the hook stays registered:
@before_tool_call(tools=["delete_file"])
def guard_delete(context):
return False
unregister_before_tool_call_hook(guard_delete) # False, 1 hook remaining
Same for @after_tool_call, @before_llm_call(agents=[...]), and @after_llm_call — all False, all still in the registry.
Your diagnosis is right on both halves. _create_hook_decorator (hooks/decorators.py) registers filtered_hook via register_function and returns the original f without stashing anything on it, while dispatch.on (hooks/dispatch.py:439) does func._registered_hook = hook — and dispatch.unregister (:124-138) already resolves hook._registered_hook when the function itself isn't in the list. So the plumbing exists; the filtered decorators just don't use it. The second half is that the four unregister_*_hook helpers call .remove(hook) directly instead of going through dispatch.unregister.
I also checked the fix direction works end to end: stashing the registered wrapper as guard_delete._registered_hook and calling dispatch.unregister(InterceptionPoint.PRE_TOOL_CALL, guard_delete) returns True with 0 hooks remaining. So your suggested change — stash in _create_hook_decorator and delegate the four helpers to dispatch.unregister — is the right shape. Worth a regression test per decorator, since the four paths go through the same helper but register at different interception points.
Thanks for independently reproducing this on 1.15.44 and verifying the fix direction!
PR #9683 implements this exact approach (stashing _registered_hook in _create_hook_decorator and delegating all four unregister_*_hook helpers to dispatch.unregister) and includes dedicated regression tests covering all four filtered decorators (@before_tool_call, @after_tool_call, @before_llm_call, and @after_llm_call).