langchain - ✅(Solved) Fix core, openai: asyncio.get_event_loop() deprecated in async contexts [1 pull requests, 2 comments, 3 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
langchain-ai/langchain#35726Fetched 2026-04-08 00:24:54
View on GitHub
Comments
2
Participants
3
Timeline
7
Reactions
0
Timeline (top)
cross-referenced ×3commented ×2labeled ×1referenced ×1

Fix Action

Fixed

PR fix notes

PR #35729: fix(core,openai): replace deprecated asyncio.get_event_loop() in async contexts

Description (problem / solution / changelog)

Fixes #35726

Replace asyncio.get_event_loop() with asyncio.get_running_loop() in async function contexts. get_event_loop() is deprecated since Python 3.10 and can fail when no event loop exists. Inside async def, get_running_loop() is the correct and guaranteed-safe replacement.

Changed files

  • libs/core/langchain_core/callbacks/manager.py_ahandle_event_for_handler()
  • libs/core/langchain_core/runnables/graph_mermaid.py_render_mermaid_using_pyppeteer()
  • libs/partners/openai/langchain_openai/chat_models/_client_utils.pyasync_api_key_wrapper()

No behavior change when called from a running event loop.

Changed files

  • libs/core/langchain_core/callbacks/manager.py (modified, +1/-1)
  • libs/core/langchain_core/runnables/graph_mermaid.py (modified, +1/-1)
  • libs/partners/openai/langchain_openai/chat_models/_client_utils.py (modified, +1/-1)
RAW_BUFFERClick to expand / collapse

Description

Several async functions use asyncio.get_event_loop() which has been deprecated since Python 3.10. Inside async def, asyncio.get_running_loop() is the correct replacement — it's guaranteed to work and won't trigger deprecation warnings.

Affected files

  • libs/core/langchain_core/callbacks/manager.py_ahandle_event_for_handler()
  • libs/core/langchain_core/runnables/graph_mermaid.py_render_mermaid_using_pyppeteer()
  • libs/partners/openai/langchain_openai/chat_models/_client_utils.pyasync_api_key_wrapper()

Suggested fix Replace asyncio.get_event_loop() with asyncio.get_running_loop() in these async contexts. No behavior change when called from a running event loop.

extent analysis

Problem Summary

Deprecation of asyncio.get_event_loop() in Python 3.10

Root Cause Analysis

The use of asyncio.get_event_loop() in async functions is deprecated since Python 3.10.

Fix Plan

Replace asyncio.get_event_loop() with asyncio.get_running_loop() in the affected files.

Step-by-Step Fix

File: libs/core/langchain_core/callbacks/manager.py

# Before
async def _handle_event_for_handler(self, event):
    loop = asyncio.get_event_loop()
    # ...

# After
async def _handle_event_for_handler(self, event):
    loop = asyncio.get_running_loop()
    # ...

File: libs/core/langchain_core/runnables/graph_mermaid.py

# Before
async def _render_mermaid_using_pyppeteer(self):
    loop = asyncio.get_event_loop()
    # ...

# After
async def _render_mermaid_using_pyppeteer(self):
    loop = asyncio.get_running_loop()
    # ...

File: libs/partners/openai/langchain_openai/chat_models/_client_utils.py

# Before
async def async_api_key_wrapper(self, api_key):
    loop = asyncio.get_event_loop()
    # ...

# After
async def async_api_key_wrapper(self, api_key):
    loop = asyncio.get_running_loop()
    # ...

Verification

Verify that the code compiles without deprecation warnings and runs as expected.

Extra Tips

  • Make sure to update all occurrences of asyncio.get_event_loop() in the affected files.
  • Consider running a linter or code analysis tool to catch similar deprecation warnings.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING