Skip to content

fix(tools): filter self and cls parameters from @beta_tool schema generation#1128

Open
Ashutosh0x wants to merge 1 commit intoanthropics:mainfrom
Ashutosh0x:fix/beta-tool-class-method-support
Open

fix(tools): filter self and cls parameters from @beta_tool schema generation#1128
Ashutosh0x wants to merge 1 commit intoanthropics:mainfrom
Ashutosh0x:fix/beta-tool-class-method-support

Conversation

@Ashutosh0x
Copy link

fix(tools): filter self and cls parameters from @beta_tool schema generation

Summary

This PR enables class methods and instance methods to be used with the @beta_tool decorator by filtering out self and cls parameters from the generated JSON schema.

Problem

When using @beta_tool on a class method or instance method, the generated JSON schema incorrectly includes self or cls as required parameters, causing validation errors.

Solution

Modified kw_arguments_schema in _beta_functions.py to filter out self and cls parameters from both the properties and required fields of the generated schema.

Example

class MyTools:
    def __init__(self, user_id: int):
        self.user_id = user_id

    @beta_tool
    def get_user_info(self, query: str) -> str:
        """Get user information.
        
        Args:
            query: The query string to search for
        """
        return f"User {self.user_id}: {query}"

# Now works correctly - 'self' is not in the schema
tools = MyTools(user_id=123)
print(tools.get_user_info.input_schema)
# {'properties': {'query': {...}}, 'required': ['query'], ...}

Fixes #1087

…eration

This enables class methods and instance methods to be used with the
@beta_tool decorator by filtering out 'self' and 'cls' parameters
from the generated JSON schema.

Fixes anthropics#1087
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support class methods for tool helpers

1 participant