microsoft/semantic-kernel · error · ValueError

Agent does not exist. Please create the agent before listing

Error message

Agent does not exist. Please create the agent before listing associated knowledge bases.

What it means

Raised by BedrockAgentBase.list_associated_agent_knowledge_bases when self.agent_model.agent_id is falsy. Listing knowledge bases is a per-agent query (boto3 list_agent_knowledge_bases needs agentId), so the agent must exist first.

Source

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

                    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 disassociate agent {self.agent_model.agent_id} with knowledge base {knowledge_base_id}."
            )
            raise e

    async def list_associated_agent_knowledge_bases(self, **kwargs) -> dict[str, Any]:
        """List associated knowledge bases with an agent."""
        if not self.agent_model.agent_id:
            raise ValueError("Agent does not exist. Please create the agent before listing associated knowledge bases.")

        try:
            return await run_in_executor(
                None,
                partial(
                    self.bedrock_client.list_agent_knowledge_bases,
                    agentId=self.agent_model.agent_id,
                    agentVersion=self.agent_model.agent_version,
                    **kwargs,
                ),
            )
        except ClientError as e:
            logger.error(f"Failed to list associated knowledge bases for agent {self.agent_model.agent_id}.")
            raise e

    # endregion Knowledge Base Management

    async def _invoke_agent(

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Create the agent first and confirm agent_model.agent_id is populated before listing knowledge bases.
  2. Move list calls after the creation step in your setup sequence.
  3. If listing is optional/diagnostic, wrap it in a guard on agent_id.
  4. Check that a prior ClientError during create was not silently swallowed.

Example fix

// before
kbs = await agent.list_associated_agent_knowledge_bases()  # raises
// after
await agent.create()
if agent.agent_model.agent_id:
    kbs = await agent.list_associated_agent_knowledge_bases()
Defensive patterns

Strategy: validation

Validate before calling

if not agent.agent_model.agent_id:
    raise RuntimeError('Create the Bedrock agent before listing knowledge bases')
return await agent.list_associated_agent_knowledge_bases()

Type guard

def is_bedrock_agent_created(agent) -> bool:
    return bool(getattr(getattr(agent, 'agent_model', None), 'agent_id', None))

Try / catch

try:
    kbs = await agent.list_associated_agent_knowledge_bases()
except ValueError as e:
    logger.warning(f'List skipped: {e}'); kbs = {}

Prevention

When it happens

Trigger: Calling agent.list_associated_agent_knowledge_bases() on a BedrockAgent that has not been created, whose create failed, or whose agent_id is None.

Common situations: Read/list operations issued during initialization before creation finishes; inspecting an uncreated agent in a notebook; CI where create was skipped or gated behind an env flag.

Related errors


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