Skip to content

Commit bb71f53

Browse files
cbornetmdrxy
andauthored
chore(core): use anext and deprecate py_anext (#34211)
LangChain uses Python 3.10+ so `py_anext` isn't needed anymore. --------- Co-authored-by: Mason Daugherty <[email protected]>
1 parent 9875ffb commit bb71f53

File tree

5 files changed

+12
-11
lines changed

5 files changed

+12
-11
lines changed

libs/core/langchain_core/runnables/base.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
AsyncRootListenersTracer,
9595
RootListenersTracer,
9696
)
97-
from langchain_core.utils.aiter import aclosing, atee, py_anext
97+
from langchain_core.utils.aiter import aclosing, atee
9898
from langchain_core.utils.iter import safetee
9999
from langchain_core.utils.pydantic import create_model_v2
100100

@@ -2377,7 +2377,7 @@ async def _atransform_stream_with_config(
23772377
# tee the input so we can iterate over it twice
23782378
input_for_tracing, input_for_transform = atee(inputs, 2)
23792379
# Start the input iterator to ensure the input Runnable starts before this one
2380-
final_input: Input | None = await py_anext(input_for_tracing, None)
2380+
final_input: Input | None = await anext(input_for_tracing, None)
23812381
final_input_supported = True
23822382
final_output: Output | None = None
23832383
final_output_supported = True
@@ -2417,7 +2417,7 @@ async def _atransform_stream_with_config(
24172417
iterator = iterator_
24182418
try:
24192419
while True:
2420-
chunk = await coro_with_context(py_anext(iterator), context)
2420+
chunk = await coro_with_context(anext(iterator), context)
24212421
yield chunk
24222422
if final_output_supported:
24232423
if final_output is None:
@@ -4025,7 +4025,7 @@ async def _atransform(
40254025

40264026
# Wrap in a coroutine to satisfy linter
40274027
async def get_next_chunk(generator: AsyncIterator) -> Output | None:
4028-
return await py_anext(generator)
4028+
return await anext(generator)
40294029

40304030
# Start the first iteration of each generator
40314031
tasks = {

libs/core/langchain_core/runnables/fallbacks.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
coro_with_context,
2929
get_unique_config_specs,
3030
)
31-
from langchain_core.utils.aiter import py_anext
3231

3332
if TYPE_CHECKING:
3433
from langchain_core.callbacks.manager import AsyncCallbackManagerForChainRun
@@ -563,7 +562,7 @@ async def astream(
563562
child_config,
564563
**kwargs,
565564
)
566-
chunk = await coro_with_context(py_anext(stream), context)
565+
chunk = await coro_with_context(anext(stream), context)
567566
except self.exceptions_to_handle as e:
568567
first_error = e if first_error is None else first_error
569568
last_error = e

libs/core/langchain_core/runnables/passthrough.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
AddableDict,
3434
ConfigurableFieldSpec,
3535
)
36-
from langchain_core.utils.aiter import atee, py_anext
36+
from langchain_core.utils.aiter import atee
3737
from langchain_core.utils.iter import safetee
3838
from langchain_core.utils.pydantic import create_model_v2
3939

@@ -614,7 +614,7 @@ async def _atransform(
614614
)
615615
# start map output stream
616616
first_map_chunk_task: asyncio.Task = asyncio.create_task(
617-
py_anext(map_output, None), # type: ignore[arg-type]
617+
anext(map_output, None),
618618
)
619619
# consume passthrough stream
620620
async for chunk in for_passthrough:

libs/core/langchain_core/tracers/event_stream.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
_astream_log_implementation,
4343
)
4444
from langchain_core.tracers.memory_stream import _MemoryStream
45-
from langchain_core.utils.aiter import aclosing, py_anext
45+
from langchain_core.utils.aiter import aclosing
4646
from langchain_core.utils.uuid import uuid7
4747

4848
if TYPE_CHECKING:
@@ -189,7 +189,7 @@ async def tap_output_aiter(
189189
# atomic check and set
190190
tap = self.is_tapped.setdefault(run_id, sentinel)
191191
# wait for first chunk
192-
first = await py_anext(output, default=sentinel)
192+
first = await anext(output, sentinel)
193193
if first is sentinel:
194194
return
195195
# get run info

libs/core/langchain_core/utils/aiter.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@
2626

2727
from typing_extensions import override
2828

29+
from langchain_core._api.deprecation import deprecated
30+
2931
T = TypeVar("T")
3032

3133
_no_default = object()
3234

3335

3436
# https://github.com/python/cpython/blob/main/Lib/test/test_asyncgen.py#L54
35-
# before 3.10, the builtin anext() was not available
37+
@deprecated(since="1.1.2", removal="2.0.0")
3638
def py_anext(
3739
iterator: AsyncIterator[T], default: T | Any = _no_default
3840
) -> Awaitable[T | Any | None]:

0 commit comments

Comments
 (0)