The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
Fix async task being destroyed before cancelled (#2787)
Nuno Campos1bea9ea4befd
diff --git a/langchain/callbacks/streaming_aiter.py b/langchain/callbacks/streaming_aiter.pyindex 45a4459b77..47372dd9ed 100644--- a/langchain/callbacks/streaming_aiter.py+++ b/langchain/callbacks/streaming_aiter.py@@ -47,14 +47,19 @@ class AsyncIteratorCallbackHandler(AsyncCallbackHandler):while not self.queue.empty() or not self.done.is_set():# Wait for the next token in the queue,# but stop waiting if the done event is set- done, _ = await asyncio.wait(+ done, other = await asyncio.wait([+ # NOTE: If you add other tasks here, update the code below,+ # which assumes each set has exactly one task eachasyncio.ensure_future(self.queue.get()),asyncio.ensure_future(self.done.wait()),],return_when=asyncio.FIRST_COMPLETED,)+ # Cancel the other task+ other.pop().cancel()+# Extract the value of the first completed tasktoken_or_done = cast(Union[str, Literal[True]], done.pop().result())