mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-04 12:18:24 +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:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
self._repr: Optional[str] = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@override
|
@override
|
||||||
def InputType(self) -> Any:
|
def InputType(self) -> Any:
|
||||||
@ -4525,14 +4527,18 @@ class RunnableLambda(Runnable[Input, Output]):
|
|||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
"""A string representation of this Runnable."""
|
"""A string representation of this Runnable."""
|
||||||
if hasattr(self, "func") and isinstance(self.func, itemgetter):
|
if self._repr is None:
|
||||||
return f"RunnableLambda({str(self.func)[len('operator.') :]})"
|
if hasattr(self, "func") and isinstance(self.func, itemgetter):
|
||||||
elif hasattr(self, "func"):
|
self._repr = f"RunnableLambda({str(self.func)[len('operator.') :]})"
|
||||||
return f"RunnableLambda({get_lambda_source(self.func) or '...'})"
|
elif hasattr(self, "func"):
|
||||||
elif hasattr(self, "afunc"):
|
self._repr = f"RunnableLambda({get_lambda_source(self.func) or '...'})"
|
||||||
return f"RunnableLambda(afunc={get_lambda_source(self.afunc) or '...'})"
|
elif hasattr(self, "afunc"):
|
||||||
else:
|
self._repr = (
|
||||||
return "RunnableLambda(...)"
|
f"RunnableLambda(afunc={get_lambda_source(self.afunc) or '...'})"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
self._repr = "RunnableLambda(...)"
|
||||||
|
return self._repr
|
||||||
|
|
||||||
def _invoke(
|
def _invoke(
|
||||||
self,
|
self,
|
||||||
|
Loading…
Reference in New Issue
Block a user