diff --git a/src/crewai/agents/langchain_agent_adapter.py b/src/crewai/agents/langchain_agent_adapter.py index 14bc5d382..6453b33ae 100644 --- a/src/crewai/agents/langchain_agent_adapter.py +++ b/src/crewai/agents/langchain_agent_adapter.py @@ -280,8 +280,12 @@ class LangChainAgentAdapter(BaseAgent): "LangGraph library not found. Please run `uv add langgraph` to add LangGraph support." ) from e - # Ensure raw_tools is always a list, even if tools and self.tools are None. - raw_tools = tools or self.tools or [] + # Ensure raw_tools is always a list. + raw_tools: List[Any] = ( + tools + if tools is not None + else (self.tools if self.tools is not None else []) + ) # Fallback: if raw_tools is still empty, try to extract them from the wrapped langchain agent. if not raw_tools: if hasattr(self.langchain_agent, "agent") and hasattr( @@ -293,14 +297,14 @@ class LangChainAgentAdapter(BaseAgent): used_tools = [] try: - # Import the CrewAI Tool class. - from crewai.tools.base_tool import Tool as CrewTool + # Import the CrewAI Tool class and name it differently to avoid type assignment issues. + from crewai.tools.base_tool import Tool as CrewToolClass except ImportError: - CrewTool: Optional[Type[BaseTool]] = None # Explicitly annotate as Optional + CrewToolClass = None # No type annotation here for tool in raw_tools: # If the tool is a CrewAI Tool, convert it to a LangChain compatible tool. - if CrewTool is not None and isinstance(tool, CrewTool): + if CrewToolClass is not None and isinstance(tool, CrewToolClass): used_tools.append(tool.to_langchain()) else: used_tools.append(tool) diff --git a/src/crewai/tools/base_tool.py b/src/crewai/tools/base_tool.py index bbe21302b..52cd0f382 100644 --- a/src/crewai/tools/base_tool.py +++ b/src/crewai/tools/base_tool.py @@ -19,6 +19,13 @@ from crewai.tools.structured_tool import CrewStructuredTool warnings.filterwarnings("ignore", category=PydanticDeprecatedSince20) +# Define a helper function with an explicit signature +def default_cache_function( + _args: Optional[Any] = None, _result: Optional[Any] = None +) -> bool: + return True + + class BaseTool(BaseModel, ABC): class _ArgsSchemaPlaceholder(PydanticBaseModel): pass @@ -37,9 +44,9 @@ class BaseTool(BaseModel, ABC): description_updated: bool = False """Flag to check if the description has been updated.""" cache_function: Callable[[Optional[Any], Optional[Any]], bool] = ( - lambda _args=None, _result=None: True + default_cache_function ) - """Function that will be used to determine if the tool should be cached, should return a boolean. If None, the tool will be cached.""" + """Function used to determine if the tool should be cached.""" result_as_answer: bool = False """Flag to check if the tool should be the final agent answer."""