microsoft/semantic-kernel · error · InvalidOperationException
OpenAI client not found.
Error message
OpenAI client not found.
What it means
When an AgentDefinition has no ModelConnection, GetOpenAIClient falls back to resolving an OpenAIClient from the kernel's service provider. If none is registered (FirstOrDefault returns null), it throws because no client exists to make calls.
Source
Thrown at dotnet/src/Agents/OpenAI/Extensions/AgentDefinitionExtensions.cs:163
{
var endpoint = connection.TryGetEndpoint();
Verify.NotNull(endpoint, "Endpoint must be specified when using Azure OpenAI.");
if (connection.ExtensionData.TryGetValue(ApiKey, out var apiKey) && apiKey is not null)
{
return OpenAIAssistantAgent.CreateAzureOpenAIClient(connection.GetApiKeyCredential(), endpoint, httpClient);
}
var tokenCredential = kernel.Services.GetRequiredService<TokenCredential>();
return OpenAIAssistantAgent.CreateAzureOpenAIClient(tokenCredential, endpoint, httpClient);
}
throw new InvalidOperationException($"Invalid OpenAI client type '{connection.Type}' was specified.");
}
// Use the client registered on the kernel
var client = kernel.GetAllServices<OpenAIClient>().FirstOrDefault();
return (OpenAIClient?)client ?? throw new InvalidOperationException("OpenAI client not found.");
}
#region private
private const string Temperature = "temperature";
private const string TopP = "top_p";
private static float? GetTemperature(this AgentDefinition agentDefinition)
{
Verify.NotNull(agentDefinition);
if (agentDefinition?.Model?.Options?.TryGetValue(Temperature, out var temperature) ?? false)
{
return (float?)temperature;
}
return null;
}
private static float? GetTopP(this AgentDefinition agentDefinition)View on GitHub (pinned to c028a0c7dc)
Solutions
- Register a client before invoking: kernel.AddOpenAIClient(apiKey) or kernel.AddAzureOpenAIClient(endpoint, credential).
- Alternatively, supply a ModelConnection in the agent definition so the client is built inline.
- Verify the service is registered in the same Kernel instance passed to the agent.
- Use kernel.GetAllServices<OpenAIClient>() in a startup check to fail fast with a clearer message.
Example fix
// before
var agent = await OpenAIAssistantAgent.CreateFromDefinitionAsync(kernel, "my-agent");
await foreach (var m in agent.InvokeAsync(thread)) { }
// after
kernel.AddOpenAIClient(apiKey); // or AddAzureOpenAIClient
var agent = await OpenAIAssistantAgent.CreateFromDefinitionAsync(kernel, "my-agent");
await foreach (var m in agent.InvokeAsync(thread)) { } Defensive patterns
Strategy: validation
Validate before calling
if (agentDefinition?.Model?.Connection is null &&
!kernel.GetAllServices<OpenAIClient>().Any())
throw new InvalidOperationException("No OpenAIClient registered. Call kernel.AddOpenAIClient(...) or add a ModelConnection."); Type guard
static bool HasOpenAIClient(Kernel kernel) => kernel.GetAllServices<OpenAIClient>().Any();
Try / catch
try { var client = agentDefinition.GetOpenAIClient(kernel); }
catch (InvalidOperationException ex) when (ex.Message.Contains("OpenAI client not found")) {
kernel.AddOpenAIClient(Environment.GetEnvironmentVariable("OPENAI_API_KEY")!);
} Prevention
- Register the OpenAIClient in your DI composition root.
- Add a startup health check verifying the client resolves.
- Keep the same Kernel instance for registration and agent invocation.
When it happens
Trigger: Invoking an agent whose definition lacks a connection block while the Kernel has no OpenAIClient/AzureOpenAIClient service registered via AddOpenAIClient/AddAzureOpenAIClient.
Common situations: Forgot to register the OpenAI client in the DI container; registered it under a different interface or as a non-OpenAIClient type; DI scope/lifetime mismatch so the service is unavailable where the agent runs.
Related errors
- OPENAI_API_KEY is not set.
- AZURE_FOUNDRY_PROJECT_ENDPOINT is not set.
- AZURE_OPENAI_ENDPOINT is not set.
- AZURE_OPENAI_ENDPOINT is not set.
- OPENAI_API_KEY is not set.
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/d26e898c132eac07.
Report an issue: GitHub.