-
Notifications
You must be signed in to change notification settings - Fork 16.1k
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
core: Cache RunnableLambda __repr__ #29199
core: Cache RunnableLambda __repr__ #29199
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 1 Skipped Deployment
|
@@ -4351,6 +4351,8 @@ def __init__( | |||
except AttributeError: | |||
pass | |||
|
|||
self.repr: str | None = None |
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.
should this be private? i.e., self._repr
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.
Of course!
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.
Done in f99af05
return f"RunnableLambda(afunc={get_lambda_source(self.afunc) or '...'})" | ||
else: | ||
return "RunnableLambda(...)" | ||
if self._repr is None: |
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.
similar comment to last one - why not decorate with @functools.lru_cache
instead?
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.
Using lru_cache
on instance methods can lead to memory leaks (as self
is hard referenced by the cache)
See https://docs.astral.sh/ruff/rules/cached-instance-method/
Do you prefer the solution with an lru_cache
external function or the attribute memoizer ?
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.
TIL
# Conflicts: # libs/core/langchain_core/runnables/base.py
RunnableLambda
's__repr__
may do costly OS operation by callingget_lambda_source
.So it's better to cache it.
See #29043