microsoft/semantic-kernel · error · ServiceInitializationError

Failed to create Brave settings.

Error message

Failed to create Brave settings.

What it means

The Brave connector constructs a BraveSettings (pydantic) object during initialization; if validation fails — most often because the required api_key is missing — pydantic raises ValidationError, which the connector re-raises as ServiceInitializationError. This prevents instantiating a connector that could never authenticate.

Source

Thrown at python/semantic_kernel/connectors/brave.py:141

    ) -> None:
        """Initializes a new instance of the Brave Search class.

        Args:
            api_key: The Brave Search API key. 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. If provided,
                the settings are read from this file path location.
        """
        try:
            settings = BraveSettings(
                api_key=api_key,
                env_file_path=env_file_path,
                env_file_encoding=env_file_encoding,
            )
        except ValidationError as ex:
            raise ServiceInitializationError("Failed to create Brave 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. Provide the key explicitly: BraveSearch(api_key="...") or BraveSettings via constructor.
  2. Set the expected environment variable (consult BraveSettings field aliases) or a .env file at the given env_file_path so the key loads.
  3. Verify the key is present and non-empty before constructing the connector.

Example fix

// before
connector = BraveSearch()  # no api_key anywhere
// after
connector = BraveSearch(api_key=os.environ["BRAVE_API_KEY"])
Defensive patterns

Strategy: validation

Validate before calling

import os
api_key = os.environ.get("BRAVE_API_KEY") or os.environ.get("BRAVE_API_KEY")  # per BraveSettings alias
if not api_key:
    raise ValueError("BRAVE_API_KEY not set; cannot initialize Brave connector")

Type guard

def has_brave_key() -> bool:
    return bool(os.environ.get("BRAVE_API_KEY"))

Prevention

When it happens

Trigger: Instantiating BraveSearch(connector) without an api_key argument and without a BRAVE api key available in environment variables or a .env file. The BraveSettings model requires api_key, so its absence triggers ValidationError caught at brave.py:140-141.

Common situations: Forgetting to set the Brave API key env var before creating the connector, pointing env_file_path at a missing/incorrect .env, or a typo in the env var name. Also after rotating/invalidating a key.

Related errors


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