crewAIInc/crewAI · error · BedrockValidationError

number_of_results must be an integer

Error message

number_of_results must be an integer

What it means

Raised when number_of_results is provided to BedrockKnowledgeBaseRetrievalTool but is not an int (bool also passes isinstance but e.g. float or str fails). The tool validates the value client-side because the Bedrock Agent Runtime API requires an integer retrievalQueryResultCount. Note that Python bool is a subclass of int, so True/False pass this check.

Source

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

                )
            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

        Returns:
            Dict[str, Any]: Processed result with standardized format
        """
        content_obj = result.get("content", {})

View on GitHub (pinned to 754d7323be)

Solutions

  1. Convert to int at the boundary: int(number_of_results)
  2. Validate config values after loading from files/env: number_of_results = int(value) if value else None
  3. Use a Pydantic model for tool config so type coercion happens automatically

Example fix

# before
tool = BedrockKnowledgeBaseRetrievalTool(knowledge_base_id=kb_id, number_of_results=os.getenv('KB_RESULTS'))
# after
results = os.getenv('KB_RESULTS')
tool = BedrockKnowledgeBaseRetrievalTool(knowledge_base_id=kb_id, number_of_results=int(results) if results else None)
Defensive patterns

Strategy: type-guard

Validate before calling

if number_of_results is not None and not isinstance(number_of_results, int):
    number_of_results = int(number_of_results)

Type guard

def is_valid_result_count(v: object) -> bool:
    return v is None or (isinstance(v, int) and not isinstance(v, bool) and v >= 1)

Try / catch

except BedrockValidationError as e:
    if 'number_of_results must be an integer' in str(e):
        tool.number_of_results = int(tool.number_of_results)
        out = tool._run(q)
    else:
        raise

Prevention

When it happens

Trigger: Passing number_of_results='5' (string from CLI/env var), 5.0 (float), or a numpy int64/float value to the constructor or tool input schema.

Common situations: Reading the value from os.environ or argparse without int conversion; loading config from JSON/YAML where the value parsed as float or string; passing results from a numpy computation.

Related errors


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