Skip to content

Commit

Permalink
Cache RunnableLambda deps
Browse files Browse the repository at this point in the history
  • Loading branch information
cbornet committed Jan 14, 2025
1 parent c55af44 commit 3194c99
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions libs/core/langchain_core/runnables/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4351,6 +4351,8 @@ def __init__(
except AttributeError:
pass

self._deps: list[Runnable] | None = None

@property
@override
def InputType(self) -> Any:
Expand Down Expand Up @@ -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]:
Expand Down

0 comments on commit 3194c99

Please sign in to comment.