Skip to content

Add method_tool Functionality #94

@nathanmargaglio

Description

@nathanmargaglio

I group my tools into classes, and they leverage attributes of the instantiated object to work properly. The function_tool decorator doesn't seem to support this. For example, the following works (as expected):

@function_tool
def get_random_number() -> int:
    """Get a random number."""
    return random.randint(0, 100)

agent = Agent(
    name="Assistant",
    instructions="You are a helpful assistant",
    tools=[get_random_number]
)
result = Runner.run_sync(agent, input="Get me a random number")
print(result.final_output)

But the following doesn't:

class Tools:
    @function_tool
    def get_random_number(self) -> int:
        """Get a random number."""
        return random.randint(0, 100)

tools = Tools()

agent = Agent(
    name="Assistant",
    instructions="You are a helpful assistant",
    tools=[tools.get_random_number]
)
result = Runner.run_sync(agent, input="Get me a random number")
print(result.final_output)

Running this produces an error like:

openai.BadRequestError: Error code: 400 - {'error': {'message': "Invalid schema for function 'get_random_number': In context=('properties', 'self'), schema must have a 'type' key.", 'type': 'invalid_request_error', 'param': 'tools[0].parameters', 'code': 'invalid_function_parameters'}}

i.e., the self parameter isn't accounted for.

There's probably a ton of different ways of approaching this, but I've been utilizing the class-based tool pattern quite extensively and I've found it works pretty well for keeping tooling clean and simple. I'm happy to consider alternative approaches, but if there was just a way to ignore the self parameter, then I think everything else would just work, no?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions