diff --git a/libs/core/langchain_core/tracers/event_stream.py b/libs/core/langchain_core/tracers/event_stream.py index 3b2f512560459..d374a3cd3f4a0 100644 --- a/libs/core/langchain_core/tracers/event_stream.py +++ b/libs/core/langchain_core/tracers/event_stream.py @@ -994,6 +994,10 @@ async def consume_astream() -> None: del event["data"]["input"] yield event + except asyncio.CancelledError as exc: + # Cancel the task if it's still running + task.cancel(exc.args[0] if exc.args else None) + raise finally: # Cancel the task if it's still running task.cancel()