microsoft/semantic-kernel · error · ValueError
Failed to determine if the agent should terminate because th
Error message
Failed to determine if the agent should terminate because the model did not return a valid response.
What it means
Raised by custom_termination_strategy after the model failed to return either the TERMINATE_TRUE_KEYWORD or TERMINATE_FALSE_KEYWORD across all retry attempts. The strategy asks a yes/no termination decision; if the model keeps returning other tokens it raises ValueError.
Source
Thrown at python/samples/demos/document_generator/custom_termination_strategy.py:75
completion = await self.chat_completion_service.get_chat_message_content(
chat_history,
AzureChatPromptExecutionSettings(),
)
if not completion:
continue
if TERMINATE_FALSE_KEYWORD in completion.content.lower():
return False
if TERMINATE_TRUE_KEYWORD in completion.content.lower():
return True
chat_history.add_message(completion)
chat_history.add_user_message(
f"You must only say either '{TERMINATE_TRUE_KEYWORD}' or '{TERMINATE_FALSE_KEYWORD}'."
)
raise ValueError(
"Failed to determine if the agent should terminate because the model did not return a valid response."
)
def get_system_message(self) -> str:
return f"""
You are in a chat with multiple agents collaborating to create a document.
Each message in the chat history contains the agent's name and the message content.
The chat history may start empty as no agents have spoken yet.
Here are the agents with their indices, names, and descriptions:
{NEWLINE.join(f"[{index}] {agent.name}:{NEWLINE}{agent.description}" for index, agent in enumerate(self.agents))}
Your task is NOT to continue the conversation. Determine if the latest content is approved by all agents.
If approved, say "{TERMINATE_TRUE_KEYWORD}". Otherwise, say "{TERMINATE_FALSE_KEYWORD}".
"""
View on GitHub (pinned to c028a0c7dc)
Solutions
- Use a stronger instruction-following model for the termination check.
- Increase retry count so the model gets more correction prompts.
- Normalize the response (strip punctuation/whitespace, take the first token) before keyword matching.
- Ensure TERMINATE_TRUE_KEYWORD / TERMINATE_FALSE_KEYWORD are distinct, unambiguous tokens.
Example fix
// before
if TERMINATE_FALSE_KEYWORD in completion.content.lower():
return False
// after
token = (completion.content or '').strip().lower().rstrip('.!,')
if TERMINATE_FALSE_KEYWORD in token:
return False Defensive patterns
Strategy: validation
Validate before calling
def parse_termination(content: str | None, true_kw: str, false_kw: str) -> bool | None:
if not content:
return None
c = content.strip().lower().rstrip('.!,')
if false_kw in c:
return False
if true_kw in c:
return True
return None Try / catch
decision = parse_termination(completion.content, TERMINATE_TRUE_KEYWORD, TERMINATE_FALSE_KEYWORD)
if decision is not None:
return decision
chat_history.add_message(completion)
chat_history.add_user_message(f"Reply with only '{TERMINATE_TRUE_KEYWORD}' or '{TERMINATE_FALSE_KEYWORD}'.") Prevention
- Choose unambiguous termination keywords.
- Normalize model output (strip punctuation) before matching.
- Use an instruction-following model for the termination decision.
When it happens
Trigger: The model returns an explanation instead of the bare keyword; TERMINATE_TRUE_KEYWORD/TERMINATE_FALSE_KEYWORD substrings are absent from completion.content.lower(); completion.content is None.
Common situations: Weak instruction-following models; overly verbose system prompts; keywords that collide with common English words causing misfires; the model wrapping the keyword in punctuation.
Related errors
- Failed to select an agent since the model did not return a v
- 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
- Failed to complete step {stepId} for session {sessionId}.
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/f7d68df9983bd953.
Report an issue: GitHub.