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).

Solutions

  1. Verify an IChatCompletionService is registered on the kernel and the deployment/model id is correct.
  2. Inspect _state.ChatMessages passed in to confirm it is non-empty and well-formed.
  3. Upgrade or align the Semantic Kernel connector packages so GetChatMessageContentAsync throws on failure rather than returning null.
  4. 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

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


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

Appendix: 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 ChatBotState

View on GitHub (pinned to c028a0c7dc)