Skip to content

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

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

Hardik K (hardikkaurani) wants to merge 1 commit 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

This branch was successfully deployed

1 active deployment
github-app-auth — c2344a88 Deployed Oct 24, 2026 by hardikkaurani via add_label #29436
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

2 participants