{"record":{"id":"d117719f714b7ca3","repo":"microsoft/semantic-kernel","slug":"openai-azure-openai-configuration-was-not-found-d11771","errorCode":null,"errorMessage":"OpenAI/Azure OpenAI configuration was not found.","messagePattern":"OpenAI/Azure OpenAI configuration was not found\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"critical","filePath":"dotnet/samples/Demos/OpenAIRealtime/Program.cs","lineNumber":424,"sourceCode":"\n        var openAIOptions = config.GetSection(OpenAIOptions.SectionName).Get<OpenAIOptions>()!;\n        var azureOpenAIOptions = config.GetSection(AzureOpenAIOptions.SectionName).Get<AzureOpenAIOptions>()!;\n\n        if (openAIOptions is not null && openAIOptions.IsValid)\n        {\n            return new RealtimeClient(new ApiKeyCredential(openAIOptions.ApiKey));\n        }\n        else if (azureOpenAIOptions is not null && azureOpenAIOptions.IsValid)\n        {\n            var client = new AzureOpenAIClient(\n                endpoint: new Uri(azureOpenAIOptions.Endpoint),\n                credential: new ApiKeyCredential(azureOpenAIOptions.ApiKey));\n\n            return client.GetRealtimeClient();\n        }\n        else\n        {\n            throw new Exception(\"OpenAI/Azure OpenAI configuration was not found.\");\n        }\n    }\n\n    #endregion\n}\n","sourceCodeStart":406,"sourceCodeEnd":430,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/samples/Demos/OpenAIRealtime/Program.cs#L406-L430","documentation":"Thrown by GetRealtimeConversationClient when neither the OpenAI configuration (ApiKey + optional endpoint) nor the Azure OpenAI configuration (Endpoint + ApiKey, flagged IsValid) is present and valid. It is a base System.Exception (not a more specific type) signaling a startup-blocking configuration failure.","triggerScenarios":"The method loads configuration from user secrets and environment variables; if neither the OpenAI section nor the AzureOpenAI section resolves to a valid set of credentials, execution falls through to the else branch and throws.","commonSituations":"User secrets not initialized (secrets.json missing or empty); environment variables not set in a container/CI environment; the AzureOpenAIConfig.IsValid check fails because Endpoint or ApiKey is null; typo in the configuration section key name; running on a machine that never had 'dotnet user-secrets set' run for this project.","solutions":["Set OpenAI:ApiKey (and optionally OpenAI:Endpoint) via 'dotnet user-secrets set \"OpenAI:ApiKey\" \"<key>\"' or as environment variables.","Alternatively, set AzureOpenAI:Endpoint and AzureOpenAI:ApiKey via user secrets or environment variables so IsValid returns true.","Verify your user secrets project ID matches (check the <UserSecretsId> in the .csproj) and that secrets.json actually contains the key.","Use a more specific exception type (e.g., ConfigurationNotFoundException or InvalidOperationException) for clearer error handling downstream."],"exampleFix":"// before\nelse\n{\n    throw new Exception(\"OpenAI/Azure OpenAI configuration was not found.\");\n}\n\n// after — explicit, actionable message naming the missing keys\nelse\n{\n    throw new InvalidOperationException(\n        \"No OpenAI or Azure OpenAI configuration found. Set either 'OpenAI:ApiKey' or 'AzureOpenAI:Endpoint' + 'AzureOpenAI:ApiKey' in user secrets or environment variables.\");\n}","handlingStrategy":"validation","validationCode":"// Validate config presence before building the client\nvar hasOpenAI = !string.IsNullOrEmpty(config[\"OpenAI:ApiKey\"]);\nvar hasAzure = !string.IsNullOrEmpty(config[\"AzureOpenAI:Endpoint\"]) && !string.IsNullOrEmpty(config[\"AzureOpenAI:ApiKey\"]);\nif (!hasOpenAI && !hasAzure)\n    throw new InvalidOperationException(\"Set OpenAI:ApiKey or AzureOpenAI:Endpoint+ApiKey in user secrets or env vars.\");","typeGuard":"bool HasValidOpenAIConfig(IConfiguration c) => !string.IsNullOrWhiteSpace(c[\"OpenAI:ApiKey\"]);","tryCatchPattern":null,"preventionTips":["Centralize config validation at startup with a dedicated Validate method.","Provide clear startup error messages naming the exact keys and where to set them.","Use user secrets consistently and document the UserSecretsId."],"tags":["configuration","openai","azure-openai","secrets","realtime","startup"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}