microsoft/semantic-kernel · error · ServiceInitializationError

Failed to validate Mistral AI settings: {e}

Error message

Failed to validate Mistral AI settings: {e}

What it means

Raised as ServiceInitializationError at construction of MistralAITextEmbedding when instantiating the pydantic `MistralAISettings` raises a `ValidationError`. This happens BEFORE any network call: it means the supplied/loaded configuration is structurally invalid (e.g. api_key present but wrong type, env file unreadable, a field failing its validator). The full pydantic error list is interpolated into the message.

Source

Thrown at python/semantic_kernel/connectors/ai/mistral_ai/services/mistral_ai_text_embedding.py:66

            ai_model_id: : A string that is used to identify the model such as the model name.
            api_key : The API key for the Mistral AI service deployment.
            service_id : Service ID for the embedding completion service.
            async_client : The Mistral AI client to use.
            env_file_path : The path to the environment file.
            env_file_encoding : The encoding of the environment file.

        Raises:
            ServiceInitializationError: If an error occurs during initialization.
        """
        try:
            mistralai_settings = MistralAISettings(
                api_key=api_key,
                embedding_model_id=ai_model_id,
                env_file_path=env_file_path,
                env_file_encoding=env_file_encoding,
            )
        except ValidationError as e:
            raise ServiceInitializationError(f"Failed to validate Mistral AI settings: {e}") from e

        if not mistralai_settings.embedding_model_id:
            raise ServiceInitializationError("The MistralAI embedding model ID is required.")

        if not async_client:
            async_client = Mistral(
                api_key=mistralai_settings.api_key.get_secret_value(),
            )
        super().__init__(
            service_id=service_id or mistralai_settings.embedding_model_id,
            ai_model_id=ai_model_id or mistralai_settings.embedding_model_id,
            async_client=async_client,
        )

    @override
    async def generate_embeddings(
        self,
        texts: list[str],

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Read the interpolated `{e}`: pydantic lists each failing field and why - fix that field first.
  2. Set MISTRALAI_API_KEY (and MISTRALAI_EMBEDDING_MODEL_ID) in the environment or a .env file at the project root.
  3. Pass `api_key=` explicitly to the constructor to bypass env resolution.
  4. Confirm the .env path in `env_file_path=` exists and uses the configured `env_file_encoding` (default utf-8).

Example fix

# before
svc = MistralAITextEmbedding()  # ValidationError -> ServiceInitializationError

# after
svc = MistralAITextEmbedding(
    api_key=os.environ['MISTRALAI_API_KEY'],
    ai_model_id='mistral-embed',
)
Defensive patterns

Strategy: validation

Validate before calling

from semantic_kernel.connectors.ai.mistral_ai import MistralAISettings
from pydantic import ValidationError
try:
    s = MistralAISettings(api_key=os.environ.get('MISTRALAI_API_KEY'),
                          embedding_model_id=os.environ.get('MISTRALAI_EMBEDDING_MODEL_ID'))
    assert s.api_key and s.embedding_model_id
except ValidationError as e:
    raise SystemExit(f'Fix Mistral settings first: {e}')

Type guard

from semantic_kernel.exceptions import ServiceInitializationError

def is_mistral_settings_error(e: BaseException) -> bool:
    return isinstance(e, ServiceInitializationError) and 'Failed to validate Mistral AI settings' in str(e)

Try / catch

from semantic_kernel.exceptions import ServiceInitializationError
try:
    svc = MistralAITextEmbedding()
except ServiceInitializationError as e:
    raise SystemExit(f'Mistral embedding service misconfigured: {e}') from e

Prevention

When it happens

Trigger: Constructing `MistralAITextEmbedding(...)` where the merged config (constructor args + MISTRALAI_API_KEY/MISTRALAI_EMBEDDING_MODEL_ID env vars + .env file) fails pydantic validation: api_key is None where a SecretStr is required, a field fails format validation, or env_file_path points to a missing/unparseable file.

Common situations: Missing .env file pointed to by env_file_path, typo'd env var names, MISTRALAI_API_KEY set to an empty string, or a pydantic v1/v2 mismatch changing validator behavior.

Related errors


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