diff --git a/libs/core/langchain_core/runnables/base.py b/libs/core/langchain_core/runnables/base.py index 893f393d8b1747..8ff04202549a12 100644 --- a/libs/core/langchain_core/runnables/base.py +++ b/libs/core/langchain_core/runnables/base.py @@ -4351,6 +4351,8 @@ def __init__( except AttributeError: pass + self._deps: list[Runnable] | None = None + @property @override def InputType(self) -> Any: @@ -4469,20 +4471,21 @@ def deps(self) -> list[Runnable]: The dependencies of this Runnable. If the function has nonlocal variables that are Runnables, they are considered dependencies. """ - if hasattr(self, "func"): - objects = get_function_nonlocals(self.func) - elif hasattr(self, "afunc"): - objects = get_function_nonlocals(self.afunc) - else: - objects = [] - - deps: list[Runnable] = [] - for obj in objects: - if isinstance(obj, Runnable): - deps.append(obj) - elif isinstance(getattr(obj, "__self__", None), Runnable): - deps.append(obj.__self__) - return deps + if self._deps is None: + self._deps = [] + if hasattr(self, "func"): + objects = get_function_nonlocals(self.func) + elif hasattr(self, "afunc"): + objects = get_function_nonlocals(self.afunc) + else: + objects = [] + + for obj in objects: + if isinstance(obj, Runnable): + self._deps.append(obj) + elif isinstance(getattr(obj, "__self__", None), Runnable): + self._deps.append(obj.__self__) + return self._deps @property def config_specs(self) -> list[ConfigurableFieldSpec]: