microsoft/semantic-kernel · critical · ServiceInitializationError

Failed to create Google settings.

Error message

Failed to create Google settings.

What it means

GoogleSearch.__init__ builds a GoogleSearchSettings object, which loads api_key (a required pydantic SecretStr) from the constructor arg or the env var GOOGLE_SEARCH_API_KEY (settings prefix GOOGLE_SEARCH_). If validation fails the pydantic ValidationError is caught and re-raised as ServiceInitializationError. This is a startup/configuration failure: the connector cannot be constructed without a valid API key.

Source

Thrown at python/semantic_kernel/connectors/google_search.py:181

        Args:
            api_key: The Google Search API key. If provided, will override
                the value in the env vars or .env file.
            search_engine_id: The Google search engine ID.
                If provided, will override the value in the env vars or .env file.
            env_file_path: The optional path to the .env file. If provided,
                the settings are read from this file path location.
            env_file_encoding: The optional encoding of the .env file.
        """
        try:
            settings = GoogleSearchSettings(
                api_key=api_key,
                engine_id=search_engine_id,
                env_file_path=env_file_path,
                env_file_encoding=env_file_encoding,
            )
        except ValidationError as ex:
            raise ServiceInitializationError("Failed to create Google settings.") from ex

        super().__init__(settings=settings)  # type: ignore[call-arg]

    @override
    async def search(
        self,
        query: str,
        output_type: type[str] | type[TSearchResult] | Literal["Any"] = str,
        *,
        filter: OptionalOneOrList[Callable | str] = None,
        skip: int = 0,
        top: int = 5,
        include_total_count: bool = False,
        **kwargs: Any,
    ) -> "KernelSearchResults[TSearchResult]":
        options = SearchOptions(filter=filter, skip=skip, top=top, include_total_count=include_total_count, **kwargs)
        results = await self._inner_search(query=query, options=options)
        return KernelSearchResults(

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Set the env var GOOGLE_SEARCH_API_KEY (and optionally GOOGLE_SEARCH_ENGINE_ID) in your shell, container, or .env.
  2. Pass the key explicitly: GoogleSearch(api_key='...', search_engine_id='...').
  3. If using a .env file, pass env_file_path='/path/to/.env' and confirm it is readable and uses the GOOGLE_SEARCH_ prefix.

Example fix

# before
search = GoogleSearch()  # no env var, no .env -> ValidationError -> [1302]

# after
import os
os.environ['GOOGLE_SEARCH_API_KEY'] = '<your key>'
os.environ['GOOGLE_SEARCH_ENGINE_ID'] = '<your cx>'
search = GoogleSearch(env_file_path='.env')
Defensive patterns

Strategy: validation

Validate before calling

import os

def ensure_google_settings_present():
    if not os.environ.get('GOOGLE_SEARCH_API_KEY'):
        raise RuntimeError('Set GOOGLE_SEARCH_API_KEY (or pass api_key=) before creating GoogleSearch')
    # engine_id is optional at settings level but required for real queries

Try / catch

from semantic_kernel.exceptions import ServiceInitializationError
try:
    google = GoogleSearch(env_file_path='.env')
except ServiceInitializationError as ex:
    # ex.__cause__ is the pydantic ValidationError listing missing fields
    raise SystemExit(f'Google Search misconfigured: {ex.__cause__.errors()}') from ex

Prevention

When it happens

Trigger: Instantiating GoogleSearch() with no api_key argument and no GOOGLE_SEARCH_API_KEY environment variable or .env entry; passing an empty string or a value of the wrong type; pointing env_file_path at a missing/unreadable file.

Common situations: Forgot to export the env var locally; .env not on the loaded path; CI/containers missing the secret; wrong prefix (e.g. GOOGLE_API_KEY instead of GOOGLE_SEARCH_API_KEY); key revoked or pasted with stray whitespace.

Related errors


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