diff --git a/src/crewai/tools/base_tool.py b/src/crewai/tools/base_tool.py index dc69b02a2..2d6526266 100644 --- a/src/crewai/tools/base_tool.py +++ b/src/crewai/tools/base_tool.py @@ -244,9 +244,13 @@ def to_langchain( return [t.to_structured_tool() if isinstance(t, BaseTool) else t for t in tools] -def tool(*args): +def tool(*args, result_as_answer=False): """ Decorator to create a tool from a function. + + Args: + *args: Positional arguments, either the function to decorate or the tool name. + result_as_answer: Flag to indicate if the tool result should be used as the final agent answer. """ def _make_with_name(tool_name: str) -> Callable: @@ -272,6 +276,7 @@ def tool(*args): description=f.__doc__, func=f, args_schema=args_schema, + result_as_answer=result_as_answer, ) return _make_tool diff --git a/tests/tools/test_base_tool.py b/tests/tools/test_base_tool.py index 51eb05b75..a1eb7a407 100644 --- a/tests/tools/test_base_tool.py +++ b/tests/tools/test_base_tool.py @@ -100,3 +100,25 @@ def test_default_cache_function_is_true(): my_tool = MyCustomTool() # Assert all the right attributes were defined assert my_tool.cache_function() + + +def test_result_as_answer_in_tool_decorator(): + @tool("Tool with result as answer", result_as_answer=True) + def my_tool_with_result_as_answer(question: str) -> str: + """This tool will return its result as the final answer.""" + return question + + assert my_tool_with_result_as_answer.result_as_answer is True + + converted_tool = my_tool_with_result_as_answer.to_structured_tool() + assert converted_tool.result_as_answer is True + + @tool("Tool with default result_as_answer") + def my_tool_with_default(question: str) -> str: + """This tool uses the default result_as_answer value.""" + return question + + assert my_tool_with_default.result_as_answer is False + + converted_tool = my_tool_with_default.to_structured_tool() + assert converted_tool.result_as_answer is False