From 094b3fe6604875907c4962e54f7a0ea85adb7a33 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Thu, 11 Sep 2025 08:45:07 +0000 Subject: [PATCH] Fix critical import error and type-checker issues MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Fix ToolUsageErrorException import in tool_utils.py (renamed to ToolUsageError) - Add proper type guards for fingerprint attribute access - Fix CacheHandler.read() argument type conversion - Resolve variable redefinition conflicts in _use method - All type-checker errors now resolved (mypy passes) - Import test passes successfully - Core MCP tool output functionality verified working Co-Authored-By: João --- src/crewai/tools/tool_usage.py | 15 ++++++++------- src/crewai/utilities/tool_utils.py | 24 ++++++++++++------------ 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/crewai/tools/tool_usage.py b/src/crewai/tools/tool_usage.py index 1c62e78bd..fe3756fb6 100644 --- a/src/crewai/tools/tool_usage.py +++ b/src/crewai/tools/tool_usage.py @@ -8,7 +8,7 @@ from textwrap import dedent from typing import TYPE_CHECKING, Any, Union import json5 -from json_repair import repair_json +from json_repair import repair_json # type: ignore from crewai.agents.tools_handler import ToolsHandler from crewai.events.event_bus import crewai_event_bus @@ -174,7 +174,7 @@ class ToolUsage: "agent": self.agent, } - if self.agent.fingerprint: + if hasattr(self.agent, 'fingerprint') and self.agent.fingerprint: event_data.update(self.agent.fingerprint) if self.task: event_data["task_name"] = self.task.name or self.task.description @@ -183,13 +183,14 @@ class ToolUsage: started_at = time.time() from_cache = False - result = None # type: ignore if self.tools_handler and self.tools_handler.cache: - result = self.tools_handler.cache.read( - tool=calling.tool_name, input=calling.arguments - ) # type: ignore - from_cache = result is not None + cache_result = self.tools_handler.cache.read( + tool=calling.tool_name, input=str(calling.arguments) + ) + from_cache = cache_result is not None + if cache_result is not None: + result = cache_result available_tool = next( ( diff --git a/src/crewai/utilities/tool_utils.py b/src/crewai/utilities/tool_utils.py index eaf065477..c1c20bc66 100644 --- a/src/crewai/utilities/tool_utils.py +++ b/src/crewai/utilities/tool_utils.py @@ -1,24 +1,24 @@ -from typing import Any, Dict, List, Optional +from typing import Any from crewai.agents.parser import AgentAction from crewai.security import Fingerprint from crewai.tools.structured_tool import CrewStructuredTool from crewai.tools.tool_types import ToolResult -from crewai.tools.tool_usage import ToolUsage, ToolUsageErrorException +from crewai.tools.tool_usage import ToolUsage, ToolUsageError from crewai.utilities.i18n import I18N def execute_tool_and_check_finality( agent_action: AgentAction, - tools: List[CrewStructuredTool], + tools: list[CrewStructuredTool], i18n: I18N, - agent_key: Optional[str] = None, - agent_role: Optional[str] = None, - tools_handler: Optional[Any] = None, - task: Optional[Any] = None, - agent: Optional[Any] = None, - function_calling_llm: Optional[Any] = None, - fingerprint_context: Optional[Dict[str, str]] = None, + agent_key: str | None = None, + agent_role: str | None = None, + tools_handler: Any | None = None, + task: Any | None = None, + agent: Any | None = None, + function_calling_llm: Any | None = None, + fingerprint_context: dict[str, str] | None = None, ) -> ToolResult: """Execute a tool and check if the result should be treated as a final answer. @@ -50,7 +50,7 @@ def execute_tool_and_check_finality( fingerprint_obj = Fingerprint.from_dict(fingerprint_context) agent.set_fingerprint(fingerprint_obj) except Exception as e: - raise ValueError(f"Failed to set fingerprint: {e}") + raise ValueError(f"Failed to set fingerprint: {e}") from e # Create tool usage instance tool_usage = ToolUsage( @@ -65,7 +65,7 @@ def execute_tool_and_check_finality( # Parse tool calling tool_calling = tool_usage.parse_tool_calling(agent_action.text) - if isinstance(tool_calling, ToolUsageErrorException): + if isinstance(tool_calling, ToolUsageError): return ToolResult(tool_calling.message, False) # Check if tool name matches