Compare commits

...

3 Commits

Author SHA1 Message Date
Devin AI
9fdd0f5f99 Merge main into devin/1744245450-add-result-as-answer-to-tool-decorator
Co-Authored-By: Joe Moura <joao@crewai.com>
2025-04-10 12:57:23 +00:00
Devin AI
047a7bdb11 Merge main into devin/1744245450-add-result-as-answer-to-tool-decorator
Co-Authored-By: Joe Moura <joao@crewai.com>
2025-04-10 12:52:02 +00:00
Devin AI
01bf085c09 Add result_as_answer parameter to @tool decorator (Fixes #2561)
Co-Authored-By: Joe Moura <joao@crewai.com>
2025-04-10 00:38:31 +00:00
2 changed files with 28 additions and 1 deletions

View File

@@ -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

View File

@@ -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