feat(strands-py): add optional hook order#2559
Conversation
Add an optional `order` parameter to `add_callback` and `add_hook` methods that controls hook execution priority. Lower values execute first, with named constants: HookOrder.FIRST (-100), HookOrder.DEFAULT (0), HookOrder.LAST (100). Port of strands-agents/sdk-typescript#1005.
Documentation Preview ReadyYour documentation preview has been successfully deployed! Preview URL: https://d3ehv1nix5p99z.cloudfront.net/pr-cms-2559/docs/user-guide/quickstart/overview/ Updated at: 2026-06-02T16:15:29.260Z |
|
Issue: The PR description says "named constants: Suggestion: Update the PR description to match the actual constant names. |
|
Issue: This PR modifies a public API ( Suggestion: Consider adding |
|
Assessment: Comment Clean, well-structured feature port that aligns with the TS SDK and maintains backward compatibility. The implementation is correct and the API design follows the project's tenets (extensible by design, pay-for-play). A few items to address around documentation accuracy and API review process. Review Categories
The implementation itself (bisect.insort, groupby for reversed events, keyword-only param) is well done. |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Description
Adds an optional
orderparameter toadd_callbackandadd_hookmethods that controls hook execution priority. Lower values execute first, with named constants:HookOrder.SDK_FIRST(-100),HookOrder.DEFAULT(0),HookOrder.SDK_LAST(100).Hooks with the same order preserve registration order. For After* events (reversed), lower order still runs first across groups, with registration order reversed within same group.
Port of strands-agents/sdk-typescript#1005.
Usage
Related Issues
strands-agents/sdk-typescript#1005
Documentation PR
Documentation included in this PR — updated the Python tab under "Callback Ordering" in
site/src/content/docs/user-guide/concepts/agents/hooks.mdx.Type of Change
New feature
Testing
hatch run prepareChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.