Skip to content

feat(agent): add add_hook convenience method for hook registration#1693

Closed
Unshure wants to merge 1 commit intomainfrom
agent-tasks/1686
Closed

feat(agent): add add_hook convenience method for hook registration#1693
Unshure wants to merge 1 commit intomainfrom
agent-tasks/1686

Conversation

@Unshure
Copy link
Member

@Unshure Unshure commented Feb 13, 2026

Motivation

Currently, registering a hook requires accessing the internal hooks registry:

agent.hooks.add_callback(BeforeModelCallEvent, my_callback)

This exposes implementation details and is less discoverable for users. Plugin authors and users who want to add hooks directly need a cleaner, more intuitive API.

Resolves #1686

Public API Changes

The Agent class now has an add_hook method that provides a cleaner API for registering hook callbacks:

# Before - accessing internal registry
agent.hooks.add_callback(BeforeModelCallEvent, my_callback)

# After - cleaner public API with explicit event type
agent.add_hook(my_callback, BeforeModelCallEvent)

# After - event type inferred from callback type hint
def my_callback(event: BeforeModelCallEvent) -> None:
    print(f"Model call for: {event.agent.name}")

agent.add_hook(my_callback)  # event_type inferred automatically

The add_callback method on HookRegistry also supports type inference:

# With explicit event type
registry.add_callback(BeforeModelCallEvent, my_handler)

# With event type inferred from type hint
registry.add_callback(my_typed_handler)

When event_type is not provided, it's inferred from the callback's first parameter type hint. A ValueError is raised with a helpful message if inference fails.

Use Cases

  • Direct hook registration: Users can register callbacks without accessing internal hooks registry
  • Plugin development: Plugin init_plugin implementations can use this cleaner API
  • Quick prototyping: Lambda callbacks for debugging or logging are easier to add
  • Type-safe registration: Typed callbacks automatically register for the correct event type

@Unshure Unshure marked this pull request as draft February 13, 2026 20:26
@codecov
Copy link

codecov bot commented Feb 13, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions
Copy link

Assessment: Approve

Clean implementation of a convenience method that improves hook registration DX. The code is well-tested and documented.

Review Notes
  • API Design: The method signature mirrors HookRegistry.add_callback which provides consistency. Per API_BAR_RAISING.md, this is a minimal change (convenience wrapper) where PR review is sufficient without requiring designated API reviewer.
  • Testing: Good coverage of sync callbacks, async callbacks, and delegation verification.
  • Type Annotations: Note that TEvent is imported from ..hooks.registry rather than the public strands.hooks module, which is fine for internal use but users won't have access to this TypeVar for their own generic type annotations (they don't typically need it).

Nice addition that aligns with SDK tenet #4 (the obvious path is the happy path).

@Unshure
Copy link
Member Author

Unshure commented Feb 13, 2026

/strands

@Unshure
Copy link
Member Author

Unshure commented Feb 13, 2026

/strands

@Unshure
Copy link
Member Author

Unshure commented Feb 13, 2026

/strands

@Unshure
Copy link
Member Author

Unshure commented Feb 13, 2026

/strands Support a list of event_types, and a union type for the callback event type. Any, or BaseEvent will support all lifecycle events

@Unshure
Copy link
Member Author

Unshure commented Feb 13, 2026

/strands Lets actually just keep it to a single event for now. We can add a list or a union later

Add a public add_hook method to the Agent class that provides a cleaner
API for registering hook callbacks. This is a convenience method that
delegates to self.hooks.add_callback but provides a more discoverable
interface for users.

Changes:
- Add Agent.add_hook(callback, event_type=None) method
- Support inferring event_type from callback's type hint
- Update HookRegistry.add_callback to support type inference
- Raise clear ValueError when type cannot be inferred

The method:
- Accepts both sync and async callbacks
- Uses proper type hints with TEvent and HookCallback
- Includes comprehensive Google-style docstring with examples

Resolves #1686
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Add add_hook Convenience Method to Agent

2 participants