Skip to content

[BUG] Cannot use @tool decorator with remote Python executor #1345

Closed
@tobiasofsn

Description

@tobiasofsn

Describe the bug
Using a @tool decorated tool with the docker executor results in an AgentError.

Code to reproduce the error

import os
from smolagents import CodeAgent, OpenAIServerModel, tool

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

model = OpenAIServerModel(
    model_id="gpt-4.1-mini",
    api_base="https://api.openai.com/v1",
    api_key=os.environ["OPENAI_API_KEY"]
)

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

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

Error logs (if any)

smolagents.utils.AgentError: ---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[2], line 22
     18     def forward(self, *args, **kwargs):
     19         pass # to be implemented in child class
---> 22 class SimpleTool(Tool):
     23     name = "magic_number_tool"
     24     description = "This tool returns the magic number."

Cell In[2], line 31, in SimpleTool()
     28 def __init__(self):
     29     self.is_initialized = True
---> 31 @tool
     32 def magic_number_tool() -> int:
     33     """
     34     This tool returns the magic number.
     35     """
     36     return 12345

NameError: name 'tool' is not defined

Expected behavior
Code shall execute without throwing an AgentError.

Packages version:
smolagents==1.16.1

Additional context
It seems that the issue is related to the @tool decorated function not being transformed into a forward method for the SimpleTool constructed by instance_to_source. I have proposed a fix in #1334.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions