strands.middleware.types
Middleware type system.
MiddlewareInputPhase
Section titled “MiddlewareInputPhase”class MiddlewareInputPhase(Generic[TContext, TResult, TEvent])Defined in: src/strands/middleware/types.py:13
Phase sub-token for Input handlers — transforms context before execution.
__init__
Section titled “__init__”def __init__(stage: MiddlewareStage[TContext, TResult, TEvent]) -> NoneDefined in: src/strands/middleware/types.py:18
Bind this phase sub-token to its parent stage.
MiddlewareWrapPhase
Section titled “MiddlewareWrapPhase”class MiddlewareWrapPhase(Generic[TContext, TResult, TEvent])Defined in: src/strands/middleware/types.py:24
Phase sub-token for Wrap handlers — full async generator wrap.
__init__
Section titled “__init__”def __init__(stage: MiddlewareStage[TContext, TResult, TEvent]) -> NoneDefined in: src/strands/middleware/types.py:29
Bind this phase sub-token to its parent stage.
MiddlewareOutputPhase
Section titled “MiddlewareOutputPhase”class MiddlewareOutputPhase(Generic[TContext, TResult, TEvent])Defined in: src/strands/middleware/types.py:35
Phase sub-token for Output handlers — transforms result after execution.
__init__
Section titled “__init__”def __init__(stage: MiddlewareStage[TContext, TResult, TEvent]) -> NoneDefined in: src/strands/middleware/types.py:40
Bind this phase sub-token to its parent stage.
MiddlewareStage
Section titled “MiddlewareStage”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.
__init__
Section titled “__init__”def __init__(name: str, *, result_type: type[TResult], result_event: type[Any]) -> NoneDefined in: src/strands/middleware/types.py:62
Create a stage token with its Input/Wrap/Output phase sub-tokens.
__repr__
Section titled “__repr__”def __repr__() -> strDefined in: src/strands/middleware/types.py:71
Return a debug representation naming the stage.
__hash__
Section titled “__hash__”def __hash__() -> intDefined in: src/strands/middleware/types.py:75
Hash by identity so each stage token is a distinct registry key.
__eq__
Section titled “__eq__”def __eq__(other: object) -> boolDefined in: src/strands/middleware/types.py:79
Compare by identity — a stage token equals only itself.