Repository navigation
feat(langgraph): add set_node_defaults() to StateGraph #7747
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
50979e8
done
longquanzheng 082c94d
done
longquanzheng 54ad469
refactor: rename set_defaults to set_node_defaults, drop default_erro…
sydney-runkle 6212f1c
refactor: inline _apply_node_defaults into compile()
sydney-runkle d04b1c4
refactor(langgraph): replace set_node_defaults with StateGraph(node_d…
sydney-runkle ceb6514
Revert "refactor(langgraph): replace set_node_defaults with StateGrap…
longquanzheng fbea83c
apply retry/timieout to error handler
longquanzheng 48c5baf
done
longquanzheng baf1945
done
longquanzheng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import inspect | ||
|
|
@@ -1094,6 +1094,7 @@ | |
| debug: bool = False, | ||
| name: str | None = None, | ||
| transformers: Sequence[Callable[[tuple[str, ...]], Any]] | None = None, | ||
| default_error_handler: StateNode[Any, ContextT] | None = None, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's remove this arg to |
||
| ) -> CompiledStateGraph[StateT, ContextT, InputT, OutputT]: | ||
| """Compiles the `StateGraph` into a `CompiledStateGraph` object. | ||
|
|
||
|
|
@@ -1133,6 +1134,12 @@ | |
| the standard `StreamTransformer` constructor shape by | ||
| accepting `scope` as their first argument. Appended after the | ||
| built-in stream transformers. | ||
| default_error_handler: Optional fallback node-level error handler | ||
| invoked when any regular node raises and does not have its own | ||
| `error_handler` set via `add_node`. Per-node `error_handler` | ||
| always takes precedence. The default handler is **not** invoked | ||
| when an error-handler node itself raises -- handler failures | ||
| fail the run. Not inherited by subgraphs. | ||
|
|
||
| Returns: | ||
| CompiledStateGraph: The compiled `StateGraph`. | ||
|
|
@@ -1193,10 +1200,35 @@ | |
| key for key, val in self.channels.items() if not is_managed_value(val) | ||
| ] | ||
| ) | ||
| default_handler_node_name: str | None = None | ||
| if default_error_handler is not None: | ||
| _default_eh_name = "__default_error_handler__" | ||
| if _default_eh_name in self.nodes: | ||
| raise ValueError( | ||
| f"Auto-generated default error handler node " | ||
| f"`{_default_eh_name}` already exists." | ||
| ) | ||
| default_handler_node_name = _default_eh_name | ||
| self.nodes[default_handler_node_name] = StateNodeSpec[Any, ContextT]( | ||
| coerce_to_runnable( | ||
| default_error_handler, # type: ignore[arg-type] | ||
| name=default_handler_node_name, | ||
| trace=False, | ||
| ), | ||
| metadata=None, | ||
| input_schema=self.state_schema, | ||
| retry_policy=None, | ||
| cache_policy=None, | ||
| is_error_handler=True, | ||
| ) | ||
| for _spec in self.nodes.values(): | ||
| if not _spec.is_error_handler and _spec.error_handler_node is None: | ||
| _spec.error_handler_node = default_handler_node_name | ||
|
|
||
| node_error_handler_map = { | ||
| node_name: spec.error_handler_node | ||
| for node_name, spec in self.nodes.items() | ||
| if spec.error_handler_node is not None | ||
| if not spec.is_error_handler and spec.error_handler_node is not None | ||
| } | ||
|
|
||
| compiled = CompiledStateGraph[StateT, ContextT, InputT, OutputT]( | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok actually, i think might be cleaner in
StateGraph.__init__that way we can just manage this in
add_node:)can we TAL at that alternative?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great feedback. change to
set_defaultsfor now.