Repository navigation
Python: provide actionable install-extra errors for missing connector dependencies (#14328) #14659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Hardik K (hardikkaurani)
wants to merge
2
commits into
microsoft:main
Choose a base branch
from
hardikkaurani:fix/optional-connector-dependencies-clean
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+161
−15
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
python/tests/unit/connectors/test_optional_dependencies.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| # Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| import sys | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| import pytest | ||
|
|
||
| from semantic_kernel.exceptions import ServiceInitializationError | ||
|
|
||
|
|
||
| def test_hugging_face_settings_instantiation_without_transformers(): | ||
| """HuggingFacePromptExecutionSettings can be imported and instantiated without transformers installed.""" | ||
| from semantic_kernel.connectors.ai.hugging_face import HuggingFacePromptExecutionSettings | ||
|
|
||
| settings = HuggingFacePromptExecutionSettings( | ||
| max_new_tokens=100, | ||
| temperature=0.7, | ||
| ) | ||
| assert settings.max_new_tokens == 100 | ||
| assert settings.temperature == 0.7 | ||
|
|
||
|
|
||
| def test_hugging_face_settings_get_generation_config_missing_dependency(): | ||
| """Calling get_generation_config without transformers produces an actionable error naming the install extra.""" | ||
| from semantic_kernel.connectors.ai.hugging_face import ( | ||
| HuggingFacePromptExecutionSettings, | ||
| hf_prompt_execution_settings, | ||
| ) | ||
|
|
||
| settings = HuggingFacePromptExecutionSettings() | ||
|
|
||
| with patch.object(hf_prompt_execution_settings, "ready", False): | ||
| with pytest.raises(ImportError) as exc_info: | ||
| settings.get_generation_config() | ||
|
|
||
| err_msg = str(exc_info.value) | ||
| assert "transformers is not installed" in err_msg | ||
| assert "semantic-kernel[hugging_face]" in err_msg | ||
|
|
||
|
|
||
| def test_hugging_face_settings_get_generation_config_available(): | ||
| """When transformers is available, get_generation_config constructs the GenerationConfig.""" | ||
| from semantic_kernel.connectors.ai.hugging_face import ( | ||
| HuggingFacePromptExecutionSettings, | ||
| hf_prompt_execution_settings, | ||
| ) | ||
|
|
||
| mock_gen_config_cls = MagicMock() | ||
| mock_transformers = MagicMock() | ||
| mock_transformers.GenerationConfig = mock_gen_config_cls | ||
|
|
||
| settings = HuggingFacePromptExecutionSettings(temperature=0.8, top_p=0.9) | ||
|
|
||
| with ( | ||
| patch.object(hf_prompt_execution_settings, "ready", True), | ||
| patch.dict(sys.modules, {"transformers": mock_transformers}), | ||
| ): | ||
| config = settings.get_generation_config() | ||
| assert config == mock_gen_config_cls.return_value | ||
| mock_gen_config_cls.assert_called_once() | ||
|
|
||
|
|
||
| def test_hugging_face_text_completion_missing_dependency(): | ||
| """HuggingFaceTextCompletion raises an actionable ImportError naming semantic-kernel[hugging_face].""" | ||
| from semantic_kernel.connectors.ai.hugging_face import HuggingFaceTextCompletion | ||
| from semantic_kernel.connectors.ai.hugging_face.services import hf_text_completion | ||
|
|
||
| with patch.object(hf_text_completion, "ready", False): | ||
| with pytest.raises(ImportError) as exc_info: | ||
| HuggingFaceTextCompletion(ai_model_id="test_model") | ||
|
|
||
| err_msg = str(exc_info.value) | ||
| assert "torch and transformers are not installed" in err_msg | ||
| assert "semantic-kernel[hugging_face]" in err_msg | ||
|
|
||
|
|
||
| def test_hugging_face_text_embedding_missing_dependency(): | ||
| """HuggingFaceTextEmbedding raises an actionable ImportError naming semantic-kernel[hugging_face].""" | ||
| from semantic_kernel.connectors.ai.hugging_face import HuggingFaceTextEmbedding | ||
| from semantic_kernel.connectors.ai.hugging_face.services import hf_text_embedding | ||
|
|
||
| with patch.object(hf_text_embedding, "torch_installed", False): | ||
| with pytest.raises(ImportError) as exc_info: | ||
| HuggingFaceTextEmbedding(ai_model_id="test_model") | ||
|
|
||
| err_msg = str(exc_info.value) | ||
| assert "torch is not installed" in err_msg | ||
| assert "semantic-kernel[hugging_face]" in err_msg | ||
|
|
||
|
|
||
| def test_onnx_gen_ai_missing_dependency(): | ||
| """OnnxGenAICompletionBase raises an actionable ImportError naming semantic-kernel[onnx].""" | ||
| from semantic_kernel.connectors.ai.onnx.services import onnx_gen_ai_completion_base | ||
| from semantic_kernel.connectors.ai.onnx.services.onnx_gen_ai_completion_base import ( | ||
| OnnxGenAICompletionBase, | ||
| ) | ||
|
|
||
| with patch.object(onnx_gen_ai_completion_base, "ready", False): | ||
| with pytest.raises(ImportError) as exc_info: | ||
| OnnxGenAICompletionBase(ai_model_path="dummy_path") | ||
|
|
||
| err_msg = str(exc_info.value) | ||
| assert "onnxruntime-genai is not installed" in err_msg | ||
| assert "semantic-kernel[onnx]" in err_msg | ||
|
|
||
|
|
||
| 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 ( | ||
| ChromaMemoryStore, | ||
| ) | ||
|
|
||
| with patch.dict(sys.modules, {"chromadb": None, "chromadb.config": None}): | ||
| with pytest.raises(ServiceInitializationError) as exc_info: | ||
| ChromaMemoryStore() | ||
|
|
||
| err_msg = str(exc_info.value) | ||
| assert "chromadb" in err_msg | ||
| assert "semantic-kernel[chroma]" in err_msg | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
ChromaMemoryStoreraisesImportError: cannot import name 'deprecated' from 'warnings'. The module currently chooseswarnings.deprecatedfor Python >=3.12, but that decorator was introduced in 3.13. Please correct that guard to >=3.13 (usingtyping_extensions.deprecatedon 3.12) so this regression test works across the supported matrix. Confirmed in https://github.com/microsoft/semantic-kernel/actions/runs/40098737410/job/121530788066.There was a problem hiding this comment.
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.