crewAIInc/crewAI · error · BedrockValidationError

knowledge_base_id must contain only alphanumeric characters

Error message

knowledge_base_id must contain only alphanumeric characters

What it means

A BedrockValidationError raised when knowledge_base_id contains characters other than ASCII alphanumerics (checked with `all(c.isalnum() ...)`). AWS KB IDs are strictly [0-9A-Za-z]{10}; anything else — hyphens, underscores, punctuation, whitespace — fails this check and is re-wrapped by the validation wrapper.

Source

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

        if self.number_of_results is not None:
            vector_search_config["numberOfResults"] = self.number_of_results

        return {"vectorSearchConfiguration": vector_search_config}

    def _validate_parameters(self) -> None:
        """Validate the parameters according to AWS API requirements."""
        try:
            if not self.knowledge_base_id:
                raise BedrockValidationError("knowledge_base_id cannot be empty")
            if not isinstance(self.knowledge_base_id, str):
                raise BedrockValidationError("knowledge_base_id must be a string")
            if len(self.knowledge_base_id) > 10:
                raise BedrockValidationError(
                    "knowledge_base_id must be 10 characters or less"
                )
            if not all(c.isalnum() for c in self.knowledge_base_id):
                raise BedrockValidationError(
                    "knowledge_base_id must contain only alphanumeric characters"
                )

            if self.next_token:
                if not isinstance(self.next_token, str):
                    raise BedrockValidationError("next_token must be a string")
                if len(self.next_token) < 1 or len(self.next_token) > 2048:
                    raise BedrockValidationError(
                        "next_token must be between 1 and 2048 characters"
                    )
                if " " in self.next_token:
                    raise BedrockValidationError("next_token cannot contain spaces")

            if self.number_of_results is not None:
                if not isinstance(self.number_of_results, int):
                    raise BedrockValidationError("number_of_results must be an integer")
                if self.number_of_results < 1:
                    raise BedrockValidationError(

View on GitHub (pinned to 754d7323be)

Solutions

  1. Use the exact console-provided ID: 10 chars of [A-Z0-9] (typically uppercase).
  2. Sanitize config-sourced values: kb_id.strip() and reject if not re.fullmatch(r'[A-Za-z0-9]{10}', kb_id).
  3. Do not construct IDs from names/slugs — always look up the real ID.

Example fix

# before
tool = BedrockKBRetrieverTool(knowledge_base_id="my-kb-prod")

# after
import re
kb_id = "my-kb-prod"  # name, not ID — look up the real one
assert re.fullmatch(r"[A-Za-z0-9]{10}", kb_id), "expected a real KB ID"
tool = BedrockKBRetrieverTool(knowledge_base_id=kb_id)
Defensive patterns

Strategy: validation

Validate before calling

import re

if not re.fullmatch(r"[A-Za-z0-9]{10}", kb_id):
    raise ValueError(f"knowledge_base_id must be 10 alphanumeric chars, got: {kb_id!r}")

Type guard

def is_kb_id(v) -> bool:
    import re
    return isinstance(v, str) and bool(re.fullmatch(r"[A-Za-z0-9]{10}", v))

Try / catch

try:
    tool = BedrockKBRetrieverTool(knowledge_base_id=kb_id)
except BedrockValidationError as e:
    if "alphanumeric" in str(e):
        raise ValueError("KB ID contains invalid characters (hyphen/space/ARN?)") from e
    raise

Prevention

When it happens

Trigger: Passing an ID containing '-', '_', ':', '/', spaces, or Unicode characters — e.g. a slugified name 'my-kb-prod', an ARN fragment, or a copy-pasted ID with invisible whitespace. Note isalnum() also accepts Unicode letters/digits, which AWS rejects.

Common situations: Using internal naming conventions with hyphens and assuming they are valid IDs; pasting from rich-text/chat clients that introduce non-breaking spaces; non-ASCII environments producing Unicode digits.

Related errors


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