crewAIInc/crewAI · error · BedrockValidationError

next_token cannot contain spaces

Error message

next_token cannot contain spaces

What it means

Raised when the next_token value contains a space character. AWS pagination tokens are opaque single-token strings; embedded whitespace breaks them, so the tool rejects them client-side with BedrockValidationError before calling bedrock-agent-runtime. It is one of the next_token validation chain checks (string, length, no spaces).

Source

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

                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

    def _process_retrieval_result(self, result: dict[str, Any]) -> dict[str, Any]:
        """Process a single retrieval result from Bedrock Knowledge Base.

        Args:
            result (Dict[str, Any]): Raw result from Bedrock Knowledge Base

View on GitHub (pinned to 754d7323be)

Solutions

  1. Strip or remove whitespace: next_token.replace(' ', '') only if you know it was paste corruption; otherwise re-fetch a fresh token
  2. Copy tokens from raw JSON output rather than rendered logs
  3. Pass response['nextToken'] programmatically instead of via copy-paste

Example fix

# before
tool = BedrockKnowledgeBaseRetrievalTool(knowledge_base_id=kb_id, next_token=copied_token)
# after
if copied_token and ' ' not in copied_token:
    tool = BedrockKnowledgeBaseRetrievalTool(knowledge_base_id=kb_id, next_token=copied_token)
Defensive patterns

Strategy: validation

Validate before calling

if token and ' ' in token:
    raise ValueError('next_token contains spaces; re-copy from the raw response JSON')

Type guard

def is_space_free_token(t: str) -> bool:
    return ' ' not in t

Try / catch

except BedrockValidationError as e:
    if 'cannot contain spaces' in str(e):
        tool.next_token = None
        out = tool._run(q)  # restart from first page
    else:
        raise

Prevention

When it happens

Trigger: A token pasted from logs/terminal with an accidental space, a token wrapped or joined with spaces (' '.join(tokens)), or line-wrapped copies inserting spaces into BedrockKnowledgeBaseRetrievalTool(next_token=...).

Common situations: Copy-paste from wrapped terminal output or log lines that soft-wrap; concatenating token fragments; passing user-typed input directly as the token.

Related errors


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