{"record":{"id":"7c127f538b6b279c","repo":"microsoft/semantic-kernel","slug":"no-agents-to-select-from","errorCode":null,"errorMessage":"No agents to select from","messagePattern":"No agents to select from","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/samples/demos/document_generator/custom_selection_strategy.py","lineNumber":43,"sourceCode":"class CustomSelectionStrategy(SelectionStrategy):\n    \"\"\"A selection strategy that selects the next agent intelligently.\"\"\"\n\n    NUM_OF_RETRIES: ClassVar[int] = 3\n\n    chat_completion_service: ChatCompletionClientBase = Field(default_factory=lambda: OpenAIChatCompletion())\n\n    async def next(self, agents: list[\"Agent\"], history: list[\"ChatMessageContent\"]) -> \"Agent\":\n        \"\"\"Select the next agent to interact with.\n\n        Args:\n            agents: The list of agents to select from.\n            history: The history of messages in the conversation.\n\n        Returns:\n            The next agent to interact with.\n        \"\"\"\n        if len(agents) == 0:\n            raise ValueError(\"No agents to select from\")\n\n        tracer = trace.get_tracer(__name__)\n        with tracer.start_as_current_span(\"selection_strategy\"):\n            chat_history = ChatHistory(system_message=self.get_system_message(agents).strip())\n\n            for message in history:\n                content = message.content\n                # We don't want to add messages whose text content is empty.\n                # Those messages are likely messages from function calls and function results.\n                if content:\n                    chat_history.add_message(message)\n\n            chat_history.add_user_message(\"Now follow the rules and select the next agent by typing the agent's index.\")\n\n            for _ in range(self.NUM_OF_RETRIES):\n                completion = await self.chat_completion_service.get_chat_message_content(\n                    chat_history,\n                    AzureChatPromptExecutionSettings(),","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/samples/demos/document_generator/custom_selection_strategy.py#L25-L61","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure at least one Agent is registered in the AgentGroupChat / passed to the selection strategy.","Guard the call site: skip selection and end the conversation when len(agents) == 0.","Check how the agents list is built upstream (registration, filtering) and confirm it is non-empty.","If the empty list is intentional for termination, handle it before invoking next()."],"exampleFix":"// before\nstrategy.next(agents=[], history=history)\n\n// after\nif not agents:\n    # end conversation or register agents first\n    return\nstrategy.next(agents=agents, history=history)","handlingStrategy":"validation","validationCode":"if not agents:\n    # nothing to select; end conversation or raise a domain-specific signal\n    raise StopAsyncIteration('No agents registered')\nawait strategy.next(agents=agents, history=history)","typeGuard":"from typing import Any\n\ndef has_agents(agents: list[Any]) -> bool:\n    return isinstance(agents, list) and len(agents) > 0","tryCatchPattern":null,"preventionTips":["Validate the agents collection at AgentGroupChat construction.","Log the agent count before invoking selection strategies.","Treat an empty agent list as a termination condition upstream."],"tags":["agents","multi-agent","input-validation","selection-strategy"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}