{"record":{"id":"9147acc8cc23f161","repo":"microsoft/semantic-kernel","slug":"agent-failure-strategy-failed-to-execute-functio","errorCode":null,"errorMessage":"Agent Failure - Strategy failed to execute function.","messagePattern":"Agent Failure - Strategy failed to execute function\\.","errorType":"exception","errorClass":"AgentExecutionException","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/agents/strategies/selection/kernel_function_selection_strategy.py","lineNumber":98,"sourceCode":"            **original_arguments,\n            **extracted_settings,\n            **{k: v for k, v in filtered_arguments.items()},\n        }\n\n        arguments = KernelArguments(\n            **combined_arguments,\n        )\n\n        logger.info(\n            f\"Kernel Function Selection Strategy next method called, \"\n            f\"invoking function: {self.function.plugin_name}, {self.function.name}\",\n        )\n\n        try:\n            result = await self.function.invoke(kernel=self.kernel, arguments=arguments)\n        except Exception as ex:\n            logger.error(\"Kernel Function Selection Strategy next method failed\", exc_info=ex)\n            raise AgentExecutionException(\"Agent Failure - Strategy failed to execute function.\") from ex\n\n        logger.info(\n            f\"Kernel Function Selection Strategy next method completed: \"\n            f\"{self.function.plugin_name}, {self.function.name}, result: {result.value if result else None}\",\n        )\n\n        agent_name = self.result_parser(result)\n        if isawaitable(agent_name):\n            agent_name = await agent_name\n\n        if agent_name is None:\n            raise AgentExecutionException(\"Agent Failure - Strategy unable to determine next agent.\")\n\n        agent_turn = next((agent for agent in agents if agent.name == agent_name), None)\n        if agent_turn is None:\n            raise AgentExecutionException(f\"Agent Failure - Strategy unable to select next agent: {agent_name}\")\n\n        return agent_turn","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/agents/strategies/selection/kernel_function_selection_strategy.py#L80-L116","documentation":"KernelFunctionSelectionStrategy invokes a KernelFunction to choose the next agent; any exception raised by `self.function.invoke(kernel, arguments)` is caught and re-raised as AgentExecutionException. The original exception is chained as __cause__ and also logged. This is a generic wrapper, so the real failure is downstream.","triggerScenarios":"The selection function raises during invoke: a missing or misnamed kernel argument (agent_variable_name / history_variable_name), a prompt-template/render error, an AI service HTTP or auth error, a function that is not registered on the kernel, or a malformed result the invoke pipeline cannot process.","commonSituations":"Wrong or missing execution settings / AI service on the kernel; the function references variables not supplied by the strategy; API key or network problems; incompatible prompt template format; function plugin not added to the kernel.","solutions":["Inspect the chained exception (`except AgentExecutionException as e: cause = e.__cause__`) to find the real error.","Call `await strategy.function.invoke(kernel=kernel, arguments=...)` in isolation with the same arguments to reproduce the underlying fault.","Confirm the AI service and execution settings are configured on the kernel and that the function/plugin is registered.","Verify the variable names the function expects match agent_variable_name/history_variable_name (defaults _agent_ / _history_)."],"exampleFix":"// before\nstrategy = KernelFunctionSelectionStrategy(function=fn, kernel=kernel)\n# kernel has no chat completion service configured -> invoke fails -> wrapped error\n\n// after\nkernel.add_service(AnthropicChatCompletion(ai_model_id=\"claude-3-5-sonnet-latest\", api_key=...))\nstrategy = KernelFunctionSelectionStrategy(function=fn, kernel=kernel)","handlingStrategy":"try-catch","validationCode":"# Validate the function executes with representative arguments before wiring it into the strategy\nresult = await strategy.function.invoke(kernel=kernel, arguments=test_args)\nif result is None:\n    raise ValueError('selection function returned None unexpectedly')","typeGuard":null,"tryCatchPattern":"from semantic_kernel.exceptions.agent_exceptions import AgentExecutionException\ntry:\n    agent = await strategy.next(agents, history)\nexcept AgentExecutionException as e:\n    cause = e.__cause__  # the real downstream error\n    logger.error('selection failed: %r', cause)","preventionTips":["Always inspect __cause__ on the wrapped exception","Test the selection function standalone with the same arguments","Ensure the kernel has an AI service and the function is registered","Confirm variable names match the strategy's agent/history variable names"],"tags":["agents","selection-strategy","kernel-function","agent-group-chat"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}