microsoft/semantic-kernel · error · ValueError
Agent does not exist. Please create the agent before deletin
Error message
Agent does not exist. Please create the agent before deleting it.
What it means
Raised by delete_agent when self.agent_model.agent_id is falsy. The delete_agent boto3 call needs an agentId; without one there is nothing to delete. Occurs when delete_agent is called on a BedrockAgent that was never created or was already deleted (agent_id is set to None after a successful delete).
Source
Thrown at python/semantic_kernel/agents/bedrock/bedrock_agent_base.py:127
self.bedrock_client.prepare_agent,
agentId=self.agent_model.agent_id,
),
)
# The agent will take some time to enter the PREPARING status after the prepare operation is called.
# We need to wait for the agent to reach the PREPARING status before we can proceed, otherwise we
# will return immediately if the agent is already in PREPARED status.
await self._wait_for_agent_status(BedrockAgentStatus.PREPARING)
# The agent will enter the PREPARED status when the preparation is complete.
await self._wait_for_agent_status(BedrockAgentStatus.PREPARED)
except ClientError as e:
logger.error(f"Failed to prepare agent {self.agent_model.agent_id}.")
raise e
async def delete_agent(self, **kwargs) -> None:
"""Delete an agent asynchronously."""
if not self.agent_model.agent_id:
raise ValueError("Agent does not exist. Please create the agent before deleting it.")
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."""View on GitHub (pinned to c028a0c7dc)
Solutions
- Guard the delete call: if agent.agent_model.agent_id: await agent.delete_agent().
- Avoid calling delete_agent twice; after the first successful call agent_id is None.
- Only call delete on agents created via create_and_prepare_agent or loaded with a valid agent_id.
Example fix
// before
await agent.delete_agent() # first call succeeds, sets agent_id=None
await agent.delete_agent() # second call raises
// after
if agent.agent_model.agent_id:
await agent.delete_agent() Defensive patterns
Strategy: validation
Validate before calling
def safe_delete(agent) -> bool:
return bool(agent.agent_model.agent_id)
# usage
if safe_delete(agent):
await agent.delete_agent() Type guard
def agent_has_id(agent) -> bool:
return bool(getattr(getattr(agent, "agent_model", None), "agent_id", None)) Try / catch
try:
await agent.delete_agent()
except ValueError as e:
if "create the agent before deleting" in str(e):
# already deleted or never created; safe to ignore
pass
else:
raise Prevention
- Guard delete_agent with an agent_id check to avoid double-delete errors.
- Track deletion state externally so cleanup code does not call delete twice.
- Only delete agents created via create_and_prepare_agent or loaded with a real agent_id.
When it happens
Trigger: Triggered when calling delete_agent() on an agent whose agent_model.agent_id is None/empty — e.g. a manually constructed agent, or calling delete twice (the first call sets agent_id = None at line 139).
Common situations: Double-deleting an agent (second call fails because agent_id was nulled); deleting a manually-constructed agent that was never provisioned; cleanup code that runs unconditionally.
Related errors
- Agent does not exist. Please create the agent before prepari
- Agent does not exist. Please create the agent before getting
- Agent does not exist. Please create the agent before creatin
- The Bedrock agent requires a message to be invoked.
- The streaming configuration must be null for non-streaming r
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/9709f279643c4b0f.
Report an issue: GitHub.