microsoft/semantic-kernel · error · ValueError

No agents to select from

Error message

No agents to select from

What it means

Raised by custom_selection_strategy.next() when the agents list passed in is empty. The selection strategy must choose an agent from the candidates; with zero candidates there is nothing to choose, so it fails fast with ValueError rather than returning None.

Source

Thrown at python/samples/demos/document_generator/custom_selection_strategy.py:43

class CustomSelectionStrategy(SelectionStrategy):
    """A selection strategy that selects the next agent intelligently."""

    NUM_OF_RETRIES: ClassVar[int] = 3

    chat_completion_service: ChatCompletionClientBase = Field(default_factory=lambda: OpenAIChatCompletion())

    async def next(self, agents: list["Agent"], history: list["ChatMessageContent"]) -> "Agent":
        """Select the next agent to interact with.

        Args:
            agents: The list of agents to select from.
            history: The history of messages in the conversation.

        Returns:
            The next agent to interact with.
        """
        if len(agents) == 0:
            raise ValueError("No agents to select from")

        tracer = trace.get_tracer(__name__)
        with tracer.start_as_current_span("selection_strategy"):
            chat_history = ChatHistory(system_message=self.get_system_message(agents).strip())

            for message in history:
                content = message.content
                # We don't want to add messages whose text content is empty.
                # Those messages are likely messages from function calls and function results.
                if content:
                    chat_history.add_message(message)

            chat_history.add_user_message("Now follow the rules and select the next agent by typing the agent's index.")

            for _ in range(self.NUM_OF_RETRIES):
                completion = await self.chat_completion_service.get_chat_message_content(
                    chat_history,
                    AzureChatPromptExecutionSettings(),

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Ensure at least one Agent is registered in the AgentGroupChat / passed to the selection strategy.
  2. Guard the call site: skip selection and end the conversation when len(agents) == 0.
  3. Check how the agents list is built upstream (registration, filtering) and confirm it is non-empty.
  4. If the empty list is intentional for termination, handle it before invoking next().

Example fix

// before
strategy.next(agents=[], history=history)

// after
if not agents:
    # end conversation or register agents first
    return
strategy.next(agents=agents, history=history)
Defensive patterns

Strategy: validation

Validate before calling

if not agents:
    # nothing to select; end conversation or raise a domain-specific signal
    raise StopAsyncIteration('No agents registered')
await strategy.next(agents=agents, history=history)

Type guard

from typing import Any

def has_agents(agents: list[Any]) -> bool:
    return isinstance(agents, list) and len(agents) > 0

Prevention

When it happens

Trigger: Calling next(agents=[], history=...) directly; running an AgentGroupChat whose available_agents/agents collection was initialized empty; filtering agents down to an empty list before invoking the strategy.

Common situations: Registering a group chat with no agents; programmatically removing agents based on a condition that excludes all of them; misconfiguring the chat so the agents list resolves to empty.

Related errors


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