Skip to content

revert: perf: lazy-load SDK symbols so import litellm stays under 60 MB RSS (#39121) - #39969

Merged
yuneng-berri merged 2 commits into
litellm_internal_stagingfrom
revert-39121-litellm_lazy_sdk_import
Sep 5, 2026
Merged

revert: perf: lazy-load SDK symbols so import litellm stays under 60 MB RSS (#39121)#39969
yuneng-berri merged 2 commits into
litellm_internal_stagingfrom
revert-39121-litellm_lazy_sdk_import

Conversation

@yuneng-berri

@yuneng-berri yuneng-berri commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • perf: lazy-load SDK symbols so import litellm stays under 60 MB RSS #39121 made import litellm lazy and turned five CircleCI jobs red on staging
  • litellm.cost_calculator, the Bedrock token counter and proxy auth checks now hit circular imports
  • The Interactions API bridge fails on every call because litellm.responses and litellm.aresponses are no longer in the module namespace
  • Staging has been red on these since 2026-09-05 19:58 UTC

How it solves it:

User Flow

Before: a developer importing a litellm submodule on its own gets a circular ImportError, and the Interactions API bridge crashes on its first request

  1. They run python -c "from litellm.cost_calculator import batch_cost_calculator"
  2. It exits with ImportError: cannot import name 'RealtimeAPITokenUsageProcessor' from partially initialized module 'litellm.cost_calculator' (most likely due to a circular import)
  3. They call litellm.interactions.create(model="gpt-5.5", input="What is 2 + 2?")
  4. It raises APIConnectionError: OpenAIException - 'module' object is not callable instead of returning an interaction

After: the same import and the same call work as they did before #39121

  1. They run python -c "from litellm.cost_calculator import batch_cost_calculator"
  2. It exits 0
  3. They call litellm.interactions.create(model="gpt-5.5", input="What is 2 + 2?")
  4. They get an InteractionsAPIResponse with status="completed"

Relevant issues

Reverts #39121

Linear ticket

Pre-Submission checklist

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

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/test_litellm/<your_test_file>.py -v. Leave the suites (make test-unit-*, make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review (Greptile reviews automatically once the PR is opened; only comment @greptileai to re-request a review after pushing changes)

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

Screenshots / Proof of Fix

Both sides ran from a fresh worktree with OPENAI_API_KEY set. The Interactions API call is a real request to OpenAI. The pytest runs are the two CircleCI test files that fail at collection time on staging (the batches_testing and litellm_utils_testing jobs)

Before (a0c9095)

Importing a submodule first

  1. python -c "from litellm.cost_calculator import batch_cost_calculator"
  2. Output: ImportError: cannot import name 'RealtimeAPITokenUsageProcessor' from partially initialized module 'litellm.cost_calculator' (most likely due to a circular import)

Interactions API bridged to /v1/responses

  1. python -c 'import litellm; r = litellm.interactions.create(model="gpt-5.5", input="What is 2 + 2? Answer with one number."); print(type(r).__name__, "status", r.status)'
  2. Output: litellm.exceptions.APIConnectionError: litellm.APIConnectionError: APIConnectionError: OpenAIException - 'module' object is not callable

The two CircleCI test files

  1. pytest tests/litellm_utils_tests/test_bedrock_token_counter.py tests/batches_tests/test_batch_custom_pricing.py -q
  2. Output: 4 warnings, 2 errors in 0.69s

After (eee162b)

Importing a submodule first

  1. python -c "from litellm.cost_calculator import batch_cost_calculator"
  2. Output: exit 0

Interactions API bridged to /v1/responses

  1. python -c 'import litellm; r = litellm.interactions.create(model="gpt-5.5", input="What is 2 + 2? Answer with one number."); print(type(r).__name__, "status", r.status)'
  2. Output: InteractionsAPIResponse status completed

The two CircleCI test files

  1. pytest tests/litellm_utils_tests/test_bedrock_token_counter.py tests/batches_tests/test_batch_custom_pricing.py -q
  2. Output: 12 passed, 1 skipped in 0.10s

Type

🐛 Bug Fix

Caveats (if any)

Medium

Low

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

@yuneng-berri
yuneng-berri requested a review from a team September 5, 2026 23:07
@linear-code

linear-code Bot commented Sep 5, 2026

Copy link
Copy Markdown

Issue reopened: LIT-6607 import litellm costs ~200 MB RSS

@greptile-apps

greptile-apps Bot commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR reverts SDK symbol lazy loading and restores eager package initialization to repair import cycles and interactions response bindings

  • Restores eager SDK, configuration, integration, and endpoint imports in litellm/__init__.py
  • Removes the SDK symbol registry, module alias resolver, and package module subclass used by the reverted implementation
  • Restores the earlier proxy package initialization and removes lazy-loading tests that no longer match the implementation

Confidence Score: 4/5

The functional revert appears sound, but repository requirements for focused regression coverage and source comments must be satisfied before merging

The eager imports restore the intended namespace and no behavioral failure was established, but the exact interactions failure lacks deterministic regression coverage and restored comments violate repository guidance

Files Needing Attention: litellm/init.py, tests/test_litellm/test_lazy_imports.py

Important Files Changed

Filename Overview
litellm/init.py Restores eager SDK imports and module-level response symbols, with repository-prohibited explanatory comments
litellm/_lazy_imports.py Removes SDK symbol, module alias, submodule fallback, and shadowable-function lazy-loading machinery
litellm/_lazy_imports_registry.py Removes the large SDK import maps and public star-import registry introduced by the reverted change
litellm/proxy/init.py Replaces dynamic proxy submodule resolution with the previous package initialization
tests/test_litellm/test_lazy_imports.py Removes tests for reverted lazy behavior without adding deterministic coverage for the interactions bridge regression

Reviews (1): Last reviewed commit: "Revert "perf: lazy-load SDK symbols so i..." | Re-trigger Greptile

@@ -353,83 +346,3 @@ def test_utils_module_lazy_imports():
assert name in utils_globals

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Interactions Regression Untested

This bug fix lacks a deterministic interactions bridge regression test, violating the repository requirement that exact failures remain covered. Add one before merging

Context Used: CLAUDE.md (source)

Comment thread litellm/__init__.py
Comment on lines +1277 to +1278

_key_management_settings: KeyManagementSettings = KeyManagementSettings()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Prohibited Source Comments

These routine import-order comments, also at lines 1282-1286 and 2189-2198, violate the repository's source-comment restrictions and must be removed before merging

Context Used: CLAUDE.md (source)

@codspeed-hq

codspeed-hq Bot commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing revert-39121-litellm_lazy_sdk_import (eee162b) with litellm_internal_staging (587311d)

Open in CodSpeed

…1 budget

The revert restored the dict annotation that #39121 had loosened to Mapping,
and the LIT001 ceiling has been ratcheted down since, so the gate rejected the
one reintroduced hit. Annotation only, no behavior change.
@yuneng-berri
yuneng-berri merged commit b1e2f5b into litellm_internal_staging Sep 5, 2026
182 of 183 checks passed
@yuneng-berri
yuneng-berri deleted the revert-39121-litellm_lazy_sdk_import branch September 5, 2026 23:22
@codecov

codecov Bot commented Sep 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants