Skip to content

Commit 035a9c9

Browse files
authored
core[minor]: Add parent_ids to astream_events API (#22563)
Include a list of parent ids for each event in astream events.
1 parent 67e58fd commit 035a9c9

File tree

5 files changed

+530
-44
lines changed

5 files changed

+530
-44
lines changed

libs/core/langchain_core/runnables/base.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,11 @@ async def astream_events(
959959
the runnable that emitted the event.
960960
A child runnable that gets invoked as part of the execution of a
961961
parent runnable is assigned its own unique ID.
962+
- ``parent_ids``: **List[str]** - The IDs of the parent runnables that
963+
generated the event. The root runnable will have an empty list.
964+
The order of the parent IDs is from the root to the immediate parent.
965+
Only available for v2 version of the API. The v1 version of the API
966+
will return an empty list.
962967
- ``tags``: **Optional[List[str]]** - The tags of the runnable that generated
963968
the event.
964969
- ``metadata``: **Optional[Dict[str, Any]]** - The metadata of the runnable
@@ -1051,7 +1056,8 @@ async def reverse(s: str) -> str:
10511056
event async for event in chain.astream_events("hello", version="v2")
10521057
]
10531058
1054-
# will produce the following events (run_id has been omitted for brevity):
1059+
# will produce the following events (run_id, and parent_ids
1060+
# has been omitted for brevity):
10551061
[
10561062
{
10571063
"data": {"input": "hello"},

libs/core/langchain_core/runnables/schema.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Module contains typedefs that are used with runnables."""
22
from __future__ import annotations
33

4-
from typing import Any, Dict, List
4+
from typing import Any, Dict, List, Sequence
55

66
from typing_extensions import NotRequired, TypedDict
77

@@ -54,7 +54,8 @@ async def reverse(s: str) -> str:
5454
5555
events = [event async for event in chain.astream_events("hello")]
5656
57-
# will produce the following events (run_id has been omitted for brevity):
57+
# will produce the following events
58+
# (where some fields have been omitted for brevity):
5859
[
5960
{
6061
"data": {"input": "hello"},
@@ -131,3 +132,16 @@ async def reverse(s: str) -> str:
131132
132133
The contents of the event data depend on the event type.
133134
"""
135+
136+
parent_ids: Sequence[str]
137+
"""A list of the parent IDs associated with this event.
138+
139+
Root Events will have an empty list.
140+
141+
For example, if a Runnable A calls Runnable B, then the event generated by Runnable
142+
B will have Runnable A's ID in the parent_ids field.
143+
144+
The order of the parent IDs is from the root parent to the immediate parent.
145+
146+
Only supported as of v2 of the astream events API. v1 will return an empty list.
147+
"""

0 commit comments

Comments
 (0)