microsoft/semantic-kernel · error · ValueError
Failed to select an agent since the model did not return a v
Error message
Failed to select an agent since the model did not return a valid index
What it means
Raised by custom_selection_strategy.next() after the model repeatedly returned content that cannot be parsed as an integer index (int(completion.content) raises ValueError each retry). The strategy asks the LLM to output a number 0..len(agents)-1; if after all retries the model still fails, it gives up and raises.
Source
Thrown at python/samples/demos/document_generator/custom_selection_strategy.py:74
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(),
)
if completion is None:
continue
try:
return agents[int(completion.content)]
except ValueError as ex:
chat_history.add_message(completion)
chat_history.add_user_message(str(ex))
chat_history.add_user_message(f"You must only say a number between 0 and {len(agents) - 1}.")
raise ValueError("Failed to select an agent since the model did not return a valid index")
def get_system_message(self, agents: list["Agent"]) -> str:
return f"""
You are in a multi-agent chat to create a document.
Each message in the chat history contains the agent's name and the message content.
Initially, the chat history may be empty.
Here are the agents with their indices, names, and descriptions:
{NEWLINE.join(f"[{index}] {agent.name}:{NEWLINE}{agent.description}" for index, agent in enumerate(agents))}
Your task is to select the next agent based on the conversation history.
The conversation must follow these steps:
1. The content creation agent writes a draft.
2. The code validation agent checks the code in the draft.
3. The content creation agent updates the draft based on the feedback.
4. The code validation agent checks the updated code.View on GitHub (pinned to c028a0c7dc)
Solutions
- Switch to a stronger instruction-following model for selection (e.g. gpt-4o-class).
- Pre-strip and validate completion.content: extract the first integer via regex before int(), reducing parse failures.
- Increase the number of retries the loop performs before giving up.
- Lower temperature / tighten the system prompt so the model emits only a number.
Example fix
// before
return agents[int(completion.content)]
// after
import re
m = re.search(r'\d+', completion.content or '')
if not m:
chat_history.add_message(completion)
chat_history.add_user_message('Respond with ONLY a single integer index.')
continue
idx = int(m.group())
if 0 <= idx < len(agents):
return agents[idx] Defensive patterns
Strategy: try-catch
Validate before calling
import re
def parse_index(content: str, count: int) -> int | None:
m = re.search(r'\d+', content or '')
if not m:
return None
idx = int(m.group())
return idx if 0 <= idx < count else None Try / catch
try:
return agents[int(completion.content)]
except (ValueError, IndexError, TypeError) as ex:
chat_history.add_message(completion)
chat_history.add_user_message(f'Reply with a single integer 0..{len(agents)-1}. Error: {ex}')
continue Prevention
- Use a stronger model for selection.
- Pre-extract digits from the model output before int().
- Tune the system prompt to demand a bare integer.
When it happens
Trigger: The LLM returns prose, multiple numbers, out-of-range numbers, or non-numeric tokens when asked for the agent index; a weak model or low temperature setup that ignores the 'only a number' instruction; completion.content is None or empty.
Common situations: Using a smaller/cheaper model that struggles with strict formatting; the prompt being drowned by long history; the agent index exceeding single digits where parsing gets ambiguous.
Related errors
- Failed to determine if the agent should terminate because th
- No agents to select from
- Unexpected failure broadcasting to channel: {channelRef.Chan
- The message could not be added to the thread due to an error
- Invalid key-value pair format: {inputPair}; expecting "{keyN
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/35010ef441a355b5.
Report an issue: GitHub.