Skip to content

Remove reverse callback ordering for After* hook events in Python 2.0 #2282

@zastrowm

Description

@zastrowm

Summary

The Python SDK currently runs callbacks for all After* hook events (AfterInvocationEvent, AfterToolCallEvent, AfterModelCallEvent, AfterNodeCallEvent, AfterMultiAgentInvocationEvent) in reverse registration order via the should_reverse_callbacks property. This follows stack-based cleanup semantics (last registered = first to run during teardown), but in practice it creates confusing behavior for users who expect hooks to execute in the order they were registered.

The TypeScript SDK addressed this in sdk-typescript#1005 by introducing explicit ordering rather than implicit reversal. For Python 2.0 (breaking change window), the proposal is to remove the reverse ordering and run all hook callbacks — including After* events — in registration order, aligning with the TypeScript SDK's approach and reducing user confusion around hook execution semantics.

Affected code

  • sdk-python/src/strands/hooks/registry.pyget_callbacks_for() checks event.should_reverse_callbacks and yields in reversed order when True
  • sdk-python/src/strands/hooks/events.py — Five event classes override should_reverse_callbacks to return True:
    • AfterInvocationEvent
    • AfterToolCallEvent
    • AfterModelCallEvent
    • AfterNodeCallEvent
    • AfterMultiAgentInvocationEvent

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions