Skip to content

strands.middleware.types

Middleware type system.

class MiddlewareInputPhase(Generic[TContext, TResult, TEvent])

Defined in: src/strands/middleware/types.py:13

Phase sub-token for Input handlers — transforms context before execution.

def __init__(stage: MiddlewareStage[TContext, TResult, TEvent]) -> None

Defined in: src/strands/middleware/types.py:18

Bind this phase sub-token to its parent stage.

class MiddlewareWrapPhase(Generic[TContext, TResult, TEvent])

Defined in: src/strands/middleware/types.py:24

Phase sub-token for Wrap handlers — full async generator wrap.

def __init__(stage: MiddlewareStage[TContext, TResult, TEvent]) -> None

Defined in: src/strands/middleware/types.py:29

Bind this phase sub-token to its parent stage.

class MiddlewareOutputPhase(Generic[TContext, TResult, TEvent])

Defined in: src/strands/middleware/types.py:35

Phase sub-token for Output handlers — transforms result after execution.

def __init__(stage: MiddlewareStage[TContext, TResult, TEvent]) -> None

Defined in: src/strands/middleware/types.py:40

Bind this phase sub-token to its parent stage.

class MiddlewareStage(Generic[TContext, TResult, TEvent])

Defined in: src/strands/middleware/types.py:46

A stage token identifying a middleware interception point.

Only the SDK’s built-in tokens (InvokeModelStage, ExecuteToolStage) are invoked; constructing a custom stage is unsupported.

Attributes:

  • name - Human-readable name for debugging and logging.
  • result_type - The wrapper class Output handlers receive and return (TResult).
  • result_event - The event class that is this stage’s result; the registry and the call sites select it from the stream by type, so Wrap handlers may yield other events before or after it.
def __init__(name: str, *, result_type: type[TResult],
result_event: type[Any]) -> None

Defined in: src/strands/middleware/types.py:62

Create a stage token with its Input/Wrap/Output phase sub-tokens.

def __repr__() -> str

Defined in: src/strands/middleware/types.py:71

Return a debug representation naming the stage.

def __hash__() -> int

Defined in: src/strands/middleware/types.py:75

Hash by identity so each stage token is a distinct registry key.

def __eq__(other: object) -> bool

Defined in: src/strands/middleware/types.py:79

Compare by identity — a stage token equals only itself.