microsoft/semantic-kernel · error · ImportError

transformers is not installed.

Error message

transformers is not installed.

What it means

Raised by HuggingFacePromptExecutionSettings.get_generation_config() when the module-level 'ready' flag is False. 'ready' is computed at import time by attempting to import the 'transformers' package and checking for GenerationConfig; if transformers isn't installed or lacks that symbol, calling get_generation_config (and thus prepare_settings_dict) raises ImportError.

Source

Thrown at python/semantic_kernel/connectors/ai/hugging_face/hf_prompt_execution_settings.py:33

class HuggingFacePromptExecutionSettings(PromptExecutionSettings):
    """Hugging Face prompt execution settings."""

    do_sample: bool = True
    max_new_tokens: int = 256
    num_return_sequences: int = 1
    stop_sequences: Any = None
    pad_token_id: int = 50256
    eos_token_id: int = 50256
    temperature: float = 1.0
    top_p: float = 1.0

    def get_generation_config(self) -> "GenerationConfig":
        """Get the generation config."""
        from transformers import GenerationConfig

        if not ready:
            raise ImportError("transformers is not installed.")

        return GenerationConfig(
            **self.model_dump(
                include={"max_new_tokens", "pad_token_id", "eos_token_id", "temperature", "top_p"},
                exclude_unset=False,
                exclude_none=True,
                by_alias=True,
            )
        )

    def prepare_settings_dict(self, **kwargs) -> dict[str, Any]:
        """Prepare the settings dictionary."""
        gen_config = self.get_generation_config()
        settings = {
            "generation_config": gen_config,
            "num_return_sequences": self.num_return_sequences,
            "do_sample": self.do_sample,
        }

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Install the transformers dependency: pip install transformers (or the project's hugging-face extra).
  2. Verify importlib can resolve transformers and that GenerationConfig exists: python -c 'from transformers import GenerationConfig'.
  3. Pin a compatible transformers version per the project's requirements.
  4. Ensure the same environment that loads HuggingFacePromptExecutionSettings also has transformers installed.

Example fix

# before: transformers missing
pip install transformers
# after
python -c "from transformers import GenerationConfig; print('ok')"
Defensive patterns

Strategy: validation

Validate before calling

import importlib
_ready = False
try:
    m = importlib.import_module('transformers')
    _ready = hasattr(m, 'GenerationConfig')
except ImportError:
    _ready = False
assert _ready, 'Install transformers: pip install transformers'

Type guard

def transformers_ready() -> bool:
    import importlib
    try:
        return hasattr(importlib.import_module('transformers'), 'GenerationConfig')
    except ImportError:
        return False

Prevention

When it happens

Trigger: Calling a HuggingFace text-completion flow without the 'transformers' extra installed, or with a transformers version so old it lacks GenerationConfig. Triggered when prepare_settings_dict() is invoked to build generation config.

Common situations: Installing semantic-kernel without the hugging_face/transformers extra. Downgrading transformers below the required version. Running in a slimmed image that strips optional deps.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/e6959cf733b8f2e0. Report an issue: GitHub.