{"record":{"id":"d4c63e076ba68da2","repo":"microsoft/semantic-kernel","slug":"error-azure-cognitive-search-client-not-set","errorCode":null,"errorMessage":"Error: Azure Cognitive Search client not set.","messagePattern":"Error: Azure Cognitive Search client not set\\.","errorType":"validation","errorClass":"ServiceInitializationError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/memory_stores/azure_cognitive_search/utils.py","lineNumber":54,"sourceCode":"        token_credential (TokenCredential): Optional Token credential (default: {None}).\n    \"\"\"\n    ENV_VAR_ENDPOINT = \"AZURE_COGNITIVE_SEARCH_ENDPOINT\"\n    ENV_VAR_API_KEY = \"AZURE_COGNITIVE_SEARCH_ADMIN_KEY\"\n\n    # Load environment variables\n    load_dotenv()\n\n    # Service endpoint\n    if search_endpoint:\n        service_endpoint = search_endpoint\n    elif os.getenv(ENV_VAR_ENDPOINT):\n        service_endpoint = os.getenv(ENV_VAR_ENDPOINT)\n    else:\n        raise ServiceInitializationError(\"Error: missing Azure Cognitive Search client endpoint.\")\n\n    if service_endpoint is None:\n        print(service_endpoint)\n        raise ServiceInitializationError(\"Error: Azure Cognitive Search client not set.\")\n\n    # Credentials\n    if admin_key:\n        azure_credential = AzureKeyCredential(admin_key)\n    elif azure_credential:\n        azure_credential = azure_credential\n    elif token_credential:\n        token_credential = token_credential\n    elif os.getenv(ENV_VAR_API_KEY):\n        azure_credential = AzureKeyCredential(os.getenv(ENV_VAR_API_KEY))\n    else:\n        raise ServiceInitializationError(\"Error: missing Azure Cognitive Search client credentials.\")\n\n    if azure_credential is None and token_credential is None:\n        raise ServiceInitializationError(\"Error: Azure Cognitive Search credentials not set.\")\n\n    sk_headers = {USER_AGENT: \"Semantic-Kernel\"}\n","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/memory_stores/azure_cognitive_search/utils.py#L36-L72","documentation":"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.","triggerScenarios":"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.","commonSituations":"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`.","solutions":["Treat as a defensive assertion: ensure you supply a valid `search_endpoint` or `AZURE_COGNITIVE_SEARCH_ENDPOINT` so resolution succeeds earlier.","If hit, audit any monkeypatching of `os.getenv`/`load_dotenv` in your test or runtime harness.","Report as a bug if it triggers under normal configuration, since the logic intends to catch this at the earlier branch (error 1371)."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"# Ensure a truthy endpoint is resolved (the normal path); this guard is\n# otherwise a defensive, effectively-unreachable check.\nimport os\nep = search_endpoint or os.getenv(\"AZURE_COGNITIVE_SEARCH_ENDPOINT\")\nassert ep, \"endpoint resolution failed unexpectedly\"","typeGuard":"def endpoint_not_none(ep) -> bool:\n    return ep is not None","tryCatchPattern":"from semantic_kernel.exceptions import ServiceInitializationError\ntry:\n    client = get_search_index_async_client(...)\nexcept ServiceInitializationError as e:\n    if \"client not set\" in str(e):\n        # defensive branch; treat as misconfiguration/regression\n        raise SystemExit(\"endpoint unexpectedly None; audit getenv/load_dotenv\") from e\n    raise","preventionTips":["Supply a valid endpoint so the normal path is taken (avoids this defensive branch).","Avoid monkeypatching `os.getenv`/`load_dotenv` in ways that yield None unexpectedly.","Treat hitting this branch as a bug/regression signal."],"tags":["azure-cognitive-search","endpoint","defensive-guard","semantic-kernel"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}