{"record":{"id":"f7d68df9983bd953","repo":"microsoft/semantic-kernel","slug":"failed-to-determine-if-the-agent-should-terminate","errorCode":null,"errorMessage":"Failed to determine if the agent should terminate because the model did not return a valid response.","messagePattern":"Failed to determine if the agent should terminate because the model did not return a valid response\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/samples/demos/document_generator/custom_termination_strategy.py","lineNumber":75,"sourceCode":"                completion = await self.chat_completion_service.get_chat_message_content(\n                    chat_history,\n                    AzureChatPromptExecutionSettings(),\n                )\n\n                if not completion:\n                    continue\n\n                if TERMINATE_FALSE_KEYWORD in completion.content.lower():\n                    return False\n                if TERMINATE_TRUE_KEYWORD in completion.content.lower():\n                    return True\n\n                chat_history.add_message(completion)\n                chat_history.add_user_message(\n                    f\"You must only say either '{TERMINATE_TRUE_KEYWORD}' or '{TERMINATE_FALSE_KEYWORD}'.\"\n                )\n\n            raise ValueError(\n                \"Failed to determine if the agent should terminate because the model did not return a valid response.\"\n            )\n\n    def get_system_message(self) -> str:\n        return f\"\"\"\nYou are in a chat with multiple agents collaborating to create a document.\nEach message in the chat history contains the agent's name and the message content.\n\nThe chat history may start empty as no agents have spoken yet.\n\nHere are the agents with their indices, names, and descriptions:\n{NEWLINE.join(f\"[{index}] {agent.name}:{NEWLINE}{agent.description}\" for index, agent in enumerate(self.agents))}\n\nYour task is NOT to continue the conversation. Determine if the latest content is approved by all agents.\nIf approved, say \"{TERMINATE_TRUE_KEYWORD}\". Otherwise, say \"{TERMINATE_FALSE_KEYWORD}\".\n\"\"\"\n","sourceCodeStart":57,"sourceCodeEnd":92,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/samples/demos/document_generator/custom_termination_strategy.py#L57-L92","documentation":"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.","triggerScenarios":"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.","commonSituations":"Weak instruction-following models; overly verbose system prompts; keywords that collide with common English words causing misfires; the model wrapping the keyword in punctuation.","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."],"exampleFix":"// before\nif TERMINATE_FALSE_KEYWORD in completion.content.lower():\n    return False\n\n// after\ntoken = (completion.content or '').strip().lower().rstrip('.!,')\nif TERMINATE_FALSE_KEYWORD in token:\n    return False","handlingStrategy":"validation","validationCode":"def parse_termination(content: str | None, true_kw: str, false_kw: str) -> bool | None:\n    if not content:\n        return None\n    c = content.strip().lower().rstrip('.!,')\n    if false_kw in c:\n        return False\n    if true_kw in c:\n        return True\n    return None","typeGuard":null,"tryCatchPattern":"decision = parse_termination(completion.content, TERMINATE_TRUE_KEYWORD, TERMINATE_FALSE_KEYWORD)\nif decision is not None:\n    return decision\nchat_history.add_message(completion)\nchat_history.add_user_message(f\"Reply with only '{TERMINATE_TRUE_KEYWORD}' or '{TERMINATE_FALSE_KEYWORD}'.\")","preventionTips":["Choose unambiguous termination keywords.","Normalize model output (strip punctuation) before matching.","Use an instruction-following model for the termination decision."],"tags":["llm","multi-agent","termination-strategy","parsing","model-output"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}