crewAIInc/crewAI · error · BedrockValidationError

next_token must be a string

Error message

next_token must be a string

What it means

A BedrockValidationError raised when the optional next_token parameter is truthy but not a str. next_token is AWS's pagination cursor returned by a previous Retrieve call; the tool only type-checks it when provided, then further validates length (1-2048) and absence of spaces in the following checks.

Source

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

    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(
                        "number_of_results must be greater than 0"
                    )

        except BedrockValidationError as e:
            raise BedrockValidationError(f"Parameter validation failed: {e!s}") from e

View on GitHub (pinned to 754d7323be)

Solutions

  1. Extract and pass the string field: next_token=prev_response.get('nextToken') (it is already str in parsed JSON).
  2. Coerce with str(next_token) if the source is bytes.
  3. Omit next_token when there is no prior page.

Example fix

# before
result = tool._run(query="...", next_token=prev_response)  # dict

# after
result = tool._run(query="...", next_token=prev_response["nextToken"])  # str or None
Defensive patterns

Strategy: type-guard

Validate before calling

if next_token is not None and not isinstance(next_token, str):
    next_token = str(next_token)

Type guard

def is_next_token(v) -> bool:
    return v is None or (isinstance(v, str) and 1 <= len(v) <= 2048 and " " not in v)

Try / catch

try:
    tool = BedrockKBRetrieverTool(knowledge_base_id=kb_id, next_token=next_token)
except BedrockValidationError as e:
    if "next_token" in str(e):
        raise ValueError("Pass response['nextToken'] (a str) or omit it") from e
    raise

Prevention

When it happens

Trigger: Passing next_token as bytes, a dict, or an int — e.g. forwarding a raw header value or the whole previous response object instead of the string 'nextToken' field. Only fires when next_token is non-empty.

Common situations: Chaining Retrieve calls programmatically and passing response['nextToken'] from a JSON-decoded source that kept it as bytes; passing the entire response dict by mistake; test fixtures with placeholder tokens of the wrong type.

Related errors


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