Skip to content

Conversation

@AlexsanderHamir
Copy link
Collaborator

Title

[Refactor#2] litellm/init – Lazy-load utils to reduce memory + import time

Relevant issues

LiteLLM import time & memory consumption on import.

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have Added testing in the tests/litellm/ directory, Adding at least 1 test is a hard requirement - see details
  • I have added a screenshot of my new test passing locally
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible, it only solves 1 specific problem

Type

🐛 Bug Fix

Changes

  • Removed utils root import from litellm init file and left only the client import since its used as a decorator and can't be lazy loaded.
  • Removed imports to litellm coming from internal libraries.

Lazy-load most functions and response types from utils.py to avoid loading
tiktoken and other heavy dependencies at import time. This significantly
reduces memory usage when importing completion from litellm.

Changes:
- Made utils functions (exception_type, get_litellm_params, ModelResponse, etc.)
  lazy-loaded via __getattr__
- Made ALL_LITELLM_RESPONSE_TYPES lazy-loaded
- Fixed circular imports by updating files to import directly from litellm.utils
  or litellm.types.utils instead of from litellm
- Kept client decorator as immediate import since it's used at function
  definition time

Only client is now imported immediately from utils.py; all other utils
functions and response types are loaded on-demand when accessed.
- Move _lazy_import_cost_calculator and _lazy_import_litellm_logging to _lazy_imports.py
- Update functions to use _get_litellm_globals() for consistency
- Remove top-level imports and registry, import lazily in __getattr__ instead
- Ensures _lazy_imports.py is only loaded when needed, maintaining true lazy loading
…tions

- Move get_model_info import from module level to method level in bedrock image transformations
- Add shared get_cached_model_info() function in common_utils.py to avoid code duplication
- Cache the import after first use to maintain performance
- Fixes circular import error during litellm module initialization
- Add Type to typing imports to fix F821 undefined name error
- Fix function call from get_rag_ingestion_class to get_ingestion_class
@vercel
Copy link

vercel bot commented Nov 27, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
litellm Ready Ready Preview Comment Dec 3, 2025 7:16pm

…nation

- Remove unused 'litellm' import from amazon_nova_transformation.py
- Remove unused imports (Dict, AsyncHTTPHandler, get_async_httpx_client, httpxSpecialProvider) from ingestion.py
- Add noqa comment with explanation for PLR0915 in _lazy_imports.py to preserve true lazy import behavior
@AlexsanderHamir AlexsanderHamir merged commit 56328e6 into main Dec 3, 2025
38 of 59 checks passed
busla added a commit to aproorg/litellm that referenced this pull request Dec 4, 2025
The initial fix (f6bc8d2) was incomplete. It restored direct imports
in litellm/__init__.py but missed two critical functions that
proxy_server.py imports at module level:

1. load_credentials_from_list (proxy_server.py:56)
2. _add_custom_logger_callback_to_specific_event (proxy_server.py:464)

When these functions weren't pre-imported, Python had to dynamically
import them from utils.py, which triggered "import litellm" at
utils.py:56, recreating the circular dependency chain that prevented
Uvicorn from starting.

This complete fix adds both functions to the import list, breaking
the circular dependency cycle.

Related to PR BerriAI#17171 which introduced lazy loading on Dec 3, 2025.

Fixes: ECS health check failures where migrations complete but Uvicorn
never starts, resulting in UNHEALTHY containers.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants