Description
Feature Area
Core functionality
Is your feature request related to a an existing bug? Please link it here.
Feature Request: Include Tool Results in ToolUsageFinishedEvent
Current Behavior
Currently, when a tool execution is completed, CrewAI emits a ToolUsageFinishedEvent
that includes various metadata about the execution (agent info, tool name, arguments, timing, etc.) but doesn't include the actual result returned by the tool.
This means that event listeners need to implement complex workarounds to access the tool results, typically by accessing the agent's tools_results
array and searching for matching entries.
Proposed Change
Add a result
field to the ToolUsageFinishedEvent
class and ensure it's populated with the tool's execution result when the event is emitted.
Benefits
- Improved developer experience when working with tool execution events
- Reduced coupling between event listeners and CrewAI's internal structure
- More complete event information - events should contain all relevant data
- Simplifies external integrations and monitoring systems that rely on events
Implementation
The change would be minimal and backward compatible:
- Modify the
ToolUsageFinishedEvent
class increwai/utilities/events/tool_usage_events.py
:
class ToolUsageFinishedEvent(ToolUsageEvent):
"""Event emitted when a tool execution is completed"""
started_at: datetime
finished_at: datetime
from_cache: bool = False
result: Any = None # Add this field
type: str = "tool_usage_finished"
Add the tool execution result directly to ToolUsageFinishedEvent:
-
Add a
result: Any = None
field to the ToolUsageFinishedEvent class -
Modify the ToolUsage.on_tool_use_finished method to include the result in the event data:
- Update the method signature to accept the result parameter
- Add the result to the event_data dictionary
-
Update the call to on_tool_use_finished in the _use method to pass the result parameter
This would allow event listeners to directly access the tool's result output through event.result without having to implement complex workarounds.
Current workarounds and alternatives I've explored:
-
Current workaround: Access agent.tools_results from the event's source
- Requires searching through a list of results to find the matching entry
- Creates tight coupling between listeners and CrewAI's internal structure
- Brittle if CrewAI changes how tool results are stored
-
Alternative: Create a custom subclass of ToolUsageFinishedEvent
- Would require monkey-patching the event bus to intercept events
- Complexity of maintaining this external to CrewAI codebase
- Risk of breaking when CrewAI is updated
-
Alternative: Add a tool results caching system to event listeners
- Additional complexity and state management
- Would still need to correlate events with cached results
Willingness to Contribute
Yes, I'd be happy to submit a pull request