Skip to content

feat: lazy load imports for language model component#11737

Merged
jordanrfrazier merged 33 commits intomainfrom
lazy-load-language-model-imports
Feb 26, 2026
Merged

feat: lazy load imports for language model component#11737
jordanrfrazier merged 33 commits intomainfrom
lazy-load-language-model-imports

Conversation

@jordanrfrazier
Copy link
Collaborator

@jordanrfrazier jordanrfrazier commented Feb 11, 2026

Ensures that only the necessary dependencies are required. For example, if OpenAI provider is used, it will now only import langchain_openai, rather than requiring langchain_anthropic, langchain_ibm, etc.

Summary by CodeRabbit

  • Tests

    • Updated test mocks across multiple test suites to align with refined model and embedding class retrieval mechanisms.
  • Refactor

    • Improved model and embedding class loading with on-demand initialization and caching, replacing bulk collection retrieval with targeted single-class lookups.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 11, 2026

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Walkthrough

This PR refactors model and embedding class discovery from collection-based retrieval (dictionary returns) to on-demand, single-class retrieval with dynamic importing and caching. The public API functions get_model_classes() and get_embedding_classes() are replaced with get_model_class(class_name) and get_embedding_class(class_name) respectively.

Changes

Cohort / File(s) Summary
Core Model Discovery Refactor
src/lfx/src/lfx/base/models/unified_models.py
Introduced dynamic, on-demand class importing via _MODEL_CLASS_IMPORTS and _EMBEDDING_CLASS_IMPORTS mappings. Replaced get_model_classes() and get_embedding_classes() with get_model_class(class_name) and get_embedding_class(class_name). Added module-level caching dictionaries _model_class_cache and _embedding_class_cache. Updated get_llm() to use the new single-class getter with validation for missing model class names.
Component Updates
src/lfx/src/lfx/components/llm_operations/batch_run.py, src/lfx/src/lfx/components/models_and_agents/embedding_model.py
Updated to call new getter functions: get_model_class(model_class_name) and get_embedding_class(name) respectively, replacing previous dictionary-based .get() patterns. Adjusted error handling to validate presence of class names in metadata before retrieval.
Test Mock Updates
src/backend/tests/unit/components/llm_operations/test_lambda_filter.py, src/backend/tests/unit/components/llm_operations/test_structured_output_component.py, src/backend/tests/unit/components/models_and_agents/test_embedding_model_component.py, src/backend/tests/unit/components/models_and_agents/test_language_model_component.py
Updated all test patches and mock fixtures to reference the new single-class getter functions. Changed mock parameter names and return value assignments from dictionary-based mocks to direct class mocks. Adjusted test assertions to verify calls to the new API functions.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes


Important

Pre-merge checks failed

Please resolve all errors before merging. Addressing warnings is optional.

❌ Failed checks (1 error, 2 warnings, 1 inconclusive)
Check name Status Explanation Resolution
Test Coverage For New Implementations ❌ Error No dedicated unit tests directly test get_model_class() and get_embedding_class() functions; only integration tests mock them. Add unit tests for lazy import mechanism, caching behavior, error handling, and correct class returns without mocking.
Docstring Coverage ⚠️ Warning Docstring coverage is 46.48% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Excessive Mock Usage Warning ⚠️ Warning Test files demonstrate excessive mock usage with deep @patch stacking and complex fixture-based mocking that obscures actual behavior verification. Reduce mock density by creating integration tests with real classes, consolidating mock setup into fixtures, asserting on actual return values rather than mock calls, and using simpler test doubles.
Test Quality And Coverage ❓ Inconclusive Unable to locate language_model_component.py file; code inspection cannot proceed without access to source files. Verify file location, check repository structure, or provide file path to examine lazy import implementation and test coverage.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: implementing lazy loading for language model component imports to reduce unnecessary dependencies.
Test File Naming And Structure ✅ Passed All four modified backend test files follow correct patterns with test_*.py naming, pytest structure, @pytest.fixture decorators, ComponentTestBaseWithoutClient inheritance, @patch decorators, descriptive should_when_ test names, hierarchical class organization, and comprehensive coverage including edge cases.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch lazy-load-language-model-imports

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions github-actions bot added enhancement New feature or request and removed enhancement New feature or request labels Feb 11, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Feb 11, 2026

Frontend Unit Test Coverage Report

Coverage Summary

Lines Statements Branches Functions
Coverage: 19%
18.8% (6095/32413) 12.25% (3097/25277) 12.64% (879/6954)

Unit Test Results

Tests Skipped Failures Errors Time
2310 0 💤 0 ❌ 0 🔥 32.801s ⏱️

@codecov
Copy link

codecov bot commented Feb 11, 2026

Codecov Report

❌ Patch coverage is 20.45455% with 35 lines in your changes missing coverage. Please review.
✅ Project coverage is 35.46%. Comparing base (e346812) to head (474c381).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
src/lfx/src/lfx/base/models/unified_models.py 20.45% 35 Missing ⚠️

❌ Your patch status has failed because the patch coverage (20.45%) is below the target coverage (40.00%). You can increase the patch coverage or adjust the target coverage.
❌ Your project status has failed because the head coverage (41.84%) is below the target coverage (60.00%). You can increase the head coverage or adjust the target coverage.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #11737      +/-   ##
==========================================
+ Coverage   35.45%   35.46%   +0.01%     
==========================================
  Files        1527     1527              
  Lines       73570    73597      +27     
  Branches    11061    11065       +4     
==========================================
+ Hits        26081    26098      +17     
- Misses      46074    46084      +10     
  Partials     1415     1415              
Flag Coverage Δ
backend 56.36% <ø> (+0.06%) ⬆️
frontend 16.98% <ø> (ø)
lfx 41.84% <20.45%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/lfx/src/lfx/base/models/unified_models.py 29.67% <20.45%> (-0.61%) ⬇️

... and 9 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.


module_path, attr_name = import_info
module = importlib.import_module(module_path)
cls = getattr(module, attr_name)
Copy link
Collaborator

Choose a reason for hiding this comment

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

if cls is None, etc, might want to surface it / raise an exception

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Since we control the mapping here, the error is developer-side and it will be surfaced as that - I think it's okay to let the natural exception propagate in this case

@github-actions github-actions bot added enhancement New feature or request and removed enhancement New feature or request labels Feb 11, 2026
jordanrfrazier and others added 5 commits February 11, 2026 20:22
Ensures that only the necessary dependencies are required.
For example, if OpenAI provider is used, it will now only
import langchain_openai, rather than requiring langchain_anthropic,
langchain_ibm, etc.
@HzaRashid HzaRashid force-pushed the lazy-load-language-model-imports branch from d6b9c5d to 82079b0 Compare February 11, 2026 20:24
@github-actions github-actions bot added enhancement New feature or request and removed enhancement New feature or request labels Feb 11, 2026
@github-actions github-actions bot added enhancement New feature or request and removed enhancement New feature or request labels Feb 11, 2026
@github-actions github-actions bot added the lgtm This PR has been approved by a maintainer label Feb 12, 2026
@github-actions github-actions bot added the enhancement New feature or request label Feb 23, 2026
jordanrfrazier and others added 2 commits February 23, 2026 18:45
* change-azure-openai-default-temperature-to-1.0

* [autofix.ci] apply automated fixes

* [autofix.ci] apply automated fixes (attempt 2/3)

* [autofix.ci] apply automated fixes (attempt 3/3)

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
@github-actions github-actions bot added enhancement New feature or request and removed enhancement New feature or request labels Feb 23, 2026
@github-actions github-actions bot added enhancement New feature or request and removed enhancement New feature or request labels Feb 23, 2026
@github-actions github-actions bot added enhancement New feature or request and removed enhancement New feature or request labels Feb 23, 2026
@github-actions github-actions bot added enhancement New feature or request and removed enhancement New feature or request labels Feb 23, 2026
@github-actions github-actions bot added enhancement New feature or request and removed enhancement New feature or request labels Feb 24, 2026
@github-actions github-actions bot removed the enhancement New feature or request label Feb 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request lgtm This PR has been approved by a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants