{"record":{"id":"6997411d7257b263","repo":"microsoft/semantic-kernel","slug":"agent-does-not-exist-please-create-the-agent-befo-699741","errorCode":null,"errorMessage":"Agent does not exist. Please create the agent before invoking it.","messagePattern":"Agent does not exist\\. Please create the agent before invoking it\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/agents/bedrock/bedrock_agent_base.py","lineNumber":360,"sourceCode":"                    **kwargs,\n                ),\n            )\n        except ClientError as e:\n            logger.error(f\"Failed to list associated knowledge bases for agent {self.agent_model.agent_id}.\")\n            raise e\n\n    # endregion Knowledge Base Management\n\n    async def _invoke_agent(\n        self,\n        thread_id: str,\n        message: str | ChatMessageContent,\n        agent_alias: str | None = None,\n        **kwargs,\n    ) -> dict[str, Any]:\n        \"\"\"Invoke an agent.\"\"\"\n        if not self.agent_model.agent_id:\n            raise ValueError(\"Agent does not exist. Please create the agent before invoking it.\")\n\n        if isinstance(message, ChatMessageContent) and message.role != AuthorRole.USER:\n            raise ValueError(\"Only user messages are supported for invoking a Bedrock agent.\")\n\n        agent_alias = agent_alias or self.WORKING_DRAFT_AGENT_ALIAS\n\n        try:\n            return await run_in_executor(\n                None,\n                partial(\n                    self.bedrock_runtime_client.invoke_agent,\n                    agentAliasId=agent_alias,\n                    agentId=self.agent_model.agent_id,\n                    sessionId=thread_id,\n                    inputText=message if isinstance(message, str) else message.content,\n                    **kwargs,\n                ),\n            )","sourceCodeStart":342,"sourceCodeEnd":378,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/agents/bedrock/bedrock_agent_base.py#L342-L378","documentation":"Raised by BedrockAgentBase._invoke_agent when self.agent_model.agent_id is falsy. Invoking a Bedrock agent requires an existing agent (and an alias) so bedrock_runtime_client.invoke_agent has an agentId to route to. This guard short-circuits before the AWS call.","triggerScenarios":"Invoking the agent via get_response/invoke before creation; creation failed or was never awaited; the agent_id was cleared. Also hit if a custom caller invokes _invoke_agent directly without setup.","commonSituations":"Calling agent.get_response() or agent.invoke() right after constructing a BedrockAgent without the create step; failed async create whose error was caught upstream; tests that skip the live AWS create.","solutions":["Complete agent creation (await create) and assert agent_model.agent_id before invoking.","Ensure the agent is prepared (prepare_agent_and_wait_until_prepared) and an alias exists or use the default WORKING_DRAFT_AGENT_ALIAS.","If creation fails, surface the ClientError instead of continuing to invoke.","Verify AWS credentials and bedrock:InvokeAgent permission are present."],"exampleFix":"// before\nresp = await agent.get_response('hi')  # raises: no agent_id\n// after\nawait agent.create()\nawait agent.prepare_agent_and_wait_until_prepared()\nresp = await agent.get_response('hi')","handlingStrategy":"validation","validationCode":"async def ensure_created_then_invoke(agent, thread_id, message):\n    if not agent.agent_model.agent_id:\n        await agent.create()\n    assert agent.agent_model.agent_id, 'agent_id still missing after create'\n    return await agent.get_response(message)","typeGuard":"def is_bedrock_agent_created(agent) -> bool:\n    return bool(getattr(getattr(agent, 'agent_model', None), 'agent_id', None))","tryCatchPattern":"try:\n    resp = await agent.get_response('hi')\nexcept ValueError as e:\n    if 'does not exist' in str(e):\n        await agent.create()\n        resp = await agent.get_response('hi')\n    else: raise","preventionTips":["Run create + prepare before first invoke","Verify bedrock:InvokeAgent IAM permissions"],"tags":["bedrock","agent-lifecycle","aws","invocation","validation"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}