Compare commits

...

4 Commits

Author SHA1 Message Date
Devin AI
84a16abb4a Fix type checker errors in on_tool_use call
Co-Authored-By: Joe Moura <joao@crewai.com>
2025-04-10 16:31:55 +00:00
Devin AI
77ed11898b Fix import sorting in test file
Co-Authored-By: Joe Moura <joao@crewai.com>
2025-04-10 16:29:49 +00:00
Devin AI
c73c795993 Fix lint and type checker errors
Co-Authored-By: Joe Moura <joao@crewai.com>
2025-04-10 16:28:30 +00:00
Devin AI
4bd7d6968a Fix issue #2574: Enable CodeInterpreterTool execution
Co-Authored-By: Joe Moura <joao@crewai.com>
2025-04-10 16:25:01 +00:00
2 changed files with 97 additions and 21 deletions

View File

@@ -141,7 +141,8 @@ class ToolUsage:
self._printer.print(content=f"\n\n{error}\n", color="red") self._printer.print(content=f"\n\n{error}\n", color="red")
return error return error
return f"{self._use(tool_string=tool_string, tool=tool, calling=calling)}" result = self._use(tool_string=tool_string, tool=tool, calling=calling)
return f"{result}"
def _use( def _use(
self, self,
@@ -149,9 +150,9 @@ class ToolUsage:
tool: CrewStructuredTool, tool: CrewStructuredTool,
calling: Union[ToolCalling, InstructorToolCalling], calling: Union[ToolCalling, InstructorToolCalling],
) -> str: ) -> str:
if self._check_tool_repeated_usage(calling=calling): # type: ignore # _check_tool_repeated_usage of "ToolUsage" does not return a value (it only ever returns None) if self._check_tool_repeated_usage(calling=calling):
try: try:
result = self._i18n.errors("task_repeated_usage").format( repeated_result = self._i18n.errors("task_repeated_usage").format(
tool_names=self.tools_names tool_names=self.tools_names
) )
self._telemetry.tool_repeated_usage( self._telemetry.tool_repeated_usage(
@@ -159,8 +160,8 @@ class ToolUsage:
tool_name=tool.name, tool_name=tool.name,
attempts=self._run_attempts, attempts=self._run_attempts,
) )
result = self._format_result(result=result) # type: ignore # "_format_result" of "ToolUsage" does not return a value (it only ever returns None) repeated_result = self._format_result(result=repeated_result)
return result # type: ignore # Fix the return type of this function return repeated_result
except Exception: except Exception:
if self.task: if self.task:
@@ -168,13 +169,15 @@ class ToolUsage:
started_at = time.time() started_at = time.time()
from_cache = False from_cache = False
result = None # type: ignore result: Optional[str] = None
if self.tools_handler and self.tools_handler.cache: if self.tools_handler and self.tools_handler.cache:
result = self.tools_handler.cache.read( cache_result = self.tools_handler.cache.read(
tool=calling.tool_name, input=calling.arguments tool=calling.tool_name, input=calling.arguments
) # type: ignore )
from_cache = result is not None if cache_result is not None:
result = cache_result
from_cache = True
available_tool = next( available_tool = next(
( (
@@ -236,31 +239,32 @@ class ToolUsage:
self._printer.print( self._printer.print(
content=f"\n\n{error_message}\n", color="red" content=f"\n\n{error_message}\n", color="red"
) )
return error # type: ignore # No return value expected return error
if self.task: if self.task:
self.task.increment_tools_errors() self.task.increment_tools_errors()
return self.use(calling=calling, tool_string=tool_string) # type: ignore # No return value expected return self.use(calling=calling, tool_string=tool_string)
if self.tools_handler: if self.tools_handler:
should_cache = True should_cache = True
if ( if available_tool is not None and (
hasattr(available_tool, "cache_function") hasattr(available_tool, "cache_function")
and available_tool.cache_function # type: ignore # Item "None" of "Any | None" has no attribute "cache_function" and available_tool.cache_function
): ):
should_cache = available_tool.cache_function( # type: ignore # Item "None" of "Any | None" has no attribute "cache_function" should_cache = available_tool.cache_function(
calling.arguments, result calling.arguments, result
) )
output_result = "" if result is None else result
self.tools_handler.on_tool_use( self.tools_handler.on_tool_use(
calling=calling, output=result, should_cache=should_cache calling=calling, output=output_result, should_cache=should_cache
) )
self._telemetry.tool_usage( self._telemetry.tool_usage(
llm=self.function_calling_llm, llm=self.function_calling_llm,
tool_name=tool.name, tool_name=tool.name,
attempts=self._run_attempts, attempts=self._run_attempts,
) )
result = self._format_result(result=result) # type: ignore # "_format_result" of "ToolUsage" does not return a value (it only ever returns None) result = self._format_result(result=result)
data = { data = {
"result": result, "result": result,
"tool_name": tool.name, "tool_name": tool.name,
@@ -275,12 +279,13 @@ class ToolUsage:
result=result, result=result,
) )
if ( if available_tool is not None and (
hasattr(available_tool, "result_as_answer") hasattr(available_tool, "result_as_answer")
and available_tool.result_as_answer # type: ignore # Item "None" of "Any | None" has no attribute "cache_function" and available_tool.result_as_answer
): ):
result_as_answer = available_tool.result_as_answer # type: ignore # Item "None" of "Any | None" has no attribute "result_as_answer" result_as_answer = available_tool.result_as_answer
data["result_as_answer"] = result_as_answer # type: ignore if isinstance(result_as_answer, bool):
data["result_as_answer"] = result_as_answer
if self.agent and hasattr(self.agent, "tools_results"): if self.agent and hasattr(self.agent, "tools_results"):
self.agent.tools_results.append(data) self.agent.tools_results.append(data)
@@ -448,7 +453,7 @@ class ToolUsage:
self.task.increment_tools_errors() self.task.increment_tools_errors()
if self.agent and self.agent.verbose: if self.agent and self.agent.verbose:
self._printer.print(content=f"\n\n{e}\n", color="red") self._printer.print(content=f"\n\n{e}\n", color="red")
return ToolUsageErrorException( # type: ignore # Incompatible return value type (got "ToolUsageErrorException", expected "ToolCalling | InstructorToolCalling") return ToolUsageErrorException(
f"{self._i18n.errors('tool_usage_error').format(error=e)}\nMoving on then. {self._i18n.slice('format').format(tool_names=self.tools_names)}" f"{self._i18n.errors('tool_usage_error').format(error=e)}\nMoving on then. {self._i18n.slice('format').format(tool_names=self.tools_names)}"
) )
return self._tool_calling(tool_string) return self._tool_calling(tool_string)

View File

@@ -0,0 +1,71 @@
from typing import Any, Dict, List, Optional, Union
from unittest.mock import MagicMock, patch
import pytest
from crewai import Agent, Crew, Task
from crewai.agents.tools_handler import ToolsHandler
from crewai.tasks.task_output import TaskOutput
from crewai.tools import BaseTool
from crewai.tools.structured_tool import CrewStructuredTool
from crewai.tools.tool_calling import InstructorToolCalling, ToolCalling
from crewai.tools.tool_usage import ToolUsage
class TestCodeInterpreterTool(BaseTool):
name: str = "Test Code Interpreter"
description: str = "A test tool that simulates code execution."
result_as_answer: bool = False
execution_called: bool = False
def _run(self, code: str = "", libraries_used: List[str] = []) -> str:
self.execution_called = True
return f"Code executed: {code}"
def test_direct_tool_execution():
"""Test that the tool can be executed directly."""
test_tool = TestCodeInterpreterTool()
result = test_tool.run("print('Hello World')")
assert "Code executed" in result
assert test_tool.execution_called
def test_tool_usage_return_types():
"""Test that the ToolUsage methods return the correct types."""
agent_mock = MagicMock()
task_mock = MagicMock()
task_mock.used_tools = 0
tools_handler_mock = MagicMock()
tool_usage = ToolUsage(
tools_handler=tools_handler_mock,
tools=[],
task=task_mock,
function_calling_llm=None,
agent=agent_mock,
action=None
)
i18n_mock = MagicMock()
i18n_mock.slice.return_value.format.return_value = "Tool description"
tool_usage._i18n = i18n_mock
result = "test result"
formatted_result = tool_usage._format_result(result)
assert formatted_result is not None, "_format_result should return the result"
assert isinstance(formatted_result, str), "_format_result should return a string"
result = tool_usage._should_remember_format()
assert isinstance(result, bool), "_should_remember_format should return a boolean"
result = "test result"
remembered_result = tool_usage._remember_format(result)
assert remembered_result is not None, "_remember_format should return the result"
assert isinstance(remembered_result, str), "_remember_format should return a string"
tool_calling_mock = MagicMock()
result = tool_usage._check_tool_repeated_usage(tool_calling_mock)
assert isinstance(result, bool), "_check_tool_repeated_usage should return a boolean"