microsoft/semantic-kernel · error · ValueError

Agent does not exist. Please create the agent before…

Error message

Agent does not exist. Please create the agent before getting it.

What it means

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.

Solutions

  1. Ensure the agent is created (has a valid agent_id) before any status-polling or prepare operation.
  2. Use create_and_prepare_agent which handles the full create -> wait -> prepare lifecycle.
  3. If referencing an existing agent, construct the model with the real agent_id.

Example fix

// before
agent = BedrockAgent({'agentName': 'x'})
await agent._get_agent()  # no agent_id -> raises

// after
agent = await BedrockAgent.create_and_prepare_agent(name='x', instructions='...')
await agent._get_agent()  # agent_id is set
Defensive patterns

Strategy: validation

Validate before calling

def assert_agent_exists(agent) -> None:
    if not agent.agent_model.agent_id:
        raise ValueError("Agent has no agent_id; cannot refresh agent state.")

Type guard

def agent_has_id(agent) -> bool:
    return bool(getattr(getattr(agent, "agent_model", None), "agent_id", None))

Try / catch

try:
    await agent._get_agent()
except ValueError as e:
    if "create the agent before getting" in str(e):
        agent = await BedrockAgent.create_and_prepare_agent(name=..., instructions=...)
        await agent._get_agent()
    else:
        raise

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/28d678fafb92b2b9. Report an issue: GitHub.

Appendix: source

Thrown at python/semantic_kernel/agents/bedrock/bedrock_agent_base.py:147

        try:
            await run_in_executor(
                None,
                partial(
                    self.bedrock_client.delete_agent,
                    agentId=self.agent_model.agent_id,
                    **kwargs,
                ),
            )

            self.agent_model.agent_id = None
        except ClientError as e:
            logger.error(f"Failed to delete agent {self.agent_model.agent_id}.")
            raise e

    async def _get_agent(self) -> None:
        """Get an agent."""
        if not self.agent_model.agent_id:
            raise ValueError("Agent does not exist. Please create the agent before getting it.")

        try:
            response = await run_in_executor(
                None,
                partial(
                    self.bedrock_client.get_agent,
                    agentId=self.agent_model.agent_id,
                ),
            )

            # Update the agent model
            self.agent_model = BedrockAgentModel(**response["agent"])
        except ClientError as e:
            logger.error(f"Failed to get agent {self.agent_model.agent_id}.")
            raise e

    async def _wait_for_agent_status(
        self,

View on GitHub (pinned to c028a0c7dc)