Skip to content
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

[BUG] Tool defined in class triggers TypeHintParsingException #681

Open
faev999 opened this issue Feb 17, 2025 · 3 comments
Open

[BUG] Tool defined in class triggers TypeHintParsingException #681

faev999 opened this issue Feb 17, 2025 · 3 comments
Labels
bug Something isn't working

Comments

@faev999
Copy link

faev999 commented Feb 17, 2025

Describe the bug
Defining class instance methods as a tool raises:
smolagents._function_type_hints_utils.TypeHintParsingException: Argument self is missing a type hint in function test_tool

Code to reproduce the error
example:

from smolagents import tool, CodeAgent, LiteLLMModel
import os


class Test:
    @tool
    def test_tool(self, text: str) -> str:
        """
        A test tool that takes a text as input and returns a string.

        Args:
            self: The class object.
            text: The input text.

        Returns:
            stringer the output string.
        """
        stringer = f"This is a test tool. The text is: {text}"
        return stringer


model_manager_agent = LiteLLMModel(
    model_id="cerebras/llama-3.3-70b",
    api_key=os.getenv("CEREBRAS_API_KEY"),
)

manager_agent = CodeAgent(
    tools=[],
    model=model_manager_agent,
    # prompt_templates=pepper_prompt_templates,
)
test = Test()

manager_agent.tools["test_tool"] = test.test_tool


manager_agent.run("what's up?")

Error logs (if any)

smolagents\_function_type_hints_utils.py", line 287, in _convert_type_hints_to_json_schema
    raise TypeHintParsingException(f"Argument {param.name} is missing a type hint in function {func.__name__}")
smolagents._function_type_hints_utils.TypeHintParsingException: Argument self is missing a type hint in function test_tool

Expected behavior
I am trying to define tools inside classes, so I have access to other methods and properties

Packages version:
1.9.2

Additional context

This happens with both ToolCalling and Code agents.

I briefly looked inside the docs to see if this case is already covered, but could find anything related except the e2b_example, which is not so close to what I want.

If I modified my example like this def test_tool(self: Any .... then I get:

smolagents\tools.py", line 878, in tool
    new_signature = original_signature.replace(parameters=new_parameters)
Lib\inspect.py", line 3068, in __init__
    raise ValueError(msg)
ValueError: duplicate parameter name: 'self'

That happens because line new_parameters = [inspect.Parameter("self", inspect.Parameter.POSITIONAL_ONLY)] + list( original_signature.parameters.values() )

Is already prepending a "self" parameter (declared as positional only) to the list of parameters from the original function signature.

@faev999 faev999 added the bug Something isn't working label Feb 17, 2025
@faev999
Copy link
Author

faev999 commented Feb 17, 2025

Is there a reason prepending that 'self' in that line new_parameters = [inspect.Parameter("self", inspect.Parameter.POSITIONAL_ONLY)] + list( original_signature.parameters.values() )

I removed that part and ran the test suite (plus my own code) without Failures pointing to that modification

@sysradium
Copy link
Contributor

sysradium commented Feb 18, 2025

Have you tried this PR #627?

@faev999
Copy link
Author

faev999 commented Feb 18, 2025

No, but it seems that's what I am looking for, I will give it a try. Thanks for the info

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants