@@ -829,8 +829,9 @@ def invoke(
829829
830830 The config supports standard keys like `'tags'`, `'metadata'` for
831831 tracing purposes, `'max_concurrency'` for controlling how much work to
832- do in parallel, and other keys. Please refer to the `RunnableConfig`
833- for more details.
832+ do in parallel, and other keys.
833+
834+ Please refer to `RunnableConfig` for more details.
834835
835836 Returns:
836837 The output of the `Runnable`.
@@ -850,8 +851,9 @@ async def ainvoke(
850851
851852 The config supports standard keys like `'tags'`, `'metadata'` for
852853 tracing purposes, `'max_concurrency'` for controlling how much work to
853- do in parallel, and other keys. Please refer to the `RunnableConfig`
854- for more details.
854+ do in parallel, and other keys.
855+
856+ Please refer to `RunnableConfig` for more details.
855857
856858 Returns:
857859 The output of the `Runnable`.
@@ -878,8 +880,9 @@ def batch(
878880 config: A config to use when invoking the `Runnable`. The config supports
879881 standard keys like `'tags'`, `'metadata'` for
880882 tracing purposes, `'max_concurrency'` for controlling how much work
881- to do in parallel, and other keys. Please refer to the
882- `RunnableConfig` for more details.
883+ to do in parallel, and other keys.
884+
885+ Please refer to `RunnableConfig` for more details.
883886 return_exceptions: Whether to return exceptions instead of raising them.
884887 **kwargs: Additional keyword arguments to pass to the `Runnable`.
885888
@@ -945,8 +948,9 @@ def batch_as_completed(
945948
946949 The config supports standard keys like `'tags'`, `'metadata'` for
947950 tracing purposes, `'max_concurrency'` for controlling how much work to
948- do in parallel, and other keys. Please refer to the `RunnableConfig`
949- for more details.
951+ do in parallel, and other keys.
952+
953+ Please refer to `RunnableConfig` for more details.
950954 return_exceptions: Whether to return exceptions instead of raising them.
951955 **kwargs: Additional keyword arguments to pass to the `Runnable`.
952956
@@ -1012,8 +1016,9 @@ async def abatch(
10121016
10131017 The config supports standard keys like `'tags'`, `'metadata'` for
10141018 tracing purposes, `'max_concurrency'` for controlling how much work to
1015- do in parallel, and other keys. Please refer to the `RunnableConfig`
1016- for more details.
1019+ do in parallel, and other keys.
1020+
1021+ Please refer to `RunnableConfig` for more details.
10171022 return_exceptions: Whether to return exceptions instead of raising them.
10181023 **kwargs: Additional keyword arguments to pass to the `Runnable`.
10191024
@@ -1076,8 +1081,9 @@ async def abatch_as_completed(
10761081
10771082 The config supports standard keys like `'tags'`, `'metadata'` for
10781083 tracing purposes, `'max_concurrency'` for controlling how much work to
1079- do in parallel, and other keys. Please refer to the `RunnableConfig`
1080- for more details.
1084+ do in parallel, and other keys.
1085+
1086+ Please refer to `RunnableConfig` for more details.
10811087 return_exceptions: Whether to return exceptions instead of raising them.
10821088 **kwargs: Additional keyword arguments to pass to the `Runnable`.
10831089
@@ -1755,46 +1761,52 @@ def with_alisteners(
17551761 import time
17561762 import asyncio
17571763
1764+
17581765 def format_t(timestamp: float) -> str:
17591766 return datetime.fromtimestamp(timestamp, tz=timezone.utc).isoformat()
17601767
1768+
17611769 async def test_runnable(time_to_sleep: int):
17621770 print(f"Runnable[{time_to_sleep}s]: starts at {format_t(time.time())}")
17631771 await asyncio.sleep(time_to_sleep)
17641772 print(f"Runnable[{time_to_sleep}s]: ends at {format_t(time.time())}")
17651773
1774+
17661775 async def fn_start(run_obj: Runnable):
17671776 print(f"on start callback starts at {format_t(time.time())}")
17681777 await asyncio.sleep(3)
17691778 print(f"on start callback ends at {format_t(time.time())}")
17701779
1780+
17711781 async def fn_end(run_obj: Runnable):
17721782 print(f"on end callback starts at {format_t(time.time())}")
17731783 await asyncio.sleep(2)
17741784 print(f"on end callback ends at {format_t(time.time())}")
17751785
1786+
17761787 runnable = RunnableLambda(test_runnable).with_alisteners(
1777- on_start=fn_start,
1778- on_end=fn_end
1788+ on_start=fn_start, on_end=fn_end
17791789 )
1790+
1791+
17801792 async def concurrent_runs():
17811793 await asyncio.gather(runnable.ainvoke(2), runnable.ainvoke(3))
17821794
1783- asyncio.run(concurrent_runs())
1784- Result:
1785- on start callback starts at 2025-03-01T07:05:22.875378+00:00
1786- on start callback starts at 2025-03-01T07:05:22.875495+00:00
1787- on start callback ends at 2025-03-01T07:05:25.878862+00:00
1788- on start callback ends at 2025-03-01T07:05:25.878947+00:00
1789- Runnable[2s]: starts at 2025-03-01T07:05:25.879392+00:00
1790- Runnable[3s]: starts at 2025-03-01T07:05:25.879804+00:00
1791- Runnable[2s]: ends at 2025-03-01T07:05:27.881998+00:00
1792- on end callback starts at 2025-03-01T07:05:27.882360+00:00
1793- Runnable[3s]: ends at 2025-03-01T07:05:28.881737+00:00
1794- on end callback starts at 2025-03-01T07:05:28.882428+00:00
1795- on end callback ends at 2025-03-01T07:05:29.883893+00:00
1796- on end callback ends at 2025-03-01T07:05:30.884831+00:00
17971795
1796+ asyncio.run(concurrent_runs())
1797+ # Result:
1798+ # on start callback starts at 2025-03-01T07:05:22.875378+00:00
1799+ # on start callback starts at 2025-03-01T07:05:22.875495+00:00
1800+ # on start callback ends at 2025-03-01T07:05:25.878862+00:00
1801+ # on start callback ends at 2025-03-01T07:05:25.878947+00:00
1802+ # Runnable[2s]: starts at 2025-03-01T07:05:25.879392+00:00
1803+ # Runnable[3s]: starts at 2025-03-01T07:05:25.879804+00:00
1804+ # Runnable[2s]: ends at 2025-03-01T07:05:27.881998+00:00
1805+ # on end callback starts at 2025-03-01T07:05:27.882360+00:00
1806+ # Runnable[3s]: ends at 2025-03-01T07:05:28.881737+00:00
1807+ # on end callback starts at 2025-03-01T07:05:28.882428+00:00
1808+ # on end callback ends at 2025-03-01T07:05:29.883893+00:00
1809+ # on end callback ends at 2025-03-01T07:05:30.884831+00:00
17981810 ```
17991811 """
18001812 return RunnableBinding (
@@ -1856,7 +1868,7 @@ def with_retry(
18561868 `exp_base`, and `jitter` (all `float` values).
18571869
18581870 Returns:
1859- A new Runnable that retries the original Runnable on exceptions.
1871+ A new ` Runnable` that retries the original ` Runnable` on exceptions.
18601872
18611873 Example:
18621874 ```python
@@ -1940,7 +1952,9 @@ def with_fallbacks(
19401952 exceptions_to_handle: A tuple of exception types to handle.
19411953 exception_key: If `string` is specified then handled exceptions will be
19421954 passed to fallbacks as part of the input under the specified key.
1955+
19431956 If `None`, exceptions will not be passed to fallbacks.
1957+
19441958 If used, the base `Runnable` and its fallbacks must accept a
19451959 dictionary as input.
19461960
@@ -1976,7 +1990,9 @@ def _generate(input: Iterator) -> Iterator[str]:
19761990 exceptions_to_handle: A tuple of exception types to handle.
19771991 exception_key: If `string` is specified then handled exceptions will be
19781992 passed to fallbacks as part of the input under the specified key.
1993+
19791994 If `None`, exceptions will not be passed to fallbacks.
1995+
19801996 If used, the base `Runnable` and its fallbacks must accept a
19811997 dictionary as input.
19821998
@@ -2442,10 +2458,14 @@ def as_tool(
24422458
24432459 `as_tool` will instantiate a `BaseTool` with a name, description, and
24442460 `args_schema` from a `Runnable`. Where possible, schemas are inferred
2445- from `runnable.get_input_schema`. Alternatively (e.g., if the
2446- `Runnable` takes a dict as input and the specific dict keys are not typed),
2447- the schema can be specified directly with `args_schema`. You can also
2448- pass `arg_types` to just specify the required arguments and their types.
2461+ from `runnable.get_input_schema`.
2462+
2463+ Alternatively (e.g., if the `Runnable` takes a dict as input and the specific
2464+ `dict` keys are not typed), the schema can be specified directly with
2465+ `args_schema`.
2466+
2467+ You can also pass `arg_types` to just specify the required arguments and their
2468+ types.
24492469
24502470 Args:
24512471 args_schema: The schema for the tool.
@@ -2514,7 +2534,7 @@ def f(x: dict[str, Any]) -> str:
25142534 as_tool.invoke({"a": 3, "b": [1, 2]})
25152535 ```
25162536
2517- String input:
2537+ `str` input:
25182538
25192539 ```python
25202540 from langchain_core.runnables import RunnableLambda
0 commit comments