Skip to content

Python: provide actionable install-extra errors for missing connector dependencies (#14328) - #14659

Open
Hardik K (hardikkaurani) wants to merge 2 commits into
microsoft:mainfrom
hardikkaurani:fix/optional-connector-dependencies-clean
Open

Hardik K (hardikkaurani) wants to merge 2 commits into
microsoft:mainfrom
hardikkaurani:fix/optional-connector-dependencies-clean

Conversation

@hardikkaurani

Copy link
Copy Markdown
Contributor

Motivation and Context

Closes #14328

When optional connector dependencies (such as transformers, torch, onnxruntime-genai, or chromadb) are not installed in the environment, callers could encounter abrupt module-level import errors or generic ModuleNotFoundError/ImportError exceptions that did not clarify which Semantic Kernel extra needed to be installed.

Specifically:

  1. hf_prompt_execution_settings.py attempted to load transformers unconditionally at the module level without try...except, crashing upon import when optional Hugging Face extras were omitted. Additionally, get_generation_config() imported GenerationConfig before verifying the optional dependency status.
  2. hf_text_completion.py and hf_text_embedding.py eagerly imported torch and transformers at the top level, preventing callers from importing or handling Hugging Face settings or services gracefully without pre-installed packages.
  3. OnnxGenAICompletionBase and ChromaMemoryStore raised generic error messages that did not identify the specific Semantic Kernel install extra (semantic-kernel[onnx] and semantic-kernel[chroma]).

Description

  • Hugging Face Connector:
    • Guarded module-level transformers and torch imports in hf_prompt_execution_settings.py, hf_text_completion.py, and hf_text_embedding.py with try...except (ImportError, ModuleNotFoundError).
    • Added clear, actionable error messages directing users to install semantic-kernel[hugging_face] when attempting to instantiate uninstalled services or invoke get_generation_config().
  • ONNX Connector:
    • Updated OnnxGenAICompletionBase lazy-import error message to specify pip install semantic-kernel[onnx].
    • Cleaned up generator comprehension in _generate_next_token.
  • Chroma Connector:
    • Updated ChromaMemoryStore to explicitly reference pip install semantic-kernel[chroma].
  • Unit Tests:
    • Added tests/unit/connectors/test_optional_dependencies.py covering:
      • Instantiation and config generation for HuggingFacePromptExecutionSettings both with and without transformers.
      • Instantiation of HuggingFaceTextCompletion and HuggingFaceTextEmbedding without required extras.
      • Instantiation of OnnxGenAICompletionBase without onnxruntime-genai.
      • Instantiation of ChromaMemoryStore without chromadb.
    • Tests remain fully isolated from local extra installations via mock patching.

Contribution Checklist

@hardikkaurani
Hardik K (hardikkaurani) requested a review from a team as a code owner October 24, 2026 13:34
Copilot AI balanced review requested due to automatic review settings October 24, 2026 13:34

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Hi Eduard van Valkenburg (@eavanvalkenburg),

Could you please take a look at this pull request when you have a moment? It addresses #14328 by adding actionable install-extra error messages for optional connector dependencies (semantic-kernel[hugging_face], semantic-kernel[onnx], and semantic-kernel[chroma]), fixing unguarded module-level imports in the Hugging Face connector, and adding isolated regression tests for missing optional dependencies.

All Ruff checks and unit tests are passing cleanly. Thank you!

@semantic-kernel-automation semantic-kernel-automation Bot added the python Pull requests for the Python Semantic Kernel label Oct 24, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The new Chroma test fails on Python 3.12; see the inline comment. The pre-commit job also rewrites the redundant parentheses around the new Chroma error-message strings with pyupgrade. Please apply the existing hooks and address the Python 3.12 import before rerunning CI.


def test_chroma_memory_store_missing_dependency():
"""ChromaMemoryStore raises ServiceInitializationError naming semantic-kernel[chroma]."""
from semantic_kernel.connectors.memory_stores.chroma.chroma_memory_store import (

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This new test fails on Python 3.12 before reaching the missing-dependency assertion: importing ChromaMemoryStore raises ImportError: cannot import name 'deprecated' from 'warnings'. The module currently chooses warnings.deprecated for Python >=3.12, but that decorator was introduced in 3.13. Please correct that guard to >=3.13 (using typing_extensions.deprecated on 3.12) so this regression test works across the supported matrix. Confirmed in https://github.com/microsoft/semantic-kernel/actions/runs/40098737410/job/121530788066.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch. Updated the import guard in ChromaMemoryStore to sys.version_info >= (3, 13) so it falls back to yping_extensions.deprecated on Python 3.12. Also cleaned up the redundant parentheses around the error string to satisfy pyupgrade.

@hardikkaurani

Hardik K (hardikkaurani) commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi Eduard van Valkenburg (@eavanvalkenburg),

I've pushed an update addressing both points:

  • Python Version Compatibility: Adjusted the warnings.deprecated import guard in ChromaMemoryStore to use sys.version_info >= (3, 13), ensuring Python 3.12 correctly falls back to typing_extensions.deprecated.
  • Formatting: Removed the redundant parentheses around the ServiceInitializationError string literal to satisfy pyupgrade.
  • Verification: Verified formatting with ruff and ran the optional dependencies test suite locally. All tests passed.

The updates are ready for another review. Thank you for your time!

This branch was successfully deployed

1 active deployment
github-app-auth — 5618a9cd Deployed Nov 2, 2026 by hardikkaurani via add_label #29541
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Pull requests for the Python Semantic Kernel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add regression coverage for missing optional connector dependencies

3 participants