From 3b48f484586a8960cf4577193cb022a273e07361 Mon Sep 17 00:00:00 2001 From: Mason Daugherty Date: Mon, 22 Jun 2026 22:26:16 -0400 Subject: [PATCH] fix(core): use `asyncio.get_running_loop()` in async contexts (#38157) Closes #35726 --- Several async functions called `asyncio.get_event_loop()`, which has been deprecated since Python 3.10. Inside `async def` bodies the loop is always running, so `asyncio.get_running_loop()` is the correct, warning-free replacement with no behavior change. This updates the call in `_ahandle_event_for_handler` (core callbacks), `_render_mermaid_using_pyppeteer` (core runnables graph), and `async_api_key_wrapper` (`langchain-openai` client utils). Users running on modern Python no longer see `DeprecationWarning`s from these paths. Made by [Open SWE](https://openswe.vercel.app) Co-authored-by: open-swe[bot] --- libs/core/langchain_core/callbacks/manager.py | 2 +- libs/core/langchain_core/runnables/graph_mermaid.py | 2 +- .../openai/langchain_openai/chat_models/_client_utils.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libs/core/langchain_core/callbacks/manager.py b/libs/core/langchain_core/callbacks/manager.py index 069d7a4e0c5..36da69b3b79 100644 --- a/libs/core/langchain_core/callbacks/manager.py +++ b/libs/core/langchain_core/callbacks/manager.py @@ -416,7 +416,7 @@ async def _ahandle_event_for_handler( elif handler.run_inline: event(*args, **kwargs) else: - await asyncio.get_event_loop().run_in_executor( + await asyncio.get_running_loop().run_in_executor( None, functools.partial(copy_context().run, event, *args, **kwargs), ) diff --git a/libs/core/langchain_core/runnables/graph_mermaid.py b/libs/core/langchain_core/runnables/graph_mermaid.py index 941211dad92..9af42921256 100644 --- a/libs/core/langchain_core/runnables/graph_mermaid.py +++ b/libs/core/langchain_core/runnables/graph_mermaid.py @@ -395,7 +395,7 @@ async def _render_mermaid_using_pyppeteer( await browser.close() if output_file_path is not None: - await asyncio.get_event_loop().run_in_executor( + await asyncio.get_running_loop().run_in_executor( None, Path(output_file_path).write_bytes, img_bytes ) diff --git a/libs/partners/openai/langchain_openai/chat_models/_client_utils.py b/libs/partners/openai/langchain_openai/chat_models/_client_utils.py index d853d19d454..e530635db8d 100644 --- a/libs/partners/openai/langchain_openai/chat_models/_client_utils.py +++ b/libs/partners/openai/langchain_openai/chat_models/_client_utils.py @@ -550,7 +550,7 @@ def _resolve_sync_and_async_api_keys( sync_api_key_value = cast(Callable, api_key) async def async_api_key_wrapper() -> str: - return await asyncio.get_event_loop().run_in_executor( + return await asyncio.get_running_loop().run_in_executor( None, cast(Callable, api_key) )