mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-03 19:57:51 +00:00
core: Cache RunnableLambda __repr__ (#29199)
`RunnableLambda`'s `__repr__` may do costly OS operation by calling `get_lambda_source`. So it's better to cache it. See #29043 --------- Co-authored-by: Chester Curme <chester.curme@gmail.com>
This commit is contained in:
parent
618e550f06
commit
b6ae7ca91d
@ -4347,6 +4347,8 @@ class RunnableLambda(Runnable[Input, Output]):
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
self._repr: Optional[str] = None
|
||||
|
||||
@property
|
||||
@override
|
||||
def InputType(self) -> Any:
|
||||
@ -4525,14 +4527,18 @@ class RunnableLambda(Runnable[Input, Output]):
|
||||
|
||||
def __repr__(self) -> str:
|
||||
"""A string representation of this Runnable."""
|
||||
if self._repr is None:
|
||||
if hasattr(self, "func") and isinstance(self.func, itemgetter):
|
||||
return f"RunnableLambda({str(self.func)[len('operator.') :]})"
|
||||
self._repr = f"RunnableLambda({str(self.func)[len('operator.') :]})"
|
||||
elif hasattr(self, "func"):
|
||||
return f"RunnableLambda({get_lambda_source(self.func) or '...'})"
|
||||
self._repr = f"RunnableLambda({get_lambda_source(self.func) or '...'})"
|
||||
elif hasattr(self, "afunc"):
|
||||
return f"RunnableLambda(afunc={get_lambda_source(self.afunc) or '...'})"
|
||||
self._repr = (
|
||||
f"RunnableLambda(afunc={get_lambda_source(self.afunc) or '...'})"
|
||||
)
|
||||
else:
|
||||
return "RunnableLambda(...)"
|
||||
self._repr = "RunnableLambda(...)"
|
||||
return self._repr
|
||||
|
||||
def _invoke(
|
||||
self,
|
||||
|
Loading…
Reference in New Issue
Block a user