{"record":{"id":"28d678fafb92b2b9","repo":"microsoft/semantic-kernel","slug":"agent-does-not-exist-please-create-the-agent-befo-28d678","errorCode":null,"errorMessage":"Agent does not exist. Please create the agent before getting it.","messagePattern":"Agent does not exist\\. Please create the agent before getting it\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/agents/bedrock/bedrock_agent_base.py","lineNumber":147,"sourceCode":"        try:\n            await run_in_executor(\n                None,\n                partial(\n                    self.bedrock_client.delete_agent,\n                    agentId=self.agent_model.agent_id,\n                    **kwargs,\n                ),\n            )\n\n            self.agent_model.agent_id = None\n        except ClientError as e:\n            logger.error(f\"Failed to delete agent {self.agent_model.agent_id}.\")\n            raise e\n\n    async def _get_agent(self) -> None:\n        \"\"\"Get an agent.\"\"\"\n        if not self.agent_model.agent_id:\n            raise ValueError(\"Agent does not exist. Please create the agent before getting it.\")\n\n        try:\n            response = await run_in_executor(\n                None,\n                partial(\n                    self.bedrock_client.get_agent,\n                    agentId=self.agent_model.agent_id,\n                ),\n            )\n\n            # Update the agent model\n            self.agent_model = BedrockAgentModel(**response[\"agent\"])\n        except ClientError as e:\n            logger.error(f\"Failed to get agent {self.agent_model.agent_id}.\")\n            raise e\n\n    async def _wait_for_agent_status(\n        self,","sourceCodeStart":129,"sourceCodeEnd":165,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/agents/bedrock/bedrock_agent_base.py#L129-L165","documentation":"Raised by the internal _get_agent when self.agent_model.agent_id is falsy. _get_agent calls bedrock_client.get_agent(agentId=...), which requires an ID. This method is called transitively by _wait_for_agent_status (status polling) and is the data-refresh path. Without an agent_id, there is no agent to fetch.","triggerScenarios":"Triggered when _get_agent() is invoked on a BedrockAgent without an agent_id — typically indirectly via _wait_for_agent_status -> _get_agent during prepare/create flows, or if called directly on an unprovisioned agent.","commonSituations":"Calling prepare_agent_and_wait_until_prepared (which polls status via _get_agent) on an agent constructed without agent_id; manually invoking _get_agent; a create flow that failed mid-way leaving agent_id unset.","solutions":["Ensure the agent is created (has a valid agent_id) before any status-polling or prepare operation.","Use create_and_prepare_agent which handles the full create -> wait -> prepare lifecycle.","If referencing an existing agent, construct the model with the real agent_id."],"exampleFix":"// before\nagent = BedrockAgent({'agentName': 'x'})\nawait agent._get_agent()  # no agent_id -> raises\n\n// after\nagent = await BedrockAgent.create_and_prepare_agent(name='x', instructions='...')\nawait agent._get_agent()  # agent_id is set","handlingStrategy":"validation","validationCode":"def assert_agent_exists(agent) -> None:\n    if not agent.agent_model.agent_id:\n        raise ValueError(\"Agent has no agent_id; cannot refresh agent state.\")","typeGuard":"def agent_has_id(agent) -> bool:\n    return bool(getattr(getattr(agent, \"agent_model\", None), \"agent_id\", None))","tryCatchPattern":"try:\n    await agent._get_agent()\nexcept ValueError as e:\n    if \"create the agent before getting\" in str(e):\n        agent = await BedrockAgent.create_and_prepare_agent(name=..., instructions=...)\n        await agent._get_agent()\n    else:\n        raise","preventionTips":["Ensure the agent is provisioned (has agent_id) before any status polling or prepare call.","Use create_and_prepare_agent for the full lifecycle rather than manual construction.","Validate agent_id presence before invoking internal data-refresh methods."],"tags":["bedrock","agent-management","missing-agent-id","internal","validation"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}