set_node_defaults(
self,
*,
retry_policy: RetryPolicy | Sequence[RetryPolicy] | None =| Name | Type | Description |
|---|---|---|
retry_policy | RetryPolicy | Sequence[RetryPolicy] | None | Default: NoneDefault retry policy for nodes that don't specify
their own via |
cache_policy | CachePolicy | None | Default: NoneDefault cache policy for nodes that don't specify
their own via |
error_handler | StateNode[Any, ContextT] | None | Default: None |
timeout | float | timedelta | TimeoutPolicy | None | Default: None |
Set default node policies that apply to every node in this graph.
Per-node values passed to add_node always take precedence over these
defaults. Defaults are applied at compile() time. Policies set here
are not inherited by subgraphs.
retry_policy and timeout defaults apply to all nodes,
including error-handler nodes. cache_policy and error_handler
defaults only apply to regular nodes -- caching error-handler results
is unsafe, and handlers must never catch themselves.
Example:
graph = (
StateGraph(State)
.set_node_defaults(
retry_policy=RetryPolicy(max_attempts=3),
error_handler=my_fallback_handler,
)
.add_node("a", node_a)
.add_node("b", node_b, retry_policy=custom_retry) # overrides default
.add_edge(START, "a")
.compile()
)Default error handler invoked when any regular node
raises and does not have its own error_handler set via
add_node. The handler is not invoked when an
error-handler node itself raises -- handler failures fail the
run.
Default timeout policy for nodes that don't specify their
own via add_node(..., timeout=...). Also applies to
error-handler nodes. Accepts a TimeoutPolicy, a number of
seconds (float), or a timedelta.