-
Notifications
You must be signed in to change notification settings - Fork 2k
fix(tool): extract function body in more robust way #1627
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
Conversation
There was a problem hiding this 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:
...
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🥰 |
There was a problem hiding this 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.
There was a problem hiding this 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 |
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>
There was a problem hiding this 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.
Fixes #1626