Compare commits

...

7 Commits

Author SHA1 Message Date
Devin AI
628345e23b Fix ToolResult parameters in test
Co-Authored-By: Joe Moura <joao@crewai.com>
2025-05-09 08:11:17 +00:00
Devin AI
51e34d8920 Fix import sorting with ruff check --fix
Co-Authored-By: Joe Moura <joao@crewai.com>
2025-05-09 08:09:55 +00:00
Devin AI
f9221ea636 Add comment explaining tool result duplication fix
Co-Authored-By: Joe Moura <joao@crewai.com>
2025-05-09 08:05:17 +00:00
Devin AI
d8876dc3bf Fix import sorting in test file
Co-Authored-By: Joe Moura <joao@crewai.com>
2025-05-09 08:04:58 +00:00
Devin AI
d90ba65e07 Add type hints and fix CrewAgentExecutor initialization in test
Co-Authored-By: Joe Moura <joao@crewai.com>
2025-05-09 08:02:49 +00:00
Devin AI
b88ea42059 Add test for tool result duplication fix
Co-Authored-By: Joe Moura <joao@crewai.com>
2025-05-09 07:46:35 +00:00
Devin AI
0bb5f4067c Fix issue #2798: Remove duplicate tool results in messages
Co-Authored-By: Joe Moura <joao@crewai.com>
2025-05-09 07:45:58 +00:00
2 changed files with 96 additions and 2 deletions

View File

@@ -214,8 +214,9 @@ def handle_agent_action_core(
if show_logs:
show_logs(formatted_answer)
if messages is not None:
messages.append({"role": "assistant", "content": tool_result.result})
# Tool results are already included in the formatted answer with "Observation:" prefix,
# so we don't need to append them again to messages to avoid duplication and token bloat.
# This fixes issue #2798 where tool results were being duplicated in the LLM prompt.
return formatted_answer

View File

@@ -0,0 +1,93 @@
from typing import Dict, List, Optional, Union
from unittest.mock import MagicMock, patch
import pytest
from crewai.agent import Agent
from crewai.agents.crew_agent_executor import CrewAgentExecutor
from crewai.agents.tools_handler import ToolsHandler
from crewai.crew import Crew
from crewai.task import Task
from crewai.tools import BaseTool
from crewai.tools.structured_tool import CrewStructuredTool
from crewai.utilities.agent_utils import parse_tools
class TestTool(BaseTool):
name: str = "Test Tool"
description: str = "A test tool to verify tool result duplication is fixed"
def _run(self) -> str:
return "Test tool result"
def test_tool_result_not_duplicated_in_messages() -> None:
"""Test that tool results are not duplicated in messages.
This test verifies the fix for issue #2798, where tool results were being
duplicated in the LLM prompt, increasing token usage and latency.
"""
agent = Agent(
role="Test Agent",
goal="Test the tool result duplication fix",
backstory="I am a test agent",
tools=[TestTool()],
)
task = Task(
description="Use the test tool and return the result",
expected_output="The test tool result",
agent=agent,
)
crew = Crew(agents=[agent], tasks=[task])
structured_tools = parse_tools(agent.tools)
tools_names = ", ".join([t.name for t in structured_tools])
tools_description = "\n".join([t.description for t in structured_tools])
tools_handler = MagicMock(spec=ToolsHandler)
with patch.object(CrewAgentExecutor, '_invoke_loop') as mock_invoke_loop:
executor = CrewAgentExecutor(
agent=agent,
task=task,
crew=crew,
llm=agent.llm,
prompt={"system": "System prompt", "user": "User prompt"},
max_iter=10,
tools=structured_tools,
tools_names=tools_names,
stop_words=[],
tools_description=tools_description,
tools_handler=tools_handler,
callbacks=[],
)
executor.messages = [{"role": "user", "content": "Use the test tool"}]
from crewai.agents.parser import AgentAction
from crewai.tools.tool_types import ToolResult
agent_action = AgentAction(
tool="Test Tool",
tool_input={},
thought="I should use the test tool",
text="I'll use the Test Tool",
)
tool_result = ToolResult(
result="Test tool result",
result_as_answer=False,
)
executor._handle_agent_action(agent_action, tool_result)
tool_result_count = sum(
1 for msg in executor.messages if msg.get("content") == "Test tool result"
)
assert tool_result_count <= 1, "Tool result is duplicated in messages"
observation_text = f"Observation: {tool_result.result}"
assert observation_text in agent_action.text, "Tool result not properly formatted in agent action text"