Files
crewAI/lib/crewai/src/crewai/agents/tools_handler.py
Vinicius Brasil 58fc692b03 Expose raw tool result to after_tool_call hooks
Typed tools format their output to JSON before handing it to the agent,
so `tool_result` reaches after hooks as a string and the original Python
object is lost. Thread the unformatted result through as
`raw_tool_result` so hooks can inspect the typed value. This covers all
execution paths: native tool calls, the ReAct `ToolUsage` path, cached
results, and error/blocked branches.
2026-06-18 21:20:30 -07:00

53 lines
1.6 KiB
Python

"""Tools handler for managing tool execution and caching."""
from __future__ import annotations
import json
from typing import Any
from pydantic import BaseModel, Field
from crewai.agents.cache.cache_handler import CacheHandler
from crewai.tools.cache_tools.cache_tools import CacheTools
from crewai.tools.tool_calling import InstructorToolCalling, ToolCalling
class ToolsHandler(BaseModel):
"""Callback handler for tool usage.
Attributes:
last_used_tool: The most recently used tool calling instance.
cache: Optional cache handler for storing tool outputs.
"""
cache: CacheHandler | None = Field(default=None)
last_used_tool: ToolCalling | InstructorToolCalling | None = Field(default=None)
def on_tool_use(
self,
calling: ToolCalling | InstructorToolCalling,
output: Any,
should_cache: bool = True,
) -> None:
"""Run when tool ends running.
Args:
calling: The tool calling instance.
output: The raw output from the tool execution.
should_cache: Whether to cache the tool output.
"""
self.last_used_tool = calling
if self.cache and should_cache and calling.tool_name != CacheTools().name:
input_str = ""
if calling.arguments:
if isinstance(calling.arguments, dict):
input_str = json.dumps(calling.arguments)
else:
input_str = str(calling.arguments)
self.cache.add(
tool=calling.tool_name,
input=input_str,
output=output,
)