crewAIInc/crewAI · error · BedrockKnowledgeBaseError

Unexpected error: {e!s}

Error message

Unexpected error: {e!s}

What it means

The final except in BedrockKnowledgeBaseRetrievalTool._run(): any non-ClientError exception raised during client creation, the retrieve call, or response processing is re-raised as BedrockKnowledgeBaseError('Unexpected error: ...') with the original chained. Typical culprits are credential chain errors, response shape surprises (KeyError/TypeError on the response dict), or region config problems that botocore raises as non-ClientError exceptions.

Source

Thrown at lib/crewai-tools/src/crewai_tools/aws/bedrock/knowledge_base/retriever_tool.py:250

            if "guardrailAction" in response:
                response_object["guardrailAction"] = response["guardrailAction"]

            return json.dumps(response_object, indent=2)

        except ClientError as e:
            error_code = "Unknown"
            error_message = str(e)

            # Try to extract error code if available
            if hasattr(e, "response") and "Error" in e.response:
                error_code = e.response["Error"].get("Code", "Unknown")
                error_message = e.response["Error"].get("Message", str(e))

            raise BedrockKnowledgeBaseError(
                f"Error ({error_code}): {error_message}"
            ) from e
        except Exception as e:
            raise BedrockKnowledgeBaseError(f"Unexpected error: {e!s}") from e

View on GitHub (pinned to 754d7323be)

Solutions

  1. Read the chained exception (e.__cause__) to see the real error: NoRegionError -> set AWS_REGION; NoCredentialsError -> configure credentials/instance role
  2. Wrap the call and log e.__cause__ for diagnosis
  3. For parsing failures, dump the raw boto3 response and compare with the expected shape

Example fix

# before
try:
    out = tool._run(q)
except BedrockKnowledgeBaseError as e:
    print(e)
# after
try:
    out = tool._run(q)
except BedrockKnowledgeBaseError as e:
    logger.error('KB retrieve failed: %s', e.__cause__ or e)
    raise
Defensive patterns

Strategy: try-catch

Validate before calling

import os
for var in ('AWS_REGION', 'AWS_DEFAULT_REGION'):
    if os.getenv(var): break
else:
    os.environ['AWS_REGION'] = 'us-east-1'  # prevent NoRegionError

Try / catch

except BedrockKnowledgeBaseError as e:
    cause = e.__cause__
    logger.error('unexpected KB error: %s', cause or e)
    if isinstance(cause, (botocore.exceptions.NoCredentialsError, botocore.exceptions.NoRegionError)):
        raise SystemExit('fix AWS config') from e
    raise

Prevention

When it happens

Trigger: NoRegionError from botocore when region resolution fails; NoCredentialsError/PartialCredentialsError; a response missing an expected key causing KeyError in result processing; TypeError from unexpected payload shapes.

Common situations: Running in containers/CI without an AWS config file or region env var; assuming a credential profile that doesn't exist; AWS API response format changes breaking result parsing.

Related errors


AI-assisted analysis of crewAIInc/crewAI@754d7323be (2026-08-15). Data as JSON: /api/errors/35b999c994e775f7. Report an issue: GitHub.