microsoft/semantic-kernel · error · ServiceInitializationError

Error: Azure Cognitive Search client not set.

Error message

Error: Azure Cognitive Search client not set.

What it means

Defensive guard in `get_search_index_async_client`: after resolving `service_endpoint`, it checks `if service_endpoint is None` and raises `ServiceInitializationError`. Given the preceding `if/elif/else` logic, `service_endpoint` is only assigned from a truthy `search_endpoint` or a truthy `os.getenv(ENV_VAR_ENDPOINT)`, so reaching this None check is effectively a logic/regression safety net rather than a normally reachable user error.

Source

Thrown at python/semantic_kernel/connectors/memory_stores/azure_cognitive_search/utils.py:54

        token_credential (TokenCredential): Optional Token credential (default: {None}).
    """
    ENV_VAR_ENDPOINT = "AZURE_COGNITIVE_SEARCH_ENDPOINT"
    ENV_VAR_API_KEY = "AZURE_COGNITIVE_SEARCH_ADMIN_KEY"

    # Load environment variables
    load_dotenv()

    # Service endpoint
    if search_endpoint:
        service_endpoint = search_endpoint
    elif os.getenv(ENV_VAR_ENDPOINT):
        service_endpoint = os.getenv(ENV_VAR_ENDPOINT)
    else:
        raise ServiceInitializationError("Error: missing Azure Cognitive Search client endpoint.")

    if service_endpoint is None:
        print(service_endpoint)
        raise ServiceInitializationError("Error: Azure Cognitive Search client not set.")

    # Credentials
    if admin_key:
        azure_credential = AzureKeyCredential(admin_key)
    elif azure_credential:
        azure_credential = azure_credential
    elif token_credential:
        token_credential = token_credential
    elif os.getenv(ENV_VAR_API_KEY):
        azure_credential = AzureKeyCredential(os.getenv(ENV_VAR_API_KEY))
    else:
        raise ServiceInitializationError("Error: missing Azure Cognitive Search client credentials.")

    if azure_credential is None and token_credential is None:
        raise ServiceInitializationError("Error: Azure Cognitive Search credentials not set.")

    sk_headers = {USER_AGENT: "Semantic-Kernel"}

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Treat as a defensive assertion: ensure you supply a valid `search_endpoint` or `AZURE_COGNITIVE_SEARCH_ENDPOINT` so resolution succeeds earlier.
  2. If hit, audit any monkeypatching of `os.getenv`/`load_dotenv` in your test or runtime harness.
  3. Report as a bug if it triggers under normal configuration, since the logic intends to catch this at the earlier branch (error 1371).
Defensive patterns

Strategy: validation

Validate before calling

# Ensure a truthy endpoint is resolved (the normal path); this guard is
# otherwise a defensive, effectively-unreachable check.
import os
ep = search_endpoint or os.getenv("AZURE_COGNITIVE_SEARCH_ENDPOINT")
assert ep, "endpoint resolution failed unexpectedly"

Type guard

def endpoint_not_none(ep) -> bool:
    return ep is not None

Try / catch

from semantic_kernel.exceptions import ServiceInitializationError
try:
    client = get_search_index_async_client(...)
except ServiceInitializationError as e:
    if "client not set" in str(e):
        # defensive branch; treat as misconfiguration/regression
        raise SystemExit("endpoint unexpectedly None; audit getenv/load_dotenv") from e
    raise

Prevention

When it happens

Trigger: Only reachable if the assignment logic is altered such that `service_endpoint` stays None while bypassing the earlier `else` raise — i.e. a code regression or a monkeypatched environment returning an unusual value. Under current code it is essentially unreachable.

Common situations: Patching `os.getenv` in tests to return a falsy-but-not-None-then-None sequence; a future refactor that breaks the endpoint resolution branches; importing the module with a modified `load_dotenv`.

Related errors


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