[BUG] Tool defined in class triggers TypeHintParsingException
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_toolExpected 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.
Source: huggingface/smolagents