Skip to content
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

Merged
merged 4 commits into from
Jan 23, 2025

Conversation

cbornet
Copy link
Collaborator

@cbornet cbornet commented Jan 14, 2025

RunnableLambda's __repr__ may do costly OS operation by calling get_lambda_source.
So it's better to cache it.
See #29043

@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Jan 14, 2025
Copy link

vercel bot commented Jan 14, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
langchain ⬜️ Ignored (Inspect) Visit Preview Jan 23, 2025 6:30pm

@dosubot dosubot bot added the 🤖:nit Small modifications/deletions, fixes, deps or improvements to existing code or docs label Jan 14, 2025
@@ -4351,6 +4351,8 @@ def __init__(
except AttributeError:
pass

self.repr: str | None = None
Copy link
Collaborator

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course!

Copy link
Collaborator Author

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:
Copy link
Member

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?

Copy link
Collaborator Author

@cbornet cbornet Jan 16, 2025

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 ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL

@efriis efriis self-assigned this Jan 16, 2025
# Conflicts:
#	libs/core/langchain_core/runnables/base.py
@dosubot dosubot bot added the lgtm PR looks good. Use to confirm that a PR is ready for merging. label Jan 23, 2025
@ccurme ccurme enabled auto-merge (squash) January 23, 2025 18:31
@ccurme ccurme merged commit b6ae7ca into langchain-ai:master Jan 23, 2025
71 checks passed
@cbornet cbornet deleted the cache-runnablelambda-repr branch January 23, 2025 18:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm PR looks good. Use to confirm that a PR is ready for merging. 🤖:nit Small modifications/deletions, fixes, deps or improvements to existing code or docs size:S This PR changes 10-29 lines, ignoring generated files.
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants