Skip to content

Try catch step

A composite pipeline step that implements try-catch-fallback logic.

This step executes a primary sequence of steps and, if an exception occurs that matches the specified catch filter, transparently switches execution to a fallback branch.

TryCatchStep(name, body, fallback=None, catch=(Exception,), caught_exception_state=None, retry_config=None, error_handler=None, cache=None)

Bases: BaseCompositeStep

A composite step that executes a primary branch and falls back on exception.

This step attempts to execute a primary branch (body). If any step in the body raises an exception that matches the catch filter, execution is diverted to the fallback branch. The exception can optionally be saved to the state.

Attributes:

Name Type Description
name str

A unique identifier for this pipeline step.

body list[BasePipelineStep]

The primary sequence of steps to execute.

fallback list[BasePipelineStep]

The sequence of steps to execute on failure.

catch tuple[type[Exception], ...]

Tuple of exception types to catch.

caught_exception_state str | None

State key to store the caught exception message.

retry_policy RetryPolicy | None

Configuration for retry behavior.

Initialize a new TryCatchStep.

Parameters:

Name Type Description Default
name str

A unique identifier for the pipeline step.

required
body BasePipelineStep | list[BasePipelineStep]

The primary steps to execute.

required
fallback BasePipelineStep | list[BasePipelineStep] | None

The steps to execute if the body fails. Defaults to None.

None
catch tuple[type[Exception], ...]

Exceptions to catch. Defaults to (Exception,).

(Exception,)
caught_exception_state str | None

State key to store the exception message. Defaults to None.

None
retry_config RetryConfig | None

Configuration for retry behavior. Defaults to None.

None
error_handler BaseStepErrorHandler | None

Strategy to handle errors. Defaults to None.

None
cache CacheConfig | None

Configuration for cache. Defaults to None.

None

Raises:

Type Description
TypeError

If body is empty, contains invalid items, or if catch is invalid.

apply_exclusions(exclusions)

Applies exclusion logic to this step and its children recursively.

Parameters:

Name Type Description Default
exclusions ExclusionSet

The exclusions to evaluate.

required

execute(state, runtime, config=None) async

Executes the body branch and falls back if a matching exception occurs.

Parameters:

Name Type Description Default
state PipelineState

The current state of the pipeline.

required
runtime Runtime

Runtime information for this step's execution.

required
config RunnableConfig | None

The runnable configuration.

None

Returns:

Type Description
dict[str, Any] | None

dict[str, Any] | None: The accumulated state updates.