Skip to content

Fix: Add cache_function of a CrewStructuredTool #3078

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

vria
Copy link

@vria vria commented Jun 27, 2025

Problem

When tools are added, cache_function parameter is silently dropped. This is because BaseTool is converted to CrewStructuredTool and the latter does not contain cache_function. As a result, tools like list_directory appear to be permanently cached, regardless of configuration.

This is a huge problem to my flow because an agent verifies the existence of newly created files and directories. It calls list_directory tool with the same arguments at different points in time, expecting different results. Due to caching, the result never changes.

Solution

Add cache_function to CrewStructuredTool.

Test case

Tool:

def no_cache_tool(arguments: dict, result: str) -> bool:
    return False

tool = Tool(
    # ...
    cache_function=no_cache_tool
)

Task:

test_tools_task = Task(
    description="""
1. check that non_existent_directory not exist using list_directory tool
2. create non_existent_directory/.gitkeep file
3. check that non_existent_directory exist using list_directory tool
YOU MUST use exactly the same arguments when you run list_directory two times
"""
)

before.log
after.log

@joaomdmoura
Copy link
Collaborator

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

Code Review Comment for PR #3078

Overview

The pull request introduces the cache_function support for CrewStructuredTool within the base_tool.py and structured_tool.py files. This enhancement aims to provide greater control over caching functionality for structured tools. Below are my observations and recommendations based on the changes.

Code Quality Findings

1. base_tool.py

  • Changes: The cache_function parameter was added to the to_structured_tool() method.
  • Issues:
    • The cache_function parameter lacks type hints, compromising code clarity and static analysis support.
    • The documentation for this method does not reflect the new parameter, which could mislead users regarding its functionality.

Improvement Suggestions:

def to_structured_tool(self) -> CrewStructuredTool:
    """Convert to CrewStructuredTool.
    
    Returns:
        CrewStructuredTool: A new instance with all properties transferred.
    """
    return CrewStructuredTool(
        name=self.name,
        description=self.description,
        func=self.func,
        tools=self.tools,
        llm=self.llm,
        result_as_answer=self.result_as_answer,
        max_usage_count=self.max_usage_count,
        current_usage_count=self.current_usage_count,
        cache_function=self.cache_function,  # Ensure proper type hint is added
    )

2. structured_tool.py

  • Changes: The cache_function parameter was added to the __init__ method with a property initialization change.
  • Issues:
    • The constructor does not validate whether cache_function is callable, which could lead to runtime errors.
    • Existing docstring lacks specificity regarding the expected behavior and return type of cache_function.

Improvement Suggestions:

class CrewStructuredTool:
    def __init__(
        self,
        ...
        cache_function: Optional[Callable[[], bool]] = None,
    ) -> None:
        """Initialize the structured tool.
        
        Args:
            ...
            cache_function: Optional callable that returns a boolean to determine if the tool's result
                          should be cached. If None, default caching behavior is used.
        """
        if cache_function is not None and not callable(cache_function):
            raise ValueError("cache_function must be a callable or None")
        self.cache_function = cache_function

Historical Context & Related PRs

Unfortunately, I was unable to directly access related pull requests or historical changes due to limitations in the tool usage. However, it's generally advisable to review previous implementations of similar caching mechanisms and any discussions around them. This will help ensure adherence to established patterns and practices.

Implications for Related Files

The change to add caching functionality may require updates in any files that interact with CrewStructuredTool. Analyzing impact on tests and other integrations will be crucial to maintain functional accuracy.

Recommendations

  1. Type Hinting: Ensure all parameters and return types are appropriately annotated. This will enhance readability and assist with static analysis.
  2. Input Validation: Implement validation for the cache_function callable within the constructor to immediately raise errors on incorrect usage.
  3. Documentation Updates: Update both method-level and class-level documentation to encompass the new caching functionality and provide usage examples.
  4. Unit Testing: Include comprehensive unit tests that validate the behavior of the caching logic as well as its interaction with various types of inputs.
  5. Security Considerations: Validate that the cache_function does not access sensitive information, and implement timeout protections where applicable.

Security Considerations

  1. Ensure Encapsulation: Review how caching functions are structured to avoid exposure of sensitive data.
  2. Performance Checks: Validate the performance impact of cache_function. Poor implementations can degrade the overall performance significantly.

With these insights and recommendations, I believe the PR can be improved further to enhance code quality, maintainability, and usability. Thank you for the effort in implementing this valuable enhancement!

@lucasgomide lucasgomide self-requested a review June 27, 2025 12:54
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.

2 participants