microsoft/semantic-kernel · error · AgentInitializationException

Failed to initialize the Amazon Bedrock Agent settings: {e}

Error message

Failed to initialize the Amazon Bedrock Agent settings: {e}

What it means

Raised by BedrockAgent.create_and_prepare_agent when BedrockAgentSettings (a Pydantic KernelBaseSettings model) fails validation. BedrockAgentSettings requires agent_resource_role_arn and foundation_model, loaded from env vars prefixed BEDROCK_AGENT_ (or a .env file). A ValidationError means one or both required fields are missing or empty.

Source

Thrown at python/semantic_kernel/agents/bedrock/bedrock_agent.py:219

            function_choice_behavior (FunctionChoiceBehavior, optional): The function choice behavior for accessing
                the kernel functions and filters. Only FunctionChoiceType.AUTO is supported.
            arguments (KernelArguments, optional): The kernel arguments.
            prompt_template_config (PromptTemplateConfig, optional): The prompt template configuration.
            env_file_path (str, optional): The path to the environment file.
            env_file_encoding (str, optional): The encoding of the environment file.

        Returns:
            An instance of BedrockAgent with the created agent.
        """
        try:
            bedrock_agent_settings = BedrockAgentSettings(
                agent_resource_role_arn=agent_resource_role_arn,
                foundation_model=foundation_model,
                env_file_path=env_file_path,
                env_file_encoding=env_file_encoding,
            )
        except ValidationError as e:
            raise AgentInitializationException(f"Failed to initialize the Amazon Bedrock Agent settings: {e}") from e

        import boto3
        from botocore.exceptions import ClientError

        bedrock_runtime_client = bedrock_runtime_client or boto3.client("bedrock-agent-runtime")
        bedrock_client = bedrock_client or boto3.client("bedrock-agent")

        try:
            response = await run_in_executor(
                None,
                partial(
                    bedrock_client.create_agent,
                    agentName=name,
                    foundationModel=bedrock_agent_settings.foundation_model,
                    agentResourceRoleArn=bedrock_agent_settings.agent_resource_role_arn,
                    instruction=instructions,
                ),
            )

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Set the env vars: export BEDROCK_AGENT_AGENT_RESOURCE_ROLE_ARN=<iam-role-arn> and export BEDROCK_AGENT_FOUNDATION_MODEL=<model-id>.
  2. Pass the values explicitly to create_and_prepare_agent: agent_resource_role_arn=..., foundation_model=....
  3. Create a .env file in the project root with BEDROCK_AGENT_AGENT_RESOURCE_ROLE_ARN and BEDROCK_AGENT_FOUNDATION_MODEL, and pass env_file_path if it is not in the default location.
  4. Verify the env var names match the prefix BEDROCK_AGENT_ exactly.

Example fix

// before
agent = await BedrockAgent.create_and_prepare_agent(
    name="my-agent", instructions="..."
)  # missing role/model

// after
agent = await BedrockAgent.create_and_prepare_agent(
    name="my-agent",
    instructions="...",
    agent_resource_role_arn="arn:aws:iam::123456789012:role/BedrockAgentRole",
    foundation_model="anthropic.claude-3-sonnet-20240229-v1:0",
)
Defensive patterns

Strategy: try-catch

Validate before calling

import os

def validate_bedrock_settings_present() -> None:
    missing = [
        v for v in (
            "BEDROCK_AGENT_AGENT_RESOURCE_ROLE_ARN",
            "BEDROCK_AGENT_FOUNDATION_MODEL",
        ) if not os.getenv(v)
    ]
    if missing:
        raise EnvironmentError(f"Missing required env vars: {missing}")

Try / catch

from semantic_kernel.exceptions.agent_exceptions import AgentInitializationException

try:
    agent = await BedrockAgent.create_and_prepare_agent(
        name="x", instructions="...",
        agent_resource_role_arn=os.getenv("BEDROCK_AGENT_AGENT_RESOURCE_ROLE_ARN"),
        foundation_model=os.getenv("BEDROCK_AGENT_FOUNDATION_MODEL"),
    )
except AgentInitializationException as e:
    if "Failed to initialize" in str(e):
        # surface missing-settings guidance to the operator
        raise SystemExit("Set BEDROCK_AGENT_AGENT_RESOURCE_ROLE_ARN and BEDROCK_AGENT_FOUNDATION_MODEL") from e
    raise

Prevention

When it happens

Trigger: Called when BedrockAgentSettings(...) is constructed inside create_and_prepare_agent and agent_resource_role_arn/foundation_model are neither passed as arguments nor found in environment variables / .env file.

Common situations: Missing BEDROCK_AGENT_AGENT_RESOURCE_ROLE_ARN or BEDROCK_AGENT_FOUNDATION_MODEL environment variables; .env file not on the expected path; wrong env_file_encoding; typo in env var names; running in an environment (CI, container) without AWS config exported; forgot to call load_dotenv().

Related errors


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