{"record":{"id":"6966764a5b662714","repo":"microsoft/semantic-kernel","slug":"agent-does-not-exist-please-create-the-agent-befo","errorCode":null,"errorMessage":"Agent does not exist. Please create the agent before preparing it.","messagePattern":"Agent does not exist\\. Please create the agent before preparing it\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/agents/bedrock/bedrock_agent_base.py","lineNumber":103,"sourceCode":"    ) -> FunctionChoiceBehavior | None:\n        \"\"\"Validate the function choice behavior.\"\"\"\n        if function_choice_behavior and function_choice_behavior.type_ != FunctionChoiceType.AUTO:\n            # Users cannot specify REQUIRED or NONE for the Bedrock agents.\n            # Please note that the function choice behavior only control if the kernel will automatically\n            # execute the functions the agent requests. It does not control the behavior of the agent.\n            raise ValueError(\"Only FunctionChoiceType.AUTO is supported.\")\n        return function_choice_behavior\n\n    def __repr__(self):\n        \"\"\"Return the string representation of the Bedrock Agent.\"\"\"\n        return f\"{self.agent_model}\"\n\n    # region Agent Management\n\n    async def prepare_agent_and_wait_until_prepared(self) -> None:\n        \"\"\"Prepare the agent for use.\"\"\"\n        if not self.agent_model.agent_id:\n            raise ValueError(\"Agent does not exist. Please create the agent before preparing it.\")\n\n        try:\n            await run_in_executor(\n                None,\n                partial(\n                    self.bedrock_client.prepare_agent,\n                    agentId=self.agent_model.agent_id,\n                ),\n            )\n\n            # The agent will take some time to enter the PREPARING status after the prepare operation is called.\n            # We need to wait for the agent to reach the PREPARING status before we can proceed, otherwise we\n            # will return immediately if the agent is already in PREPARED status.\n            await self._wait_for_agent_status(BedrockAgentStatus.PREPARING)\n            # The agent will enter the PREPARED status when the preparation is complete.\n            await self._wait_for_agent_status(BedrockAgentStatus.PREPARED)\n        except ClientError as e:\n            logger.error(f\"Failed to prepare agent {self.agent_model.agent_id}.\")","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/agents/bedrock/bedrock_agent_base.py#L85-L121","documentation":"Raised by prepare_agent_and_wait_until_prepared when self.agent_model.agent_id is falsy (None or empty). The prepare_agent boto3 call requires an agentId, so the method guards upfront. This means the BedrockAgent instance was constructed without a real agent existing in the AWS service (e.g. from a dict without agent_id).","triggerScenarios":"Triggered when calling prepare_agent_and_wait_until_prepared() on a BedrockAgent whose agent_model.agent_id is None — e.g. constructed manually with an incomplete model dict rather than via create_and_prepare_agent.","commonSituations":"Constructing BedrockAgent({'agentName': '...'}) without first creating the agent in AWS; referencing an agent that was deleted; deserializing a stored config that lacked agent_id; calling prepare before create.","solutions":["Create the agent first using await BedrockAgent.create_and_prepare_agent(...), which provisions the agent and sets agent_id.","If the agent already exists in AWS, construct the model with the real agent_id from a get_agent response.","Check agent.agent_model.agent_id is set before calling prepare_agent_and_wait_until_prepared()."],"exampleFix":"// before\nagent = BedrockAgent({'agentName': 'my-agent', 'foundationModel': '...'})\nawait agent.prepare_agent_and_wait_until_prepared()  # no agent_id -> raises\n\n// after\nagent = await BedrockAgent.create_and_prepare_agent(\n    name='my-agent', instructions='...'\n)  # provisions and prepares\n# or, if it exists:\nagent = BedrockAgent({'agentId': 'EXISTING_ID', 'agentName': 'my-agent', ...})\nawait agent.prepare_agent_and_wait_until_prepared()","handlingStrategy":"validation","validationCode":"def assert_agent_exists(agent) -> None:\n    if not agent.agent_model.agent_id:\n        raise ValueError(\n            \"Agent has no agent_id. Create it via create_and_prepare_agent \"\n            \"before calling prepare_agent_and_wait_until_prepared().\"\n        )","typeGuard":"def agent_has_id(agent) -> bool:\n    return bool(getattr(getattr(agent, \"agent_model\", None), \"agent_id\", None))","tryCatchPattern":"try:\n    await agent.prepare_agent_and_wait_until_prepared()\nexcept ValueError as e:\n    if \"create the agent before preparing\" in str(e):\n        agent = await BedrockAgent.create_and_prepare_agent(name=..., instructions=...)\n        await agent.prepare_agent_and_wait_until_prepared()\n    else:\n        raise","preventionTips":["Always provision the agent via create_and_prepare_agent before prepare.","Check agent.agent_model.agent_id before calling lifecycle methods.","Do not construct BedrockAgent from incomplete dicts without an agent_id."],"tags":["bedrock","agent-management","missing-agent-id","prepare","validation"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}