microsoft/semantic-kernel · error · ValueError
Agent does not exist. Please create the agent before disasso
Error message
Agent does not exist. Please create the agent before disassociating it with a knowledge base.
What it means
Raised by BedrockAgentBase.disassociate_agent_knowledge_base when self.agent_model.agent_id is falsy. Disassociating a knowledge base requires a created agent whose agent_id is known, since the underlying boto3 disassociate_agent_knowledge_base call needs an agentId parameter.
Source
Thrown at python/semantic_kernel/agents/bedrock/bedrock_agent_base.py:305
agentVersion=self.agent_model.agent_version,
knowledgeBaseId=knowledge_base_id,
**kwargs,
),
)
await self.prepare_agent_and_wait_until_prepared()
return response
except ClientError as e:
logger.error(
f"Failed to associate agent {self.agent_model.agent_id} with knowledge base {knowledge_base_id}."
)
raise e
async def disassociate_agent_knowledge_base(self, knowledge_base_id: str, **kwargs) -> None:
"""Disassociate an agent with a knowledge base."""
if not self.agent_model.agent_id:
raise ValueError(
"Agent does not exist. Please create the agent before disassociating it with a knowledge base."
)
try:
response = await run_in_executor(
None,
partial(
self.bedrock_client.disassociate_agent_knowledge_base,
agentId=self.agent_model.agent_id,
agentVersion=self.agent_model.agent_version,
knowledgeBaseId=knowledge_base_id,
**kwargs,
),
)
await self.prepare_agent_and_wait_until_prepared()
return responseView on GitHub (pinned to c028a0c7dc)
Solutions
- Ensure agent creation completed and agent_model.agent_id is set before calling disassociate_agent_knowledge_base.
- Guard teardown logic with a check on agent.agent_model.agent_id and skip disassociation if absent.
- If the agent may have been deleted, catch the error in cleanup paths and log rather than abort.
- Run creation in setup and verify the agent_id propagates to the object used for disassociation.
Example fix
// before
await agent.disassociate_agent_knowledge_base('kb-id') # raises if not created
// after
if agent.agent_model.agent_id:
await agent.disassociate_agent_knowledge_base('kb-id')
else:
logger.warning('Skipping disassociate: agent not created') Defensive patterns
Strategy: validation
Validate before calling
if agent.agent_model.agent_id:
await agent.disassociate_agent_knowledge_base(kb_id)
else:
logger.warning('Agent not created; skipping disassociate') Type guard
def is_bedrock_agent_created(agent) -> bool:
return bool(getattr(getattr(agent, 'agent_model', None), 'agent_id', None)) Try / catch
try:
await agent.disassociate_agent_knowledge_base(kb_id)
except ValueError:
logger.warning('Cannot disassociate; agent not created') Prevention
- Guard teardown paths with an agent_id check
- Avoid reusing deleted/uncleared agent objects for disassociation
When it happens
Trigger: Calling agent.disassociate_agent_knowledge_base('kb-id') before the agent was created on AWS, after the agent was deleted, or on an object built from settings where creation was skipped.
Common situations: Cleanup/teardown code that tries to disassociate knowledge bases from an agent that was never fully created; reusing a deleted agent object; assume-the-agent-exists logic in integration tests where setup failed.
Related errors
- Agent does not exist. Please create the agent before associa
- Agent does not exist. Please create the agent before listing
- Agent does not exist. Please create the agent before invokin
- Last message in chat history was null or whitespace.
- No file found in the response.
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/6ad117b5da6b7326.
Report an issue: GitHub.