Skip to content

Fix @tool decorator for remote Python executor #1334

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

Merged
merged 3 commits into from
Jun 3, 2025

Conversation

tobiasofsn
Copy link
Contributor

@tobiasofsn tobiasofsn commented May 15, 2025

Fixes an issue where @tool decorated tools would not work with the docker executor.

The decorated function would previously not be properly transformed into a forward method for the SimpleTool constructed by instance_to_source.

Code to reproduce the issue:

@tool
def magic_number_tool() -> int:
    """
    This tool returns the magic number.
    """
    return 12345

agent = CodeAgent(
    model=model,
    tools=[magic_number_tool],
    executor_type="docker"
)

agent.run("Get the magic number.")

Fixes #1345.

@tobiasofsn
Copy link
Contributor Author

@aymeric-roucher and @albertvillanova, what do you think about this solution?

I think the problem before was that getting the func, in this case the forward method, from cls.__dict__.items(), doesn't preserve the __source__ field which is set in tools.py#L977. From my testing, using getattr works better in this regard as it preserves the __source__ field. When the __source__ field is preserved it is used by get_source to successfully transform the function into code.

So with the fix the resulting tool source for my example becomes:

class SimpleTool(Tool):
    name = "magic_number_tool"
    description = "This tool returns the magic number."
    inputs = {}
    output_type = "integer"

    def __init__(self):
        self.is_initialized = True

    def forward(self) -> int:
        """
        This tool returns the magic number.
        """
        return 12345

Instead of:

class SimpleTool(Tool):
    name = "magic_number_tool"
    description = "This tool returns the magic number."
    inputs = {}
    output_type = "integer"

    def __init__(self):
        self.is_initialized = True

    @tool
    def magic_number_tool() -> int:
        """
        This tool returns the magic number.
        """
        return 12345

@albertvillanova
Copy link
Member

albertvillanova commented Jun 2, 2025

Thanks for the proposed solution, @tobiasofsn.

I think the error is caused by other part of the code. See comment: #1345 (comment)

Please, feel free to reopen if you think otherwise.

@albertvillanova
Copy link
Member

albertvillanova commented Jun 3, 2025

Hi @tobiasofsn.
After investigating further, I think the issue is caused by the line you proposed to fix. Thanks!
That said, I'd approach the fix a bit differently, so I've reopened your PR and will push a commit with the updated solution.

Copy link
Member

@albertvillanova albertvillanova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!
Thanks for your contribution!

@albertvillanova albertvillanova merged commit 0107bbc into huggingface:main Jun 3, 2025
3 checks passed
@tobiasofsn tobiasofsn deleted the fix-tool-decorator branch June 5, 2025 22:33
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.

[BUG] Cannot use @tool decorator with remote Python executor
2 participants