Skip to content

Commit

Permalink
core: in astream_events v2 always await task even if already finished (
Browse files Browse the repository at this point in the history
…#22916)

- this ensures exceptions propagate to the caller
  • Loading branch information
nfcampos authored Jun 14, 2024
1 parent 513e491 commit 338180f
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions libs/core/langchain_core/tracers/event_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -940,9 +940,10 @@ async def consume_astream() -> None:

yield event
finally:
# Wait for the runnable to finish, if not cancelled (eg. by break)
if task.cancel():
try:
await task
except asyncio.CancelledError:
pass
# Cancel the task if it's still running
task.cancel()
# Await it anyway, to run any cleanup code, and propagate any exceptions
try:
await task
except asyncio.CancelledError:
pass

0 comments on commit 338180f

Please sign in to comment.