Skip to content

[Refactor] litellm/init.py: lazy load encoding from main.py#18070

Merged
AlexsanderHamir merged 2 commits intomainfrom
litellm_memory_issue_on_import_13
Dec 16, 2025
Merged

[Refactor] litellm/init.py: lazy load encoding from main.py#18070
AlexsanderHamir merged 2 commits intomainfrom
litellm_memory_issue_on_import_13

Conversation

@AlexsanderHamir
Copy link
Contributor

@AlexsanderHamir AlexsanderHamir commented Dec 16, 2025

Relevant issues

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
  • 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

CI (LiteLLM team)

CI status guideline:

  • 50-55 passing tests: main is stable with minor issues.
  • 45-49 passing tests: acceptable but needs attention
  • <= 40 passing tests: unstable; be careful with your merges and assess the risk.
  • Branch creation CI run
    Link: Baseline

  • CI run for the last commit
    Link: Last Commit

  • Merge / cherry-pick CI run
    Links:

Type

🧹 Refactoring

Changes

  • Add getattr in main.py to lazy load encoding when accessed
  • Remove direct assignment of encoding = tiktoken.get_encoding() at module level
  • Heavy tiktoken.get_encoding() call is now deferred until encoding is first accessed
  • Follows existing lazy loading patterns
  • All existing code using litellm.encoding continues to work

Test Results

Screenshot 2025-12-16 at 11 04 30 AM Screenshot 2025-12-16 at 11 16 40 AM
  • Failures seem unrelated.

- Add __getattr__ in main.py to lazy load encoding when accessed
- Remove direct assignment of encoding = tiktoken.get_encoding() at module level
- Heavy tiktoken.get_encoding() call is now deferred until encoding is first accessed
- Follows existing lazy loading patterns
- All existing code using litellm.encoding continues to work
@vercel
Copy link

vercel bot commented Dec 16, 2025

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

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

@AlexsanderHamir AlexsanderHamir merged commit f177e4f into main Dec 16, 2025
43 of 59 checks passed
AlexsanderHamir added a commit that referenced this pull request Jan 6, 2026
… (issue #18659)

- Add opt-in environment variable LITELLM_DISABLE_LAZY_LOADING to disable lazy loading
- When set, encoding is loaded at import time (pre-#18070 behavior)
- This ensures tiktoken is initialized before VCR starts recording HTTP requests
- Fixes issue where accessing litellm.encoding triggers HTTP request that breaks VCR
- For now, only affects encoding as it was the only reported issue
- Includes comprehensive tests
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.

1 participant