microsoft/semantic-kernel · error · ValueError

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 chat step after calling chat_service.get_chat_message_contents: a None response is treated as a hard failure. get_chat_message_contents normally returns a list (possibly empty), so a true None is an abnormal service result. Note an empty list would instead fail later at response[0]; this guard targets the explicit-None case.

Source

Thrown at python/samples/getting_started_with_processes/step01/step01_processes.py:147

        self.state = state.state or ChatBotState()
        self.state.chat_messages = self.state.chat_messages or []

    @kernel_function(name=GET_CHAT_RESPONSE)
    async def get_chat_response(self, context: "KernelProcessStepContext", user_message: str, kernel: "Kernel"):
        """Generates a response from the chat completion service."""
        # Add user message to the state
        self.state.chat_messages.append({"role": "user", "message": user_message})

        # Get chat completion service and generate a response
        chat_service: ChatCompletionClientBase = kernel.get_service(service_id=SERVICE_ID)
        settings = chat_service.instantiate_prompt_execution_settings(service_id=SERVICE_ID)

        chat_history = ChatHistory()
        chat_history.add_user_message(user_message)
        response = await chat_service.get_chat_message_contents(chat_history=chat_history, settings=settings)

        if response is None:
            raise ValueError("Failed to get a response from the chat completion service.")

        answer = response[0].content

        print(f"ASSISTANT: {answer}")

        # Update state with the response
        self.state.chat_messages.append(answer)

        # Emit an event: assistantResponse
        await context.emit_event(process_event=ChatBotEvents.AssistantResponseGenerated, data=answer)


kernel = Kernel()


async def step01_processes(scripted: bool = True):
    kernel.add_service(OpenAIChatCompletion(service_id="default"))

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Verify the service registered under SERVICE_ID is a real chat completion service with valid model/credentials and returns a non-empty list.
  2. If using a custom connector, ensure it returns list[ChatMessageContent] and never None.
  3. Handle empty-list responses (guard response before indexing) so an empty completion doesn't crash with IndexError.
  4. Check logs for upstream errors that produced the None/empty result.

Example fix

# before
response = await chat_service.get_chat_message_contents(chat_history=chat_history, settings=settings)
if response is None:
    raise ValueError("Failed to get a response from the chat completion service.")
answer = response[0].content
# after (also guard empty list)
response = await chat_service.get_chat_message_contents(chat_history=chat_history, settings=settings)
if not response:
    raise ValueError("Failed to get a response from the chat completion service.")
answer = response[0].content
Defensive patterns

Strategy: validation

Validate before calling

svc = kernel.get_service(service_id=SERVICE_ID)
from semantic_kernel.connectors.ai.chat_completion_client_base import ChatCompletionClientBase
assert isinstance(svc, ChatCompletionClientBase), f'{SERVICE_ID} is not a chat service'
response = await svc.get_chat_message_contents(chat_history=chat_history, settings=settings)
assert response, 'chat service returned an empty/None response'

Type guard

from semantic_kernel.connectors.ai.chat_completion_client_base import ChatCompletionClientBase
def is_chat_service(svc) -> bool:
    return isinstance(svc, ChatCompletionClientBase)

Try / catch

try:
    response = await chat_service.get_chat_message_contents(chat_history=chat_history, settings=settings)
    if not response:
        raise ValueError('Failed to get a response from the chat completion service.')
except ValueError:
    # handle empty/None completion (retry or surface to user)
    raise

Prevention

When it happens

Trigger: The chat completion service returns None (custom/buggy connector); the service is misconfigured so it yields no completion object; an upstream exception is masked and returns None.

Common situations: Using a custom ChatCompletionClientBase subclass that returns None; pointing SERVICE_ID at a non-chat service; missing model/API settings causing a no-op response.

Related errors


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