Skip to content

Fix issue #2798: Remove duplicate tool results in messages #2799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

devin-ai-integration[bot]
Copy link
Contributor

Fix issue #2798: Remove duplicate tool results in messages

Description

This PR fixes issue #2798 where tool results were being duplicated in the LLM prompt, increasing token usage and latency.

The issue was caused by tool results being added to messages twice:

  1. First directly in agent_utils.py with messages.append({"role": "assistant", "content": tool_result.result})
  2. Then again when the formatted_answer.text (which already includes the tool result with "Observation:" prefix) is appended to messages

Changes

  • Removed the direct append of tool results to messages in agent_utils.py
  • Added a test to verify that tool results are not duplicated in messages

Testing

  • Added a test that verifies tool results are not duplicated in messages
  • The test confirms that the tool result appears at most once in the messages array

Link to Devin run

https://app.devin.ai/sessions/98b28116a3834a1db6aad90ff8ea278c

Requested by

Joe Moura ([email protected])

Copy link
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

@joaomdmoura
Copy link
Collaborator

Disclaimer: This review was made by a crew of AI Agents.

Code Review Comment for PR #2799: Fix Tool Result Duplication

Overview

This pull request successfully addresses issue #2798 by removing duplicate tool results in messages and adding comprehensive test coverage. Noteworthy modifications include:

  1. File Modified:
    • src/crewai/utilities/agent_utils.py
    • tests/test_tool_result_duplication.py (newly created)

Detailed Analysis

1. Changes in agent_utils.py

  • Modifications:

    • The code responsible for appending tool results to messages has been removed to prevent duplication. This change effectively streamlines the message handling process.
  • Quality Assessment:

    • ✅ The code is concise and focused directly on resolving the stated issue.
    • ✅ No new complexities have been introduced, maintaining the original behavior and function signatures.
  • Improvement Suggestion:

    • Adding a comment to clarify the rationale behind the removal can prevent future misunderstandings. For example:
      # Tool results are already included in the formatted answer, 
      # Duplication in messages is unnecessary and leads to token bloat.

2. Changes in test_tool_result_duplication.py

  • Strengths:

    • Robust coverage and clear structure with well-documented test cases.
    • Effective use of mocking demonstrates high-quality test practices.
  • Code Quality Observation:

  • Improvement Suggestions:

    1. Add Type Hints:

      • To ensure better readability and maintainability, consider adding type hints to the test functions:
        def test_tool_result_not_duplicated_in_messages() -> None:
    2. Implement Parameterized Test Cases:

      • This will facilitate testing various inputs efficiently:
        @pytest.mark.parametrize("tool_result", [
            "Sample tool result",
            "Test with special characters: @$%^&*()",
            "Long multi-line\nresult"
        ])
        def test_tool_result_with_various_inputs(tool_result: str) -> None:
    3. Edge Case Scenarios:

      • Include additional tests for empty inputs and special characters:
        def test_tool_result_with_empty_message() -> None:
            # Add implementation for testing empty messages
        
        def test_tool_result_with_special_characters() -> None:
            # Add implementation for testing special characters handling

Overall Assessment

  • The PR effectively tackles the duplication issue and enhances the reliability of message handling through robust test coverage.
  • While the changes are commendable, implementing the suggested improvements could lead to increased code maintainability and reliability.

Recommendations

  1. Approval: The pull request should be accepted as it meets the requirements and fixes the highlighted issues.
  2. Future Improvements: Encourage the incorporation of the proposed improvements in subsequent PRs, focusing on documentation and test enhancement.

Security Considerations

  • No security vulnerabilities were identified in this PR. Changes are deemed safe in terms of user data handling and authentication mechanisms.

In conclusion, this PR represents a step forward in addressing the tool result duplication while ensuring the changes maintain quality standards in code and testing practices. The ongoing attention to documentation and testing will significantly benefit future development efforts.

Copy link
Contributor Author

Closing due to inactivity for more than 7 days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant