Skip to content

'This event loop is already running' error when using LiteLLM inside another event loop #1647

Closed as not planned

Description

@martinscooper

LiteLLMInferenceEngine, as it is implemented on unitxt, creates an event loop. FastAPI/Jupyter Notebooks also create one. asyncio doesn't allow nested loop, so we need to rely on nest_asyncio to handle them.

We should catch this error and add an error message with instructions and suggested fix

Refer to this discussion for more details.

Activity

  1. self-assigned this
    on Mar 5, 2025
  2. github-actions commented on Apr 10, 2025

    @github-actions

    This issue is stale because it has been open for 30 days with no activity.

  3. github-actions commented on May 16, 2026

    @github-actions

    This issue was closed because it has been inactive for 14 days since being marked as stale.

  4. wak327 commented on Oct 3, 2026

    @wak327
    Contributor

    @martinscooper I opened #1982 for this. Calling sync inference inside a running loop now raises a UnitxtError that suggests asyncio.to_thread(engine.infer, dataset) or nest_asyncio.apply(). It also fixes LiteLLMInferenceEngine failing after asyncio.run(), from worker threads, and on Python 3.14. cc @elronbandel @yoavkatz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions