Skip to content

Conversation

@fede-kamel
Copy link

Summary

Adds support for callable class instances and bound methods with beta_tool().

This makes it easier to create tools that need access to instance state or request-scoped context (database sessions, API keys, user context, etc.) without having to wrap everything in closure factories.

Before

def setup_tools(session):
    @beta_tool
    def fetch_product(id: int) -> str:
        return session.get(Product, id)
    return [fetch_product]

tools = setup_tools(db_session)

After

class FetchProduct:
    def __init__(self, session):
        self.session = session

    def __call__(self, id: int) -> str:
        """Fetch a product by ID."""
        return self.session.get(Product, id)

tool = beta_tool(FetchProduct(db_session), name="fetch_product")

Bound methods also work:

tool = beta_tool(my_service.get_weather, name="weather")

Test plan

  • Unit tests for callable class instances
  • Unit tests for bound methods
  • Integration tests with tool_runner
  • All existing tests pass

Closes #1087

Add support for using callable class instances and bound methods with
beta_tool. This enables a more natural pattern for tools that need
access to instance state or request-scoped context.

Previously, users had to wrap their tools in closures to capture state:

    def setup_tools(user_id):
        @beta_tool
        def get_user(id: int) -> str:
            return fetch_user(user_id, id)
        return [get_user]

Now they can use class instances directly:

    class GetUser:
        def __init__(self, user_id):
            self.user_id = user_id

        def __call__(self, id: int) -> str:
            return fetch_user(self.user_id, id)

    tool = beta_tool(GetUser(user_id), name="get_user")

Changes:
- Add _normalize_callable() to extract bound __call__ from instances
- Update BaseFunctionTool to normalize callables before validation
- Add unit tests for callable instances and bound methods
- Add integration tests with tool_runner

Closes anthropics#1087
@fede-kamel fede-kamel requested a review from a team as a code owner January 20, 2026 14:57
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