Skip to content

Conversation

HairlessVillager
Copy link
Contributor

Fixes #1626

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.

Thanks for raising the issue and propose a solution.

I have added some regression tests and it seems there are some case that are not properly fixed:

@tool
@dummy_decorator
def multi_decorator_tool(text: str) -> str:
    ...

becomes

def forward(self, text: str) -> str:
    def multi_decorator_tool(text: str) -> str:
        ...

and
2.

@tool
@dummy_decorator_1
@dummy_decorator_2
def complex_tool(
    text: str,
    multiplier: int = 2,
    separator: str = " ",
    multiline_parameter_1: int = 1_000,
    multiline_parameter_2: int = 2_000,
) -> str:
    ...

becomes

def forward(self, text: str, multiplier: int = 2, separator: str = ' ', multiline_parameter_1: int = 1000, multiline_parameter_2: int = 2000) -> str:
    @dummy_decorator_2
    def complex_tool(
        text: str,
        multiplier: int = 2,
        separator: str = " ",
        multiline_parameter_1: int = 1_000,
        multiline_parameter_2: int = 2_000,
    ) -> str:
        ...

@HairlessVillager
Copy link
Contributor Author

You're right, I've also noticed these issues, but I haven't contributed yet... Let me cherry pick it up.

And very thanks for your testing🥰

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.

Thanks again for addressing the issues in the new tests. That said, it looks like the proposed changes are causing some other tests to fail.

@albertvillanova albertvillanova requested a review from Copilot July 31, 2025 09:57
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes the function body extraction mechanism in the @tool decorator to handle more complex function definitions, including functions with multiple decorators and multiline signatures. The fix addresses issue #1626 where the previous simple line-splitting approach failed to correctly extract function bodies in these cases.

Key changes:

  • Replaces naive line-splitting approach with AST-based parsing for robust function body extraction
  • Adds warning for functions with decorators other than @tool to alert users about potential remote execution issues
  • Adds comprehensive tests covering multiple decorators and multiline function signatures

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
src/smolagents/tools.py Implements robust AST-based function body extraction replacing simple line splitting
tests/test_tools.py Adds comprehensive test cases for multiple decorators and multiline signature scenarios

HairlessVillager and others added 9 commits July 31, 2025 17:59
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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.

Thanks again! Great!

I just made some simplifications and rephrasing of the error/warning messages.

@albertvillanova albertvillanova merged commit 8261f4c into huggingface:main Jul 31, 2025
3 checks passed
@HairlessVillager HairlessVillager deleted the patch-1 branch July 31, 2025 14:00
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: @tool cannot correctly handle the body of some functions in remote Python executor
2 participants