crewAIInc/crewAI · error · BedrockValidationError

knowledge_base_id must be a string

Error message

knowledge_base_id must be a string

What it means

A BedrockValidationError raised when knowledge_base_id is truthy but not a str instance. It follows the empty check in _validate_parameters and is re-wrapped into "Parameter validation failed: knowledge_base_id must be a string".

Source

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

        """Build the retrieval configuration based on provided parameters.

        Returns:
            Dict[str, Any]: The constructed retrieval configuration
        """
        vector_search_config = {}

        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")

View on GitHub (pinned to 754d7323be)

Solutions

  1. Coerce at the boundary: knowledge_base_id=str(kb_id).
  2. Use the correct field from boto3: response['knowledgeBase']['knowledgeBaseId'].
  3. Type config models (pydantic knowledge_base_id: str) so mismatches fail early.

Example fix

# before
resp = bedrock.create_knowledge_base(...)
tool = BedrockKBRetrieverTool(knowledge_base_id=resp["knowledgeBase"]["knowledgeBaseId"])

# after — the field is already str; if your source is numeric:
tool = BedrockKBRetrieverTool(knowledge_base_id=str(kb_id))
Defensive patterns

Strategy: type-guard

Validate before calling

if not isinstance(kb_id, str):
    kb_id = str(kb_id)

Type guard

def is_kb_id(v) -> bool:
    return isinstance(v, str) and bool(v)

Try / catch

try:
    tool = BedrockKBRetrieverTool(knowledge_base_id=kb_id)
except BedrockValidationError as e:
    if "must be a string" in str(e):
        tool = BedrockKBRetrieverTool(knowledge_base_id=str(kb_id))
    else:
        raise

Prevention

When it happens

Trigger: Constructing BedrockKBRetrieverTool with a numeric or otherwise non-string knowledge_base_id — e.g. an int from a config parser, or bytes from an external system.

Common situations: IDs stored as numbers in internal tooling; passing boto3 response objects (create_knowledge_base result) instead of the string 'knowledgeBaseId' field; scripting layers that coerce config values to native types.

Related errors


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