Skip to content

Commit c671d54

Browse files
authored
core: make with_alisteners() example workable. (#30059)
**Description:** 5 fix of example from function with_alisteners() in libs/core/langchain_core/runnables/base.py Replace incoherent example output with workable example's output. 1. SyntaxError: unterminated string literal print(f"on start callback starts at {format_t(time.time())} correct as print(f"on start callback starts at {format_t(time.time())}") 2. SyntaxError: unterminated string literal print(f"on end callback starts at {format_t(time.time())} correct as print(f"on end callback starts at {format_t(time.time())}") 3. NameError: name 'Runnable' is not defined Fix as from langchain_core.runnables import Runnable 4. NameError: name 'asyncio' is not defined Fix as import asyncio 5. NameError: name 'format_t' is not defined. Implement format_t() as from datetime import datetime, timezone def format_t(timestamp: float) -> str: return datetime.fromtimestamp(timestamp, tz=timezone.utc).isoformat()
1 parent eca8c55 commit c671d54

File tree

1 file changed

+20
-15
lines changed
  • libs/core/langchain_core/runnables

1 file changed

+20
-15
lines changed

libs/core/langchain_core/runnables/base.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,21 +1644,26 @@ def with_alisteners(
16441644
16451645
.. code-block:: python
16461646
1647-
from langchain_core.runnables import RunnableLambda
1647+
from langchain_core.runnables import RunnableLambda, Runnable
1648+
from datetime import datetime, timezone
16481649
import time
1650+
import asyncio
1651+
1652+
def format_t(timestamp: float) -> str:
1653+
return datetime.fromtimestamp(timestamp, tz=timezone.utc).isoformat()
16491654
16501655
async def test_runnable(time_to_sleep : int):
16511656
print(f"Runnable[{time_to_sleep}s]: starts at {format_t(time.time())}")
16521657
await asyncio.sleep(time_to_sleep)
16531658
print(f"Runnable[{time_to_sleep}s]: ends at {format_t(time.time())}")
16541659
16551660
async def fn_start(run_obj : Runnable):
1656-
print(f"on start callback starts at {format_t(time.time())}
1661+
print(f"on start callback starts at {format_t(time.time())}")
16571662
await asyncio.sleep(3)
16581663
print(f"on start callback ends at {format_t(time.time())}")
16591664
16601665
async def fn_end(run_obj : Runnable):
1661-
print(f"on end callback starts at {format_t(time.time())}
1666+
print(f"on end callback starts at {format_t(time.time())}")
16621667
await asyncio.sleep(2)
16631668
print(f"on end callback ends at {format_t(time.time())}")
16641669
@@ -1671,18 +1676,18 @@ async def concurrent_runs():
16711676
16721677
asyncio.run(concurrent_runs())
16731678
Result:
1674-
on start callback starts at 2024-05-16T14:20:29.637053+00:00
1675-
on start callback starts at 2024-05-16T14:20:29.637150+00:00
1676-
on start callback ends at 2024-05-16T14:20:32.638305+00:00
1677-
on start callback ends at 2024-05-16T14:20:32.638383+00:00
1678-
Runnable[3s]: starts at 2024-05-16T14:20:32.638849+00:00
1679-
Runnable[5s]: starts at 2024-05-16T14:20:32.638999+00:00
1680-
Runnable[3s]: ends at 2024-05-16T14:20:35.640016+00:00
1681-
on end callback starts at 2024-05-16T14:20:35.640534+00:00
1682-
Runnable[5s]: ends at 2024-05-16T14:20:37.640169+00:00
1683-
on end callback starts at 2024-05-16T14:20:37.640574+00:00
1684-
on end callback ends at 2024-05-16T14:20:37.640654+00:00
1685-
on end callback ends at 2024-05-16T14:20:39.641751+00:00
1679+
on start callback starts at 2025-03-01T07:05:22.875378+00:00
1680+
on start callback starts at 2025-03-01T07:05:22.875495+00:00
1681+
on start callback ends at 2025-03-01T07:05:25.878862+00:00
1682+
on start callback ends at 2025-03-01T07:05:25.878947+00:00
1683+
Runnable[2s]: starts at 2025-03-01T07:05:25.879392+00:00
1684+
Runnable[3s]: starts at 2025-03-01T07:05:25.879804+00:00
1685+
Runnable[2s]: ends at 2025-03-01T07:05:27.881998+00:00
1686+
on end callback starts at 2025-03-01T07:05:27.882360+00:00
1687+
Runnable[3s]: ends at 2025-03-01T07:05:28.881737+00:00
1688+
on end callback starts at 2025-03-01T07:05:28.882428+00:00
1689+
on end callback ends at 2025-03-01T07:05:29.883893+00:00
1690+
on end callback ends at 2025-03-01T07:05:30.884831+00:00
16861691
16871692
"""
16881693
from langchain_core.tracers.root_listeners import AsyncRootListenersTracer

0 commit comments

Comments
 (0)