microsoft/semantic-kernel · error · InvalidOperationException
Failed to get a response from the chat completion service.
Error message
Failed to get a response from the chat completion service.
What it means
Thrown by the chatbot process step when IChatCompletionService.GetChatMessageContentAsync returns a null ChatMessageContent. The Semantic Kernel chat completion contract normally returns a non-null message, so a null result indicates the underlying service failed to produce any content (model error, empty deployment, transport failure swallowed).
Source
Thrown at dotnet/samples/GettingStartedWithProcesses/Step01/Step01_Processes.cs:170
return ValueTask.CompletedTask;
}
/// <summary>
/// Generates a response from the chat completion service.
/// </summary>
/// <param name="context">The context for the current step and process. <see cref="KernelProcessStepContext"/></param>
/// <param name="userMessage">The user message from a previous step.</param>
/// <param name="_kernel">A <see cref="Kernel"/> instance.</param>
/// <returns></returns>
[KernelFunction(ProcessFunctions.GetChatResponse)]
public async Task GetChatResponseAsync(KernelProcessStepContext context, string userMessage, Kernel _kernel)
{
_state!.ChatMessages.Add(new(AuthorRole.User, userMessage));
IChatCompletionService chatService = _kernel.Services.GetRequiredService<IChatCompletionService>();
ChatMessageContent response = await chatService.GetChatMessageContentAsync(_state.ChatMessages).ConfigureAwait(false);
if (response == null)
{
throw new InvalidOperationException("Failed to get a response from the chat completion service.");
}
System.Console.ForegroundColor = ConsoleColor.Yellow;
System.Console.WriteLine($"ASSISTANT: {response.Content}");
System.Console.ResetColor();
// Update state with the response
_state.ChatMessages.Add(response);
// emit event: assistantResponse
await context.EmitEventAsync(new KernelProcessEvent { Id = ChatBotEvents.AssistantResponseGenerated, Data = response });
}
}
/// <summary>
/// The state object for the <see cref="ChatBotResponseStep"/>.
/// </summary>
private sealed class ChatBotStateView on GitHub (pinned to c028a0c7dc)
Solutions
- Verify an IChatCompletionService is registered on the kernel and the deployment/model id is correct.
- Inspect _state.ChatMessages passed in to confirm it is non-empty and well-formed.
- Upgrade or align the Semantic Kernel connector packages so GetChatMessageContentAsync throws on failure rather than returning null.
- Wrap the call to surface the underlying HTTP error instead of a generic message.
Example fix
// before
ChatMessageContent response = await chatService.GetChatMessageContentAsync(_state.ChatMessages).ConfigureAwait(false);
if (response == null) { throw new InvalidOperationException("Failed to get a response from the chat completion service."); }
// after - also log the request shape and inner cause
ChatMessageContent? response;
try { response = await chatService.GetChatMessageContentAsync(_state.ChatMessages).ConfigureAwait(false); }
catch (Exception ex) { throw new InvalidOperationException("Chat completion call failed.", ex); }
if (response is null) throw new InvalidOperationException($"No content returned for {_state.ChatMessages.Count} messages."); Defensive patterns
Strategy: try-catch
Validate before calling
var chatService = kernel.Services.GetService<IChatCompletionService>();
if (chatService is null) throw new InvalidOperationException("No IChatCompletionService registered.");
if (_state.ChatMessages.Count == 0) _state.ChatMessages.Add(new(AuthorRole.User, "hello")); Type guard
bool HasChatCompletion(Kernel k) => k.Services.GetService<IChatCompletionService>() is not null;
Try / catch
ChatMessageContent response;
try { response = await chatService.GetChatMessageContentAsync(_state.ChatMessages); }
catch (HttpRequestException ex) { /* surface real HTTP error */ throw; }
if (response is null) { /* log + fallback or retry */ } Prevention
- Always register an IChatCompletionService on the kernel before invoking completion steps.
- Confirm the model/deployment id matches an existing deployment.
- Never pass an empty chat history to a completion call.
When it happens
Trigger: Invoking the GetChatResponseAsync kernel function in Step01_Processes when the registered IChatCompletionService returns null; e.g. misconfigured model/deployment id, empty chat history, or a backend that returned an empty body.
Common situations: Wrong deployment/model id for Azure OpenAI; chat history with no messages; connector version mismatch where the service returns null instead of throwing; rate-limited or empty completion response.
Related errors
- Configuration section '{section}' not found
- Configuration section '{section}' not found
- The OpenAI model ID is required.
- OPENAI_API_KEY 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/e8b8326e9c78cd87.
Report an issue: GitHub.